在百度地图开放平台找了好久都没找到天气预报的api文档说明。以下的代码都是参考某个在用系统的天气预告部分代码来写的,写来测试测试。简单通过百度天气预报api获取天气预报的有关信息。简单的PHP代码如下:
<?php
header("Content-type:text/html;charset=utf-8");
// 百度获取天气情况
$city_name="南宁市"; //城市名
$output="json"; //返回数据格式
$ak="3vMLUnjui3DNrgYHtmB62uSlX7hTtV5V"; //百度ak
$url = "http://api.map.baidu.com/telematics/v3/weather?location=$city_name&output=json&ak=$ak"; //获取数据的请求地址
$result = json_decode(getData($url),true);
echo "<pre>";
echo "天气预报信息<br/>";
print_r($result);
echo "<br/>天气预报信息";
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页面:
<?php
header("Content-type:text/html;charset=utf-8");
// 百度获取天气情况
$city_name="南宁市"; //城市名
$output="json"; //返回数据格式
$ak="3vMLUnjui3DNrgYHtmB62uSlX7hTtV5V"; //百度ak
$url = "http://api.map.baidu.com/telematics/v3/weather?location=$city_name&output=json&ak=$ak"; //获取数据的请求地址
$result = json_decode(getData($url),true);
// echo "<pre>";
// echo "天气预报信息<br/>";
// print_r($result);
// echo "<br/>天气预报信息";
// 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;
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
<title>天气预报</title>
<style type="text/css">
html,body,.container{
position: absolute;
overflow-y: hidden;
top: 0;
left: 0;
bottom: 0;
right: 0;
font-size: 1.2em;
width: 100%;
height: 100%;
border:none;
padding:0;
margin:0;
/*background-color: rgba(50,147,243,0.1);*/
/*height: 200px;*/
background: -webkit-linear-gradient(rgba(50,147,243,0.2), rgba(50,147,243,0.1)); /* Safari 5.1 - 6.0 */
background: -o-linear-gradient(rgba(50,147,243,0.2), rgba(50,147,243,0.1)); /* Opera 11.1 - 12.0 */
background: -moz-linear-gradient(rgba(50,147,243,0.2), rgba(50,147,243,0.1)); /* Firefox 3.6 - 15 */
background: linear-gradient(rgba(50,147,243,0.2), rgba(50,147,243,0.1)); /* 标准的语法(必须放在最后) */
}
*{
color: #fff;
}
.container{
overflow-y: auto;
}
.row{
width: 80%;
margin: 0 auto;
text-align: center;
clear: all;
}
.temp{
width: 80%;
}
h3{
font-size: 2.3em;
font-family: "agency fb";
margin: 0.3em auto;
}
.row i{
font-size: 1.4em;
margin: 0 auto;
}
p{
/*position: absolute;
left: 25%;*/
margin: 0 auto;
}
h1{
position: relative;
display: block;
width: 6em;
left: -1.2em;
font-size: 6em;
font-family: inherit;
margin: 0.2em auto 0 auto;
}
.now{
float: right;
/*position: absolute;
top: 0.5em;
right: 0.3em;*/
width: 6em;
height: 6em;
z-index: -1;
}
.row span{
font-size: 1.3em;
display: inline-block;
margin: 0.2em;
}
#weather{
font-size: 1.4em;
}
#day{
/*position: absolute;
top: 36%;
left: 0;*/
display: inline-block;
width: 100%;
font-size: 3em;
margin: 0.2em auto;
}
#index,#index li{
margin: 1em auto;
list-style-type: none;
padding: 0.8em;
border-radius: 0.6em;
}
#index{
height: 20em;
margin: 0;
overflow: auto;
}
#index li{
border: 1px #fff solid;
text-align: left;
}
#index span{
text-align: left;
/*float: left;*/
}
</style>
</head>
<body>
<div class="container">
<div class="row">
<h3><? echo $data['city']?></h3>
<i id="time">2016-9-5 12:30</i>
</div>
<div class="row">
<div class="temp">
<h1><? echo $data['temp']?></h1><img class="now" src="<? echo $data['dayImg']?>" alt="" />
<p><span id="weather"><? echo $data['weather']?> </span><br />
<span>PM2.5: <? echo $data['pm25']?></span><br />
<span><? echo $data['wd']?></span></p>
</div>
</div>
<div class="row">
<p id="day">
<? echo $data['temp1']?>
</p>
</div>
<div class="row">
<ul id="index">
<li>
<p>
<span><? echo $data['index']['0']['title']?>:</span>
<span><? echo $data['index']['0']['zs']?></span><br /><br />
<span><? echo $data['index']['0']['tipt']?>:</span><br />
<span> <? echo $data['index']['0']['des']?></span>
</p>
</li>
<li>
<p>
<span><? echo $data['index'][1]['title']?>:</span>
<span><? echo $data['index'][1]['zs']?></span><br /><br />
<span><? echo $data['index'][1]['tipt']?>:</span><br />
<span> <? echo $data['index'][1]['des']?></span>
</p>
</li>
<li>
<p>
<span><? echo $data['index'][2]['title']?>:</span>
<span><? echo $data['index'][2]['zs']?></span><br /><br />
<span><? echo $data['index'][2]['tipt']?>:</span><br />
<span> <? echo $data['index'][2]['des']?></span>
</p>
</li>
<li>
<p>
<span><? echo $data['index'][3]['title']?>:</span>
<span><? echo $data['index'][3]['zs']?></span><br /><br />
<span><? echo $data['index'][3]['tipt']?>:</span><br />
<span> <? echo $data['index'][3]['des']?></span>
</p>
</li>
<li>
<p>
<span><? echo $data['index'][4]['title']?>:</span>
<span><? echo $data['index'][4]['zs']?></span><br /><br />
<span><? echo $data['index'][4]['tipt']?>:</span><br />
<span> <? echo $data['index'][4]['des']?></span>
</p>
</li>
<li>
<p>
<span><? echo $data['index'][5]['title']?>:</span>
<span><? echo $data['index'][5]['zs']?></span><br /><br />
<span><? echo $data['index'][5]['tipt']?>:</span><br />
<span> <? echo $data['index'][5]['des']?></span>
</p>
</li>
</ul>
</div>
</div>
<script type="text/javascript">
var time,
myDate ,
elm = document.getElementById('time');
setInterval(function(){
myDate = new Date();
time = formatDateTime(myDate);
elm.innerHTML = time;
},1000);
//返回日期
var formatDate = function (date) {
var y = date.getFullYear();
var m = date.getMonth() + 1;
m = m < 10 ? '0' + m : m;
var d = date.getDate();
d = d < 10 ? ('0' + d) : d;
return y + '-' + m + '-' + d;
};
//返回日期和时间
var formatDateTime = function (date) {
var y = date.getFullYear();
var m = date.getMonth() + 1;
m = m < 10 ? ('0' + m) : m;
var d = date.getDate();
d = d < 10 ? ('0' + d) : d;
var h = date.getHours();
h=h < 10 ? ('0' + h) : h;
var minute = date.getMinutes();
minute = minute < 10 ? ('0' + minute) : minute;
var second=date.getSeconds();
second=second < 10 ? ('0' + second) : second;
return y + '-' + m + '-' + d+' '+h+':'+minute+':'+second;
};
</script>
</body>
</html>