在百度地图开放平台找了好久都没找到天气预报的api文档说明。以下的代码都是参考某个在用系统的天气预告部分代码来写的,写来测试测试。简单通过百度天气预报api获取天气预报的有关信息。简单的PHP代码如下:
";
echo "天气预报信息
";
print_r($result);
echo "
天气预报信息";
exit();
/*
* 用GET方式获取指定URL的数据
*/
function getData($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
?>
天气预报信息
Array
(
[error] => 0
[status] => success
[date] => 2017-02-16
[results] => Array
(
[0] => Array
(
[currentCity] => 南宁市
[pm25] => 58
[index] => Array
(
[0] => Array
(
[title] => 穿衣
[zs] => 较舒适
[tipt] => 穿衣指数
[des] => 建议着薄外套、开衫牛仔衫裤等服装。年老体弱者应适当添加衣物,宜着夹克衫、薄毛衣等。
)
[1] => Array
(
[title] => 洗车
[zs] => 较适宜
[tipt] => 洗车指数
[des] => 较适宜洗车,未来一天无雨,风力较小,擦洗一新的汽车至少能保持一天。
)
[2] => Array
(
[title] => 旅游
[zs] => 适宜
[tipt] => 旅游指数
[des] => 天气较好,温度适宜,总体来说还是好天气哦,这样的天气适宜旅游,您可以尽情地享受大自然的风光。
)
[3] => Array
(
[title] => 感冒
[zs] => 较易发
[tipt] => 感冒指数
[des] => 天气较凉,较易发生感冒,请适当增加衣服。体质较弱的朋友尤其应该注意防护。
)
[4] => Array
(
[title] => 运动
[zs] => 较适宜
[tipt] => 运动指数
[des] => 阴天,较适宜进行各种户内外运动。
)
[5] => Array
(
[title] => 紫外线强度
[zs] => 最弱
[tipt] => 紫外线强度指数
[des] => 属弱紫外线辐射天气,无需特别防护。若长期在户外,建议涂擦SPF在8-12之间的防晒护肤品。
)
)
[weather_data] => Array
(
[0] => Array
(
[date] => 周四 02月16日 (实时:21℃)
[dayPictureUrl] => http://api.map.baidu.com/images/weather/day/yin.png
[nightPictureUrl] => http://api.map.baidu.com/images/weather/night/duoyun.png
[weather] => 阴转多云
[wind] => 东南风微风
[temperature] => 21 ~ 16℃
)
[1] => Array
(
[date] => 周五
[dayPictureUrl] => http://api.map.baidu.com/images/weather/day/duoyun.png
[nightPictureUrl] => http://api.map.baidu.com/images/weather/night/duoyun.png
[weather] => 多云
[wind] => 东南风微风
[temperature] => 25 ~ 16℃
)
[2] => Array
(
[date] => 周六
[dayPictureUrl] => http://api.map.baidu.com/images/weather/day/duoyun.png
[nightPictureUrl] => http://api.map.baidu.com/images/weather/night/qing.png
[weather] => 多云转晴
[wind] => 东南风微风
[temperature] => 28 ~ 16℃
)
[3] => Array
(
[date] => 周日
[dayPictureUrl] => http://api.map.baidu.com/images/weather/day/qing.png
[nightPictureUrl] => http://api.map.baidu.com/images/weather/night/duoyun.png
[weather] => 晴转多云
[wind] => 东南风微风
[temperature] => 28 ~ 16℃
)
)
)
)
)
天气预报信息
下面是一个完成的test页面:
";
// echo "天气预报信息
";
// print_r($result);
// echo "
天气预报信息";
// exit();
if(!empty($result)&&$result['error']==0){
$weather=$result['results'][0];
//白天还是黑夜
$now = date("H:i");
$timeName = (strtotime($now) > strtotime('18:00')) ? "night" : "day";
//城市
$data['city'] = $weather['currentCity'];
//气温
$tempArr = explode(':', $weather['weather_data'][0]['date'],2);
$tempStr = trim($tempArr[1],')');
$data['temp'] = $tempStr;
$data['dayImg']=$weather['weather_data'][0][$timeName."PictureUrl"];
//PM2.5
$data['pm25'] = $weather['pm25'];
//风向
$data['wd'] = $weather['weather_data'][0]['wind'];
//天气状况:晴
$data['weather'] = $weather['weather_data'][0]['weather'];
//一天气温
$data['temp1'] = $weather['weather_data'][0]['temperature'];
// 各种指数
$data['index'] = $weather['index'];
}else{
exit("获取不到天气情况!");
}
/*
* 用GET方式获取指定URL的数据
*/
function getData($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
?>
天气预报
echo $data['city']?>
2016-9-5 12:30
echo $data['temp']?>
echo $data['weather']?>
PM2.5: echo $data['pm25']?>
echo $data['wd']?>
echo $data['temp1']?>
-
echo $data['index']['0']['title']?>:
echo $data['index']['0']['zs']?>
echo $data['index']['0']['tipt']?>:
echo $data['index']['0']['des']?>
-
echo $data['index'][1]['title']?>:
echo $data['index'][1]['zs']?>
echo $data['index'][1]['tipt']?>:
echo $data['index'][1]['des']?>
-
echo $data['index'][2]['title']?>:
echo $data['index'][2]['zs']?>
echo $data['index'][2]['tipt']?>:
echo $data['index'][2]['des']?>
-
echo $data['index'][3]['title']?>:
echo $data['index'][3]['zs']?>
echo $data['index'][3]['tipt']?>:
echo $data['index'][3]['des']?>
-
echo $data['index'][4]['title']?>:
echo $data['index'][4]['zs']?>
echo $data['index'][4]['tipt']?>:
echo $data['index'][4]['des']?>
-
echo $data['index'][5]['title']?>:
echo $data['index'][5]['zs']?>
echo $data['index'][5]['tipt']?>:
echo $data['index'][5]['des']?>