在這篇教程中,我們將介紹如何在微信公眾平臺(tái)上開發(fā)天氣預(yù)報(bào)功能。我們將使用中國(guó)天氣網(wǎng)的氣象數(shù)據(jù)接口來(lái)獲取天氣信息。
這篇教程將介紹以下內(nèi)容:
一、中國(guó)天氣網(wǎng)
中國(guó)天氣網(wǎng)提供了豐富的氣象數(shù)據(jù)接口。下面我們介紹主要的幾個(gè):
國(guó)家根節(jié)點(diǎn)接口:
http://Flash.weather.com.cn/wmaps/xml/china.xml
該接口以XML格式列出所有的省(自治區(qū)、直轄市)的中文名稱(quName)和拼音名稱(pyName)及省會(huì)城市的天氣信息,其中廣東的數(shù)據(jù)如下:
<city quName="廣東" pyName="guangdong" cityname="廣州" state1="2" state2="7" stateDetailed="陰轉(zhuǎn)小雨" tem1="8" tem2="15" windState="微風(fēng)"/>
該接口的主要功能是通過(guò)它獲取省級(jí)節(jié)點(diǎn)的拼音名稱。以廣東為例:其pyName是“Guangdong”。
省級(jí)節(jié)點(diǎn)接口(以廣東為例):
http://flash.weather.com.cn/wmaps/xml/Guangdong.xml
該接口以XML格式列出當(dāng)前省下轄所有市的中文名稱(quName)、拼音名稱(pyName)、中心坐標(biāo)、天氣信息以及城市編碼。其中深圳的數(shù)據(jù)如下所示:
<city cityX="409" cityY="257" cityname="深圳" centername="深圳" fontColor="FFFFFF" pyName="shenzhen" state1="1" state2="1" stateDetailed="多云" tem1="12" tem2="18" temNow="12" windState="微風(fēng)" windDir="東北風(fēng)" windPower="1級(jí)" humidity="74%" time="21:15" url="101280601"/>
在這個(gè)接口中主要獲取城市名稱及相應(yīng)的城市編碼。例如,深圳的城市編碼為:101280601??梢跃帉懗绦?qū)⑷珖?guó)城市拓?fù)浣Y(jié)構(gòu)采集下來(lái)。也可以使用方倍工作室已經(jīng)采集好的,點(diǎn)擊這里下載。
實(shí)時(shí)天氣數(shù)據(jù)(以深圳為例):
http://www.weather.com.cn/data/sk/101280601.html
該接口以JSON格式列出當(dāng)前城市的實(shí)時(shí)天氣狀況。如下所示:
{ "weatherinfo": { "city": "深圳", "cityid": "101280601", "temp": "12", "WD": "東北風(fēng)", "WS": "1級(jí)", "SD": "75%", "WSE": "1", "time": "21:20", "isRadar": "1", "Radar": "JC_RADAR_AZ9755_JB" }}
實(shí)時(shí)天氣數(shù)據(jù)參數(shù)說(shuō)明下所示:
六日天氣數(shù)據(jù)(以深圳為例):
http://m.weather.com.cn/data/101280601.html
該接口以JSON格式列出當(dāng)前城市的六日天氣狀況。如下所示:
{ "weatherinfo": { "city": "深圳", "city_en": "shenzhen", "date_y": "2014年2月15日", "date": "", "week": "星期六", "fchh": "18", "cityid": "101280601", "temp1": "12℃~18℃", "temp2": "16℃~21℃", "temp3": "17℃~23℃", "temp4": "11℃~18℃", "temp5": "10℃~15℃", "temp6": "12℃~17℃", "tempF1": "53.6℉~64.4℉", "tempF2": "60.8℉~69.8℉", "tempF3": "62.6℉~73.4℉", "tempF4": "51.8℉~64.4℉", "tempF5": "50℉~59℉", "tempF6": "53.6℉~62.6℉", "weather1": "多云", "weather2": "陰", "weather3": "多云", "weather4": "中雨", "weather5": "多云", "weather6": "陰", "img1": "1", "img2": "99", "img3": "2", "img4": "99", "img5": "1", "img6": "99", "img7": "8", "img8": "99", "img9": "1", "img10": "99", "img11": "2", "img12": "99", "img_single": "1", "img_title1": "多云", "img_title2": "多云", "img_title3": "陰", "img_title4": "陰", "img_title5": "多云", "img_title6": "多云", "img_title7": "中雨", "img_title8": "中雨", "img_title9": "多云", "img_title10": "多云", "img_title11": "陰", "img_title12": "陰", "img_title_single": "多云", "wind1": "微風(fēng)", "wind2": "微風(fēng)", "wind3": "微風(fēng)", "wind4": "東北風(fēng)3-4級(jí)", "wind5": "微風(fēng)", "wind6": "微風(fēng)", "fx1": "微風(fēng)", "fx2": "微風(fēng)", "fl1": "小于3級(jí)", "fl2": "小于3級(jí)", "fl3": "小于3級(jí)", "fl4": "3-4級(jí)", "fl5": "小于3級(jí)", "fl6": "小于3級(jí)", "index": "較舒適", "index_d": "建議著薄外套、開衫牛仔衫褲等服裝。年老體弱者應(yīng)適當(dāng)添加衣物,宜著夾克衫、薄毛衣等。", "index48": "較舒適", "index48_d": "建議著薄外套、開衫牛仔衫褲等服裝。年老體弱者應(yīng)適當(dāng)添加衣物,宜著夾克衫、薄毛衣等。", "index_uv": "最弱", "index48_uv": "最弱", "index_xc": "適宜", "index_tr": "適宜", "index_co": "舒適", "st1": "19", "st2": "13", "st3": "23", "st4": "16", "st5": "24", "st6": "18", "index_cl": "適宜", "index_ls": "適宜", "index_ag": "不易發(fā)" }}
未來(lái)六日天氣數(shù)據(jù)主要參數(shù)說(shuō)明如下表所示:
以上接口是我們做天氣預(yù)報(bào)功能將使用到的接口。
二、微信公眾平臺(tái)開發(fā)
1. 需要將城市編碼對(duì)照表導(dǎo)入到數(shù)據(jù)庫(kù)中,以下是SAE導(dǎo)入后截圖
2. 將城市名轉(zhuǎn)為城市代碼的方法,這將在用戶發(fā)送城市名的時(shí)候調(diào)用
1 function fromNameToCode($name) 2 { 3 $MySQL_host = SAE_MYSQL_HOST_M; 4 $mysql_host_s = SAE_MYSQL_HOST_S; 5 $mysql_port = SAE_MYSQL_PORT; 6 $mysql_user = SAE_MYSQL_USER; 7 $mysql_passWord = SAE_MYSQL_PASS; 8 $mysql_database = SAE_MYSQL_DB; 9 10 $mysql_table = "weather";11 $mysql_state = "SELECT * FROM ".$mysql_table." WHERE `cityName` = '".$name."'";12 13 $con = mysql_connect($mysql_host.':'.$mysql_port, $mysql_user, $mysql_password, true);14 if (!$con){15 die('Could not connect: ' . mysql_error());16 }17 mysql_query("SET NAMES 'UTF8'");18 mysql_select_db($mysql_database, $con);19 $result = mysql_query($mysql_state);20 $cityCode = "";21 while($row = mysql_fetch_array($result))22 {23 $cityCode = $row['cityCode']; 24 break;25 }26 mysql_close($con);27 return $cityCode;28 }
3. 編寫接口調(diào)用函數(shù),用于向天氣網(wǎng)發(fā)送cURL請(qǐng)求,獲取數(shù)據(jù)
1 function httPRequest($url) 2 { 3 $ch = curl_init(); 4 curl_setopt($ch, CURLOPT_URL, $url); 5 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 6 $output = curl_exec($ch); 7 curl_close($ch); 8 if ($output === FALSE){ 9 return "cURL Error: ". curl_error($ch);10 }11 return $output;12 }
4. 編寫獲取天氣數(shù)據(jù)的函數(shù),這里獲取實(shí)時(shí)天氣和未來(lái)3日天氣,并將返回結(jié)果封裝成圖文信息的數(shù)組
1 function getWeatherInfo($cityName) 2 { 3 $cityCode = fromNameToCode($cityName); 4 5 if ($cityCode == "") 6 { 7 return "錯(cuò)誤的城市名或者此城市不在數(shù)據(jù)庫(kù)中!"; 8 } 9 10 //獲取實(shí)時(shí)天氣11 $url = "http://www.weather.com.cn/data/sk/".$cityCode.".html";12 $output = httpRequest($url);13 $weather = json_decode($output, true); 14 $info = $weather['weatherinfo'];15 16 $weatherArray = array();17 $weatherArray[] = array("Title"=>$info['city']."天氣預(yù)報(bào)", "Descr5. 在微信公眾平臺(tái)接口函數(shù)中處理收到的文本消息時(shí),文本將為城市名,直接調(diào)用獲取天氣的函數(shù)
1 /* 2 方倍工作室 3 CopyRight 2013 All Rights Reserved 4 */ 5 6 define("TOKEN", "weixin"); 7 8 $wechatObj = new wechatCallbackapiTest(); 9 if (!isset($_GET['echostr'])) { 10 $wechatObj->responseMsg(); 11 }else{ 12 $wechatObj->valid(); 13 } 14 15 class wechatCallbackapiTest 16 { 17 public function valid() 18 { 19 $echoStr = $_GET["echostr"]; 20 if($this->checkSignature()){ 21 echo $echoStr; 22 exit; 23 } 24 } 25 26 private function checkSignature() 27 { 28 $signature = $_GET["signature"]; 29 $timestamp = $_GET["timestamp"]; 30
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注