Home Backend Development PHP Tutorial PHP implements automatic identification of the return content type of Restful API

PHP implements automatic identification of the return content type of Restful API

Jan 17, 2017 am 11:06 AM

如题,PHP如何自动识别第三方Restful API的内容,自动渲染成 json、xml、html、serialize、csv、php等数据?

其实这也不难,因为Rest API也是基于http协议的,只要我们按照协议走,就能做到自动化识别 API 的内容,方法如下:

1、API服务端要返回明确的 http Content-Type头信息,如:

Content-Type: application/json; charset=utf-8
Content-Type: application/xml; charset=utf-8
Content-Type: text/html; charset=utf-8
Copy after login

2、PHP端(客户端)接收到上述头信息后,再酌情自动化处理,参考代码如下:

<?php
// 请求初始化
$url = 'http://www.php.cn';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
 
// 返回的 http body 内容
$response = curl_exec($ch);
 
// 返回的 http header 的 Content-Type 的内容
$contentType = curl_getinfo($ch, 'content_type');
 
// 关闭请求资源
curl_close($ch);
 
// 结果自动格式输出
$autoDetectFormats = array(
 'application/xml' => 'xml',
 'text/xml'  => 'xml',
 'application/json' => 'json',
 'text/json'  => 'json',
 'text/csv'  => 'csv',
 'application/csv' => 'csv',
 'application/vnd.php.serialized' => 'serialize'
);
 
if (strpos($contentType, ';'))
{
 list($contentType) = explode(';', $contentType);
}
 
$contentType = trim($contentType);
 
if (array_key_exists($contentType, $autoDetectFormats))
{
 echo '_' . $autoDetectFormats[$contentType]($response);
}
 
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++
// 常用 格式化 方法
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
/**
 * 格式化xml输出
 */
function _xml($string)
{
 return $string ? (array)simplexml_load_string($string, 'SimpleXMLElement', LIBXML_NOCDATA) : array();
}
 
/**
 * 格式化csv输出
 */
function _csv($string)
{
 $data = array();
 
 $rows = explode("\n", trim($string));
 $headings = explode(',', array_shift($rows));
 foreach( $rows as $row )
 {
 // 利用 substr 去掉 开始 与 结尾 的 "
 $data_fields = explode('","', trim(substr($row, 1, -1)));
 if (count($data_fields) === count($headings))
 {
  $data[] = array_combine($headings, $data_fields);
 }
 }
 
 return $data;
}
 
/**
 * 格式化json输出
 */
function _json($string)
{
 return json_decode(trim($string), true);
}
 
/**
 * 反序列化输出
 */
function _serialize($string)
{
 return unserialize(trim($string));
}
 
/**
 * 执行PHP脚本输出
 */
function _php($string)
{
 $string = trim($string);
 $populated = array();
 eval("\$populated = \"$string\";");
 
 return $populated;
}
Copy after login

更多PHP实现自动识别Restful API的返回内容类型相关文章请关注PHP中文网!

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

Hot Article

Hot Article

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

11 Best PHP URL Shortener Scripts (Free and Premium)

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Working with Flash Session Data in Laravel

6 Extra Skills Every PHP Developer Should Have 6 Extra Skills Every PHP Developer Should Have Feb 28, 2025 am 10:52 AM

6 Extra Skills Every PHP Developer Should Have

Understand Arrays in PHP Understand Arrays in PHP Feb 28, 2025 am 10:53 AM

Understand Arrays in PHP

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

Build a React App With a Laravel Back End: Part 2, React

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Simplified HTTP Response Mocking in Laravel Tests

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

12 Best PHP Chat Scripts on CodeCanyon

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

cURL in PHP: How to Use the PHP cURL Extension in REST APIs

See all articles