PHP读取API注意事项

有时候多是乱码,倒不是由于json不是utf8格式,多是网页编码不对,须要在PHP下面添加:php

header("Content-type:text/json;charset=utf-8");express

下面是比较完备的PHP的curl代码:json

$host = "http://";
		$path = "/express/query";
		$method = "GET";
		$appcode = "";
		$headers = array();
		array_push($headers, "Authorization:APPCODE " . $appcode);
		$querys = "number=".$kd_number."&type=auto";
		$bodys = "";
		$url = $host . $path . "?" . $querys;

		$curl = curl_init();
		curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
		curl_setopt($curl, CURLOPT_URL, $url);
		curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
		curl_setopt($curl, CURLOPT_FAILONERROR, false);
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($curl, CURLOPT_HEADER, false);
		if (1 == strpos("$".$host, "https://"))
		{
			curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
			curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
		}
		$data = curl_exec($curl);
		//print_r($data); die();
		curl_close($curl);
		$json_obj = json_decode($data);

注意若是不要返回头部,要把HEADER设置成false,代码中已经含有对是不是https的断定。数组

用对象的格式读取:app

$json_obj->HeWeather5[0]->daily_forecast[0]->tmp->maxcurl

数组的选择遍历:编码

foreach ($json_obj->result->list as $list){ 
		if(1==$i){
			$i=0;
			continue;
		}
		
		$guocheng_list=$guocheng_list."时间:".$list->time."</br>"."状态:".$list->status."</br>";
		}
相关文章
相关标签/搜索