使用PHP多维数组重组方法,兼容API接口-php教程

资源魔 33 0
正在平常的php开发中,咱们通常会挪用第三方的API来餍足咱们的营业需要,然而会遇到前往数据格局没有对立的状况,特地是第三方api接口前往数据兼容咱们的api接口,这个时分咱们就需求应用到PHP多维数组重组的办法。

一、申请第三方API接口前往的格局

array(1) {
  [0] =>
  array(20) {
    'url' =>
    string(147) "http:/*************************************************/*****"
    'filemtime' =>
    int(1525688855)
    'app' =>
    string(11) "smarket_dev"
    'stream' =>
    string(23) "stream20180507102518_67"
    'start' =>
    int(1525688855)
    'end' =>
    int(1525689358)
    'm3u8' =>
    string(147) "http://*******************************/**************************/"
    'duration' =>
    int(503)
    'thumbnail' =>
    string(100) "https://cdn-************************/********************"
    'size' =>
    int(9259195)
    'width' =>
    int(640)
    'height' =>
    int(360)
    'begin' =>
    int(0)
    'uptime' =>
    int(1525689364)
    'update' =>
    int(1525689364)
    'id' =>
    string(24) "5af02c1415d5239acc6ee28e"
    'title' =>
    string(9) "不决义"
    'desc' =>
    string(9) "不决义"
    'case' =>
    string(1) "0"
    'caseName' =>
    string(3) "无"
  }
}

第三方API接口前往的普通都是数组格局的字符串,咱们就能够用数组行止理,解决成咱们想要的格局。

二、正在类中写一个解决的办法,挪用这个办法便可,前往的数据就是咱们想要的

function getRecordInfo($webcastId)
    {
        $app = 'webinar';
        $stream = $webcastId;

        $_access_id = '***********';
        $_access_key = '*************';
        $_openApiUrl = 'http://*************/*******/';

        $service = new \webinar\_services\webCast\Impl\AodianyunApi($_access_id, $_access_key, $_openApiUrl);
        $result = $service->vodGetInfo($app, $stream);
        foreach ($result as $value) {
            $results[] = [
                'createdTime' => $value['filemtime'],
                'id' => $value['stream'],
                'recordStartTime' => $value['start'],
                'recordEndTime' => $value['end'],
                'size' => $value['size'],
                'subject' => $value['title'],
                'url' => $value['url']
            ];
        }
        return $results;
    }

三、getRecordInfo前往的数据

array(100) {
  [0] =>
  array(7) {
    'createdTime' =>
    int(1527072944)
    'id' =>
    string(6) "stream"
    'recordStartTime' =>
    int(1527072944)
    'recordEndTime' =>
    int(1527073551)
    'size' =>
    int(131098618)
    'subject' =>
    string(9) "不决义"
    'url' =>
    string(105) "https://cdn-************************/********************"
  }
  [1] =>
  array(7) {
    'createdTime' =>
    int(1526029294)
    'id' =>
    string(6) "stream"
    'recordStartTime' =>
    int(1526029294)
    'recordEndTime' =>
    int(1526029826)
    'size' =>
    int(114636073)
    'subject' =>
    string(9) "不决义"
    'url' =>
    string(105) "https://cdn-************************/********************"
  }

四、思绪图:

界说解决第三方接口的getRecordInfo()=》正在getRecordInfo()中申请第三方api =》将第三方的api前往的数据给到result=》将result数据进行格局解决。

心愿本片文章能够协助到各人,文中若有谬误还望指出。谢谢!

更过PHP相干成绩请拜访PHP中文网:PHP视频教程

以上就是应用PHP多维数组重组办法,兼容API接口的具体内容,更多请存眷资源魔其它相干文章!

标签: php开发教程 php开发资料 php开发自学 多维数组

抱歉,评论功能暂时关闭!