百度天气预报接口
百度提供天氣預(yù)報查詢接口API,可以根據(jù)經(jīng)緯度/城市名查詢天氣情況,我們可以在微信公眾平臺開發(fā)中調(diào)用這一接口。
接口說明
根據(jù)經(jīng)緯度/城市名查詢天氣的結(jié)果
接口示例
http://api.map.baidu.com/telematics/v3/weather?location=北京&output=json&ak=yourkey&sn=yoursn
百度ak申請地址:http://lbsyun.baidu.com/apiconsole/key
sn算法見底部附錄
接口參數(shù)說明
| 參數(shù)類型 | 參數(shù)名稱 | 是否必須 | 具體描述 |
|---|---|---|---|
| String | ak | true | 開發(fā)者密鑰 |
| String | sn | false | 若用戶所用ak的校驗方式為sn校驗時該參數(shù)必須。 |
| String | location | true | 輸入城市名或經(jīng)緯度,城市名稱如:北京,經(jīng)緯度格式為lng,lat坐標(biāo)如: location=116.305145,39.982368;城市天氣預(yù)報中間"|"分隔,location=116.305145,39.982368| 122.305145,36.982368|…. |
| String | output | false | 輸出的數(shù)據(jù)格式,默認為xml格式,當(dāng)output設(shè)置為’json’時,輸出的為json格式的數(shù)據(jù); |
| String | coord_type | false | 請求參數(shù)坐標(biāo)類型,默認為gcj02經(jīng)緯度坐標(biāo)。允許的值為bd09ll、bd09mc、gcj02、wgs84。bd09ll表示百度經(jīng)緯度坐標(biāo),bd09mc表示百度墨卡托坐標(biāo),gcj02表示經(jīng)過國測局加密的坐標(biāo)。wgs84表示gps獲取的坐標(biāo)。 |
返回結(jié)果
| 參數(shù)名稱 | 含義 | 說明 | |
|---|---|---|---|
| currentCity | 當(dāng)前城市 | 返回城市名 | |
| status | 返回結(jié)果狀態(tài)信息 | ||
| date | 當(dāng)前時間 | 年-月-日 | |
| results | 天氣預(yù)報信息 | 白天可返回近期3天的天氣情況(今天、明天、后天)、晚上可返回近期4天的天氣情況(今天、明天、后天、大后天) | |
| results.currentCity | 當(dāng)前城市 | ||
| results.weather_data | weather_data.date | 天氣預(yù)報時間 | |
| weather_data.dayPictureUrl | 白天的天氣預(yù)報圖片url | ||
| weather_data.nightPictureUrl | 晚上的天氣預(yù)報圖片url | ||
| weather_data.weather | 天氣狀況 | 所有天氣情況(”|”分隔符):晴|多云|陰|陣雨|雷陣雨|雷陣雨伴有冰雹|雨夾雪|小雨|中雨|大雨|暴雨|大暴雨|特大暴雨|陣雪|小雪|中雪|大雪|暴雪|霧|凍雨|沙塵暴|小雨轉(zhuǎn)中雨|中雨轉(zhuǎn)大雨|大雨轉(zhuǎn)暴雨|暴雨轉(zhuǎn)大暴雨|大暴雨轉(zhuǎn)特大暴雨|小雪轉(zhuǎn)中雪|中雪轉(zhuǎn)大雪|大雪轉(zhuǎn)暴雪|浮塵|揚沙|強沙塵暴|霾 | |
| weather_data.wind | 風(fēng)力 | ||
| weather_data.temperature | 溫度 | ||
返回xml格式的數(shù)據(jù)
<?xml version="1.0" encoding="utf-8" ?>
<CityWeatherResponse>
<status>success</status>
<date>2014-05-03</date>
<results>
<currentCity>北京</currentCity>
<weather_data>
<date>周六(今天, 實時:22℃)</date>
<dayPictureUrl>http://api.map.baidu.com/images/weather/day/leizhenyu.png</dayPictureUrl>
<nightPictureUrl>http://api.map.baidu.com/images/weather/night/duoyun.png</nightPictureUrl>
<weather>雷陣雨轉(zhuǎn)多云</weather>
<wind>北風(fēng)5-6級</wind>
<temperature>24 ~ 11℃</temperature>
<date>周日</date>
<dayPictureUrl>http://api.map.baidu.com/images/weather/day/duoyun.png</dayPictureUrl>
<nightPictureUrl>http://api.map.baidu.com/images/weather/night/qing.png</nightPictureUrl>
<weather>多云轉(zhuǎn)晴</weather>
<wind>北風(fēng)4-5級</wind>
<temperature>19 ~ 8℃</temperature>
<date>周一</date>
<dayPictureUrl>http://api.map.baidu.com/images/weather/day/qing.png</dayPictureUrl>
<nightPictureUrl>http://api.map.baidu.com/images/weather/night/qing.png</nightPictureUrl>
<weather>晴</weather>
<wind>微風(fēng)</wind>
<temperature>21 ~ 9℃</temperature>
<date>周二</date>
<dayPictureUrl>http://api.map.baidu.com/images/weather/day/duoyun.png</dayPictureUrl>
<nightPictureUrl>http://api.map.baidu.com/images/weather/night/qing.png</nightPictureUrl>
<weather>多云轉(zhuǎn)晴</weather>
<wind>微風(fēng)</wind>
<temperature>21 ~ 10℃</temperature>
</weather_data>
</results>
</CityWeatherResponse>
返回json格式的數(shù)據(jù)
{
"error": 0,
"status": "success",
"date": "2014-05-03",
"results": [
{
"currentCity": "北京",
"weather_data": [
{
"date": "周六(今天, 實時:22℃)",
"dayPictureUrl": "http://api.map.baidu.com/images/weather/day/leizhenyu.png",
"nightPictureUrl": "http://api.map.baidu.com/images/weather/night/duoyun.png",
"weather": "雷陣雨轉(zhuǎn)多云",
"wind": "北風(fēng)5-6級",
"temperature": "24 ~ 11℃"
},
{
"date": "周日",
"dayPictureUrl": "http://api.map.baidu.com/images/weather/day/duoyun.png",
"nightPictureUrl": "http://api.map.baidu.com/images/weather/night/qing.png",
"weather": "多云轉(zhuǎn)晴",
"wind": "北風(fēng)4-5級",
"temperature": "19 ~ 8℃"
},
{
"date": "周一",
"dayPictureUrl": "http://api.map.baidu.com/images/weather/day/qing.png",
"nightPictureUrl": "http://api.map.baidu.com/images/weather/night/qing.png",
"weather": "晴",
"wind": "微風(fēng)",
"temperature": "21 ~ 9℃"
},
{
"date": "周二",
"dayPictureUrl": "http://api.map.baidu.com/images/weather/day/duoyun.png",
"nightPictureUrl": "http://api.map.baidu.com/images/weather/night/qing.png",
"weather": "多云轉(zhuǎn)晴",
"wind": "微風(fēng)",
"temperature": "21 ~ 10℃"
}
]
}
]
}
附錄1:sn計算算法
sn = MD5(urlencode(basicString + sk))
其中 basicString 的算法如下:
(1) get 請求
url 中 http://域名{uri}
basicString = uri
(2) post 請求
url 中 http://域名{uri} POST 參數(shù)按照key進行從小大到字母排序
然后拼裝成:k1= v1&k2= v2&k3=v3&...&kn=vn的格式=> {params}
basicString = uri +?+ params
SN簽名計算的PHP代碼參考示例:
/**
*@brief計算SN簽名算法
*@paramstring$akaccesskey
*@paramstring$sksecretkey
*@paramstring$urlurl值,例如:/geosearch/nearby不能帶hostname和querstring,也不能帶?
*@paramarray$querystring_arrays參數(shù)數(shù)組,key=>value形式。在計算簽名后不能重新排序,也不能添加或者刪除數(shù)據(jù)元素
*@paramstring$method只能為'POST'或者'GET'
*/
publicfunctioncaculateAKSN($ak,$sk,$url,$querystring_arrays,$method='GET'){
if($method==='POST'){
ksort($querystring_arrays);
}
$querystring=http_build_query($querystring_arrays);
returnmd5(urlencode($url.'?'.$querystring.$sk));
}
附錄2:返回碼定義
| 返回碼 | 定義 | 英文描述 |
|---|---|---|
| 0 | 正常 | |
| 1 | 服務(wù)器內(nèi)部錯誤 | |
| 2 | 請求參數(shù)非法 | |
| 3 | 權(quán)限校驗失敗 | |
| 4 | 配額校驗失敗 | |
| 5 | ak不存在或者非法 | |
| 101 | 服務(wù)禁用 | |
| 102 | 不通過白名單或者安全碼不對 | |
| 2xx | 無權(quán)限 | |
| 3xx | 配額錯誤 |
控制服務(wù)部分錯誤碼:
| 202-205、210、233 | 無請求權(quán)限 |
| 231 | 用戶uid,ak不存在 |
| 232 | 用戶、ak被封禁 |
| 234 | sn簽名計算錯誤 |
| 210 | 權(quán)限資源不存在 |
| 345 | 分鐘配額超額 |
| 346 | 月配額超額 |
| 347 | 年配額超額 |
| 348 | 永久配額超額無請求權(quán)限 |
| 355 | 日配額超額 |
| 350 | 配額資源不存在 |
微信中開發(fā)實現(xiàn)的方法,請訪問
微信公眾平臺開發(fā)(4)天氣預(yù)報
效果如下:
掃描下面的二維碼可體驗
總結(jié)
- 上一篇: 电信光纤入户怎么安装无线路由器光纤入户路
- 下一篇: 可不可以放弃你(直到我终于放弃你)