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.dhdzp.com';
$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 去掉 開始 與 結(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中的 == 運(yùn)算符進(jìn)行字符串比較
用PHP中的 == 運(yùn)算符進(jìn)行字符串比較...2006-11-11
php array_map與array_walk比較案例詳解
這篇文章主要介紹了php array_map與array_walk比較案例詳解,本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09
使用Canal實(shí)現(xiàn)PHP應(yīng)用程序與MySQL數(shù)據(jù)庫的實(shí)時(shí)數(shù)據(jù)同步
Canal是阿里巴巴開源的一個(gè)數(shù)據(jù)同步工具,可實(shí)現(xiàn)MySQL數(shù)據(jù)庫到其他數(shù)據(jù)源的實(shí)時(shí)同步,PHP應(yīng)用程序中可輕松使用,提高系統(tǒng)的可靠性和實(shí)時(shí)性,提供了豐富的API和文檔支持2023-04-04
PHP中IP地址與整型數(shù)字互相轉(zhuǎn)換詳解
這篇文章主要介紹了PHP中IP地址與整型數(shù)字互相轉(zhuǎn)換詳解,本文介紹了使用PHP函數(shù)ip2long與long2ip的使用,以及它們的BUG介紹,最后給出自己寫的兩個(gè)算法,需要的朋友可以參考下2014-08-08
php Redis函數(shù)用法實(shí)例總結(jié)【附php連接redis單例類】
這篇文章主要介紹了php Redis函數(shù)用法,結(jié)合實(shí)例形式總結(jié)分析了php redis操作常用函數(shù)及具體使用方法,并附帶php連接redis單例類,需要的朋友可以參考下2017-11-11
PHP中的strtr函數(shù)使用介紹(str_replace)
PHP的 strtr 函數(shù), 性能要比 str_replace 函數(shù)高, 可以代替 str_replace 來使用2011-10-10
php頁碼形式分頁函數(shù)支持靜態(tài)化地址及ajax分頁
這篇文章主要介紹了php頁碼形式分頁函數(shù),此分頁支持靜態(tài)化地址分頁和無鏈接地址時(shí)的ajax分頁,需要的朋友可以參考下2014-03-03

