• 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端(客户端)接收到上述头信息后,再酌情自动化处理,参考代码如下:

    <?php
    // 请求初始化
    $url = 'http://blog.snsgou.com/user/123456';
    $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("
    ", 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;
    }
    
  • 相关阅读:
    两个静态的页面嵌入动态页面进行传值
    网页滚动图片窗
    最近用VS2008SP1+ .NET Framework3.5SP1开发程序,使用了MsChart,但是部署到服务器的时候提示如下错误:
    使用ajax的登录页面
    动态创建DataTable
    滚动信息
    通过Web Services上传和下载文件
    弹出窗体
    ajax开源网址
    jQuery图片滚动十佳插件重点介绍
  • 原文地址:https://www.cnblogs.com/52php/p/5677687.html
Copyright © 2020-2023  润新知