【PHP】患者同程API实例

采用搜狗官方API,实时更新。

<?php
header('Access-Control-Allow-Origin:*');
header('Content-Type:text/json;charset=utf8');
/**
 * @name 蓝优
 * 幽梦数据 https://umqun.com/
**/
$api = myCurl('http://hhyfeed.sogoucdn.com/js/common/epidemic-search/main.js');
$data = json_decode($api,true);
foreach ($data as $list){
	$arr = trim($list['trainPath'],'[]');
	$Path = str_replace('"', '', $arr);
	$value[] = array(
        'type'=>$list["trafficType"],
        'num'=>$list["trafficNum"],
        'path'=>$Path,
        'time'=>$list["trafficTime"],
		'detail'=>$list["detail"],
		'url'=>$list["evidenceDocUrl"]
    );	
}
/**
* @return string
* 判断与输出
**/
if($value){
	echo json_encode(array('data'=>$value),JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE);
}else {
	echo "无数据";
}
/**
* @return string
* Curl GET
**/
function myCurl($url){
    $ch = curl_init();     // Curl 初始化  
    $timeout = 30;     // 超时时间:30s  
    $ua='Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36';    // 伪造抓取 UA
	$ip = mt_rand(11, 191) . "." . mt_rand(0, 240) . "." . mt_rand(1, 240) . "." . mt_rand(1, 240);
    curl_setopt($ch, CURLOPT_URL, $url);              // 设置 Curl 目标  
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);      // Curl 请求有返回的值  
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);     // 设置抓取超时时间  
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);        // 跟踪重定向  
    curl_setopt($ch, CURLOPT_REFERER, $url);   // 伪造来源网址  
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-FORWARDED-FOR:'.$ip, 'CLIENT-IP:'.$ip));  //伪造IP  
    curl_setopt($ch, CURLOPT_USERAGENT, $ua);   // 伪造ua   
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts  
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
	curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); //强制协议为1.0
	curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 ); //强制使用IPV4协议解析域名
    $content = curl_exec($ch);   
    curl_close($ch);    // 结束 Curl  
    return $content;    // 函数返回内容  
}