OS:Ubuntu 13.10
Server version: Apache/2.4.6 (Ubuntu)
PHP version:PHP 5.5.3-1ubuntu2 (cli)
在使用PHP调用weather.com.cn里的天气接口时,代码如下:
public function getWeather(){
$weatherInfo = file_get_contents("http://m.weather.com.cn/data/".$this->cityId.".html");
$weatherInfo=json_decode($weatherInfo);
print_r($weatherInfo);
}
其中$this->cityId是根据接口要求提供的城市编码,经过测试通过file_get_contents()拿到的南昌天气数据如下(json):
{"weatherinfo":{"city":"南昌","city_en":"nanchang","date_y":"2013年12月2日","date":"","week":"星期一","fchh":"08","cityid":"101240101","temp1":"18℃~8℃","temp2":"19℃~8℃","temp3":"17℃~6℃","temp4":"17℃~5℃","temp5":"15℃~5℃","temp6":"16℃~6℃","tempF1":"64.4℉~46.4℉","tempF2":"66.2℉~46.4℉","tempF3":"62.6℉~42.8℉","tempF4":"62.6℉~41℉","tempF5":"59℉~41℉","tempF6":"60.8℉~42.8℉","weather1":"晴","weather2":"晴","weather3":"晴","weather4":"晴转多云","weather5":"晴","weather6":"晴","img1":"0","img2":"99","img3":"0","img4":"99","img5":"0","img6":"99","img7":"0","img8":"1","img9":"0","img10":"99","img11":"0","img12":"99","img_single":"0","img_title1":"晴","img_title2":"晴","img_title3":"晴","img_title4":"晴","img_title5":"晴","img_title6":"晴","img_title7":"晴","img_title8":"多云","img_title9":"晴","img_title10":"晴","img_title11":"晴","img_title12":"晴","img_title_single":"晴","wind1":"微风","wind2":"微风","wind3":"微风","wind4":"微风","wind5":"微风","wind6":"微风","fx1":"微风","fx2":"微风","fl1":"小于3级","fl2":"小于3级","fl3":"小于3级","fl4":"小于3级","fl5":"小于3级","fl6":"小于3级","index":"较冷","index_d":"建议着大衣、呢外套加毛衣、卫衣等服装。体弱者宜着厚外套、厚毛衣。因昼夜温差较大,注意增减衣服。","index48":"较冷","index48_d":"建议着大衣、呢外套加毛衣、卫衣等服装。体弱者宜着厚外套、厚毛衣。因昼夜温差较大,注意增减衣服。","index_uv":"中等","index48_uv":"中等","index_xc":"适宜","index_tr":"适宜","index_co":"舒适","st1":"18","st2":"9","st3":"19","st4":"9","st5":"16","st6":"7","index_cl":"适宜","index_ls":"适宜","index_ag":"极不易发"}}
已经确定数据编码是UTF-8,而且经测试该格式符合json规范: 但是通过PHP中的json_decode()方法反编译后,使用print_r()输出数据为空,一直找不出问题在哪里,是否有人遇到过能解决,望赐教!
再言:同样的我通过淘宝的ip数据接口拿到的数据通过json_decode()方法反编译也得不到数据,而该源码在@xiaobai7513 的http://www.oschina.net/code/snippet_660778_15467文章里有提出。