高德关于获取天气接口
最近在做天氣這塊,在網(wǎng)上一搜有各種天氣接口,但是大多數(shù)都是收費的。偶然發(fā)現(xiàn),高德也提供天氣接口,一天可以免費調(diào)用100000次,就試了一下,還是蠻好用的,就拿出來分享一波。
高德API接口地址?https://lbs.amap.com/api/webservice/guide/api/weatherinfo/
第一步,申請”web服務(wù) API”密鑰(Key);
第二步,拼接HTTP請求URL,第一步申請的Key需作為必填參數(shù)一同發(fā)送;
第三步,接收HTTP請求返回的數(shù)據(jù)(JSON或XML格式),解析數(shù)據(jù)。
如無特殊聲明,接口的輸入?yún)?shù)和輸出數(shù)據(jù)編碼全部統(tǒng)一為UTF-8。
天氣API的服務(wù)地址:https://restapi.amap.com/v3/weather/weatherInfo?parameters
因為高德地圖接口里面返回的數(shù)據(jù):天氣現(xiàn)象,是數(shù)字表示的,所以我們需要首先定義一個天氣現(xiàn)象的數(shù)組,并且天氣數(shù)據(jù)在短時間內(nèi)不會太大的變化,可以做個緩存,利用框架自帶的緩存、寫到文件里,或者寫到redis等其他緩存中都是可以的。
public function getWeather(){// 定義天氣狀態(tài)數(shù)組$weatherArray = ['晴', '多云', '陰', '陣雨', '雷陣雨', '雷陣雨并伴有冰雹', '雨夾雪', '小雨', '中雨', '大雨', '暴雨', '大暴雨', '特大暴雨', '陣雪', '小雪', '中雪', '大雪', '暴雪', '霧', '凍雨', '沙塵暴', '小雨-中雨', '中雨-大雨', '大雨-暴雨', '暴雨-大暴雨', '大暴雨-特大暴雨', '小雪-中雪', '中雪-大雪', '大雪-暴雪', '浮沉', '揚沙', '強沙塵暴', '颮', '龍卷風(fēng)', '弱高吹雪', '輕霧', '霾'];S(['type' => 'file', 'expire' => 3600, 'prefix' => 'think_']);$cTime = date('YmdH');//刪除上一天的緩存for ($i = 0; $i < 24; $i ++) {$yTime = $i < 10 ? date('Ymd', time() - 86400) . '0' . $i : date('Ymd', time() - 86400) . $i;if (S($yTime) !== false) {S($yTime, null);}}//判斷當(dāng)天前一時間段的緩存是否存在if (S($cTime) !== false) {//輸出緩存中的天氣數(shù)據(jù):} else {// 獲取天氣數(shù)據(jù)//$url = 'http://restapi.amap.com/v3/weather/weatherInfo';$url = 'http://106.11.249.75/v3/weather/weatherInfo';$key = '';$city = '110101'; // 城市編碼,北京東城區(qū)$cDayParams = 'key=' . $key . '&city=' . $city . '&extensions=all&output=JSON';$cHourParams = 'key=' . $key . '&city=' . $city . '&extensions=base&output=JSON';// 判斷鏈接是否可用$retDay = get_headers($url . '?' . $cDayParams);$retHour = get_headers($url . '?' . $cHourParams);if ($retDay !== false && $retHour !== false) {$cDayWeatherArr = json_decode(getContentByCurl($url, $cDayParams), true);$cHourWeatherArr = json_decode(getContentByCurl($url, $cHourParams), true);if (preg_match('/200/', $retDay[0]) && preg_match('/200/', $retHour[0])) { $ret['min_tem'] = $cDayWeatherArr['forecasts'][0]['casts'][0]['nighttemp'];$ret['max_tem'] = $cDayWeatherArr['forecasts'][0]['casts'][0]['daytemp'];$ret['wea_style'] = $cHourWeatherArr['lives'][0]['weather'];$ret['temperature'] = $cHourWeatherArr['lives'][0]['temperature'];$ret['wea_pic'] = WEB_PATH . 'weather/d' . array_search($ret['wea_style'], $weatherArray) . '.png';// 將天氣存入緩存S($cTime, json_encode($ret));//輸出} else {//操作失敗}} else {//操作失敗}}}function getContentByCurl($url, $params = false, $ispost = 0) {$httpInfo = array();$ch = curl_init();curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);curl_setopt($ch, CURLOPT_TIMEOUT, 60);curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);if ($ispost) {curl_setopt($ch, CURLOPT_POST, true);curl_setopt($ch, CURLOPT_POSTFIELDS, $params);curl_setopt($ch, CURLOPT_URL, $url);} else {if ($params) {curl_setopt($ch, CURLOPT_URL, $url . '?' . $params);} else {curl_setopt($ch, CURLOPT_URL, $url);}}$response = curl_exec($ch);curl_close($ch);return $response; }?
關(guān)于城市編碼,網(wǎng)上都是可以搜到的;關(guān)于天氣現(xiàn)象數(shù)組,高德API中也有詳細的介紹,大概就是這樣了。
總結(jié)
以上是生活随笔為你收集整理的高德关于获取天气接口的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 桌面计算机用法,pointofix使用说
- 下一篇: bson实践