您的位置:首页 > 编程语言 > PHP开发

PHP解析百度heweather

2016-01-28 12:00 381 查看
<?php
$ch = curl_init();
$url = 'http://apis.baidu.com/heweather/pro/weather?city=beijing';
$header = array(
'apikey:**your key**',
);
// 添加apikey到header
curl_setopt($ch, CURLOPT_HTTPHEADER  , $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// 执行HTTP请求
curl_setopt($ch , CURLOPT_URL , $url);
$res = curl_exec($ch);
//首个key中含有空格,无法直接解析为object调用,先解析为array类型
$jsond = json_decode($res,TRUE);
//在array中就可以用双引号,这样把内容提取出来,再转回string,再转成object
$jsondd = json_decode(json_encode($jsond["HeWeather data service 3.0"][0]));
//这样就可以->调用了
print_r($jsondd->daily_forecast[3]->cond->txt_d);
//example
print_r($jsondd->hourly_forecast[0]->wind->deg);

?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  heweather url php 百度 curl