PHP實(shí)現(xiàn)自動(dòng)識(shí)別Restful API的返回內(nèi)容類型
如題,PHP如何自動(dòng)識(shí)別第三方Restful API的內(nèi)容,自動(dòng)渲染成 json、xml、html、serialize、csv、php等數(shù)據(jù)?
其實(shí)這也不難,因?yàn)镽est API也是基于http協(xié)議的,只要我們按照協(xié)議走,就能做到自動(dòng)化識(shí)別 API 的內(nèi)容,方法如下:
1、API服務(wù)端要返回明確的 http Content-Type頭信息,如:
Content-Type: application/json; charset=utf-8 Content-Type: application/xml; charset=utf-8 Content-Type: text/html; charset=utf-8
2、PHP端(客戶端)接收到上述頭信息后,再酌情自動(dòng)化處理,參考代碼如下:
<?php
// 請(qǐng)求初始化
$url = 'http://www.dbjr.com.cn';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
// 返回的 http body 內(nèi)容
$response = curl_exec($ch);
// 返回的 http header 的 Content-Type 的內(nèi)容
$contentType = curl_getinfo($ch, 'content_type');
// 關(guān)閉請(qǐng)求資源
curl_close($ch);
// 結(jié)果自動(dòng)格式輸出
$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 去掉 開(kāi)始 與 結(jié)尾 的 "
$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));
}
/**
* 執(zhí)行PHP腳本輸出
*/
function _php($string)
{
$string = trim($string);
$populated = array();
eval("\$populated = \"$string\";");
return $populated;
}
相關(guān)文章
基于PHP實(shí)現(xiàn)短信驗(yàn)證碼發(fā)送次數(shù)限制
這篇文章主要介紹了基于PHP實(shí)現(xiàn)短信驗(yàn)證碼發(fā)送次數(shù)限制,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07
PHP 登錄完成后如何跳轉(zhuǎn)上一訪問(wèn)頁(yè)面
訪問(wèn)網(wǎng)站頁(yè)面時(shí),有的頁(yè)面需要授權(quán)才能訪問(wèn),這時(shí)候就會(huì)要求用戶登錄,跳轉(zhuǎn)到登錄頁(yè)面login.php,怎么實(shí)現(xiàn)登錄后返回到剛才訪問(wèn)的頁(yè)面2014-01-01
PHP長(zhǎng)連接實(shí)現(xiàn)與使用方法詳解
這篇文章主要介紹了PHP長(zhǎng)連接實(shí)現(xiàn)與使用方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了php長(zhǎng)連接的概念、功能、實(shí)現(xiàn)與使用方法,需要的朋友可以參考下2018-02-02
PHP JSON出錯(cuò):Cannot use object of type stdClass as array解決方法
這篇文章主要介紹了PHP JSON出錯(cuò):Cannot use object of type stdClass as array解決方法,需要的朋友可以參考下2014-08-08

