当前位置:首页 > PHP教程 > php高级应用 > 列表

PHP实现自动识别Restful API的返回内容类型

发布:smiling 来源: PHP粉丝网  添加日期:2021-05-10 11:34:09 浏览: 评论:0 

这篇文章主要介绍了PHP实现自动识别Restful API的返回内容类型,并实现自动自动渲染成 json、xml、html、serialize、csv、php等数据格式输出,需要的朋友可以参考下

如题,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

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

  1. <?php 
  2. // 请求初始化 
  3. $url = 'https://www.phpfensi.com'; 
  4. $ch = curl_init(); 
  5. curl_setopt($ch, CURLOPT_URL, $url); 
  6. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
  7. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30); 
  8.  
  9. // 返回的 http body 内容 
  10. $response = curl_exec($ch); 
  11.  
  12. // 返回的 http header 的 Content-Type 的内容 
  13. $contentType = curl_getinfo($ch, 'content_type'); 
  14.  
  15. // 关闭请求资源 
  16. curl_close($ch); 
  17.  
  18. // 结果自动格式输出 
  19. $autoDetectFormats = array( 
  20.  'application/xml' => 'xml', 
  21.  'text/xml'  => 'xml', 
  22.  'application/json' => 'json', 
  23.  'text/json'  => 'json', 
  24.  'text/csv'  => 'csv', 
  25.  'application/csv' => 'csv', 
  26.  'application/vnd.php.serialized' => 'serialize' 
  27. ); 
  28.  
  29. if (strpos($contentType, ';')) 
  30. { 
  31.  list($contentType) = explode(';', $contentType); 
  32. } 
  33.  
  34. $contentType = trim($contentType); 
  35.  
  36. if (array_key_exists($contentType, $autoDetectFormats)) 
  37. { 
  38.  echo '_' . $autoDetectFormats[$contentType]($response); 
  39. } 
  40.  
  41. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
  42. // 常用 格式化 方法 
  43. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
  44.  
  45. /** 
  46.  * 格式化xml输出 
  47.  */ 
  48. function _xml($string) 
  49. { 
  50.  return $string ? (array)simplexml_load_string($string, 'SimpleXMLElement', LIBXML_NOCDATA) : array(); 
  51. } 
  52.  
  53. /** 
  54.  * 格式化csv输出 
  55.  */ 
  56. function _csv($string) 
  57. { 
  58.  $data = array(); 
  59.  
  60.  $rows = explode("\n", trim($string)); 
  61.  $headings = explode(',', array_shift($rows)); 
  62.  foreach( $rows as $row ) 
  63.  { 
  64.  // 利用 substr 去掉 开始 与 结尾 的 " 
  65.  $data_fields = explode('","', trim(substr($row, 1, -1))); 
  66.  if (count($data_fields) === count($headings)) 
  67.  { 
  68.   $data[] = array_combine($headings, $data_fields); 
  69.  } 
  70.  } 
  71.  
  72.  return $data; 
  73. } 
  74.  
  75. /** 
  76.  * 格式化json输出 
  77.  */ 
  78. function _json($string) 
  79. { 
  80.  return json_decode(trim($string), true); 
  81. } 
  82.  
  83. /** 
  84.  * 反序列化输出 
  85.  */ 
  86. function _serialize($string) 
  87. { 
  88.  return unserialize(trim($string)); 
  89. } 
  90.  
  91. /** 
  92.  * 执行PHP脚本输出 
  93.  */ 
  94. function _php($string) 
  95. { 
  96.  $string = trim($string); 
  97.  $populated = array(); 
  98.  eval("\$populated = \"$string\";"); 
  99.  
  100.  return $populated; 
  101. }

Tags: PHP自动识别 Restful

分享到: