• 技术文章 >后端开发 >php教程

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

    王林王林2019-08-22 09:54:32原创743
    在平时的php开发中,我们通常会调用第三方的API来满足我们的业务需求,但是会遇到返回数据格式不统一的情况,特别是第三方api接口返回数据兼容我们的api接口,这个时候我们就需要使用到PHP多维数组重组的方法。

    1、请求第三方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接口返回的一般都是数组格式的字符串,我们就可以用数组去处理,处理成我们想要的格式。

    2、在类中写一个处理的方法,调用这个方法即可,返回的数据就是我们想要的

    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;
        }

    3、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-************************/********************"
      }

    4、思路图:

    定义处理第三方接口的getRecordInfo()=》在getRecordInfo()中请求第三方api =》将第三方的api返回的数据给到result=》将result数据进行格式处理。

    希望本片文章可以帮助到大家,文中如有错误还望指出。谢谢!

    更过PHP相关问题请访问PHP中文网:PHP视频教程

    以上就是使用PHP多维数组重组方法,兼容API接口的详细内容,更多请关注php中文网其它相关文章!

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    专题推荐:多维数组
    上一篇:php单引号和双引号用法 下一篇:php如何获取上传文件大小
    20期PHP线上班

    相关文章推荐

    • 【活动】充值PHP中文网VIP即送云服务器• php多维数组中将每个单元值翻倍• PHP多维数组指定多字段排序的示例代码_php实例• php中如何将元素添加到多维数组• 如何从PHP多维数组中删除重复值?
    1/1

    PHP中文网