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

高洛峰
Release: 2023-03-04 21:42:01
Original
1298 people have browsed it

如题,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 = &#39;http://www.php.cn&#39;;
$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, &#39;content_type&#39;);
 
// 关闭请求资源
curl_close($ch);
 
// 结果自动格式输出
$autoDetectFormats = array(
 &#39;application/xml&#39; => &#39;xml&#39;,
 &#39;text/xml&#39;  => &#39;xml&#39;,
 &#39;application/json&#39; => &#39;json&#39;,
 &#39;text/json&#39;  => &#39;json&#39;,
 &#39;text/csv&#39;  => &#39;csv&#39;,
 &#39;application/csv&#39; => &#39;csv&#39;,
 &#39;application/vnd.php.serialized&#39; => &#39;serialize&#39;
);
 
if (strpos($contentType, &#39;;&#39;))
{
 list($contentType) = explode(&#39;;&#39;, $contentType);
}
 
$contentType = trim($contentType);
 
if (array_key_exists($contentType, $autoDetectFormats))
{
 echo &#39;_&#39; . $autoDetectFormats[$contentType]($response);
}
 
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++
// 常用 格式化 方法
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
/**
 * 格式化xml输出
 */
function _xml($string)
{
 return $string ? (array)simplexml_load_string($string, &#39;SimpleXMLElement&#39;, LIBXML_NOCDATA) : array();
}
 
/**
 * 格式化csv输出
 */
function _csv($string)
{
 $data = array();
 
 $rows = explode("\n", trim($string));
 $headings = explode(&#39;,&#39;, array_shift($rows));
 foreach( $rows as $row )
 {
 // 利用 substr 去掉 开始 与 结尾 的 "
 $data_fields = explode(&#39;","&#39;, 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中文网!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template