Use PHP multi-dimensional array reorganization method, compatible with API interface

王林
Release: 2023-04-07 08:30:01
Original
1886 people have browsed it

In normal PHP development, we usually call third-party APIs to meet our business needs, but we will encounter situations where the return data format is not uniform, especially when the data returned by the third-party API interface is compatible with our API interface. , at this time we need to use the PHP multi-dimensional array reorganization method.

1. Request the format returned by the third-party API interface

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) "无" } }
Copy after login

The third-party API interface returns generally a string in array format, we can Use arrays to process it and process it into the format we want.

2. Write a processing method in the class, just call this method, and the data returned is what we want.

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; }
Copy after login

3. Data returned by 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-************************/********************" }
Copy after login

4. Idea map:

Define getRecordInfo() that handles the third-party interface =》Request the third-party api in getRecordInfo()=》Give the data returned by the third-party api to the result=》Format the result data.

I hope this article can help everyone. If there are any errors in the article, please point it out. Thanks!

For more information on PHP related issues, please visit the PHP Chinese website:PHP Video Tutorial

The above is the detailed content of Use PHP multi-dimensional array reorganization method, compatible with API interface. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!