<?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);
?>
PHP解析百度heweather
最新推荐文章于 2024-04-06 09:52:02 发布