《PHP實戰(zhàn):微信公眾平臺開發(fā)(五) 天氣預(yù)報功能開發(fā)》要點:
本文介紹了PHP實戰(zhàn):微信公眾平臺開發(fā)(五) 天氣預(yù)報功能開發(fā),希望對您有用。如果有疑問,可以聯(lián)系我們。
一、簡介PHP實戰(zhàn)
前面幾篇文章對微信公眾平臺的開通及簡單使用做了簡單的介紹,但都沒有涉及到實際使用中的問題,例如天氣查詢,公交查詢,快遞查詢等.接下來的幾篇文章將對實際生活中會經(jīng)常使用到的一些功能進行開發(fā)講解,以供讀者參考.PHP實戰(zhàn)
這一篇文章將對大家每天都會關(guān)心的天氣查詢進行開發(fā),例如,用戶發(fā)送消息 “蘇州天氣”,則會返回蘇州實時天氣狀況,以及未來兩天甚至未來五天的天氣狀況.PHP實戰(zhàn)
二、思路分析PHP實戰(zhàn)
首先要對用戶發(fā)送過來的消息進行判斷,判斷消息里是否含有“天氣”關(guān)鍵字,如果含有,則需要繼續(xù)提取地區(qū)信息,然后再通過中國天氣網(wǎng)(http://www.weather.com.cn)提供的開放API進行相關(guān)地區(qū)天氣查詢.PHP實戰(zhàn)
三、關(guān)鍵字判斷與地區(qū)讀取PHP實戰(zhàn)
用戶發(fā)送過來查詢天氣的消息的格式是固定好的,即 “地區(qū)+天氣”,所以首先截取后兩個字,判斷是否為 “天氣” 關(guān)鍵字.PHP實戰(zhàn)
使用php函數(shù) mb_substr() 截取,關(guān)于該函數(shù)的用法:
PHP實戰(zhàn)
mb_substr ― 獲取字符串的部分PHP實戰(zhàn)
string mb_substr ( string $str , int $start [, int $length [, string $encoding ]] )
說明:根據(jù)字符數(shù)執(zhí)行一個多字節(jié)安全的 substr() 操作. 位置是從 str 的開始位置進行計數(shù). 第一個字符的位置是 0.第二個字符的位置是 1,以此類推.PHP實戰(zhàn)
參數(shù):
PHP實戰(zhàn)
str
PHP實戰(zhàn)
從該 string 中提取子字符串.PHP實戰(zhàn)
start
PHP實戰(zhàn)
str 中要使用的第一個字符的位置.
PHP實戰(zhàn)
正數(shù) -> 從字符串開頭指定位置開始;
PHP實戰(zhàn)
負數(shù) -> 從字符串結(jié)尾指定位置開始;PHP實戰(zhàn)
length
PHP實戰(zhàn)
str 中要使用的最大字符數(shù).
PHP實戰(zhàn)
正數(shù) -> 從 start 處開始最多包括 length 個字符;
PHP實戰(zhàn)
負數(shù) -> string 末尾處的 length 個字符將會被漏掉(若 start 是負數(shù)則從字符串開頭算起).PHP實戰(zhàn)
encoding
PHP實戰(zhàn)
encoding 參數(shù)為字符編碼.如果省略,則使用內(nèi)部字符編碼.PHP實戰(zhàn)
返回值:
PHP實戰(zhàn)
mb_substr() 函數(shù)根據(jù) start 和 length 參數(shù)返回 str 中指定的部分.
PHP實戰(zhàn)
$str = mb_substr($keyword,-2,2,"UTF-8");
從消息的結(jié)尾數(shù)第二個字符開始截取,截取兩個字符,然后加以判斷是否為 “天氣” 關(guān)鍵字.PHP實戰(zhàn)
下面進行地區(qū)提取,還是使用 mb_substr() 函數(shù).PHP實戰(zhàn)
$str_key = mb_substr($keyword,0,-2,"UTF-8");PHP實戰(zhàn)
從消息的開頭開始,截掉末尾的兩個字符(天氣),既得地區(qū)關(guān)鍵字.PHP實戰(zhàn)
然后進行判斷,繼而調(diào)用函數(shù)查詢天氣數(shù)據(jù).PHP實戰(zhàn)
if($str == '天氣' && !empty($str_key)) { //調(diào)用函數(shù)查詢天氣數(shù)據(jù) }
四、調(diào)用 weather() 函數(shù)查詢PHP實戰(zhàn)
我們這里調(diào)用的是中國國家氣象局提供的天氣預(yù)報API接口,接口地址:http://m.weather.com.cn/data/101190401.htmlPHP實戰(zhàn)
URL中的數(shù)字指代城市的編號101190401(蘇州),其他城市對應(yīng)關(guān)系將在下面提供.PHP實戰(zhàn)
該接口返回信息比較全面,也是以json格式提供,格式如下:PHP實戰(zhàn)
{"weatherinfo":{ //基本信息; "city":"蘇州","city_en":"suzhou", "date_y":"2013年7月9日","date":"","week":"星期二","fchh":"18","cityid":"101190401", //攝氏溫度 "temp1":"30℃~37℃", "temp2":"30℃~37℃", "temp3":"29℃~35℃", "temp4":"27℃~33℃", "temp5":"27℃~31℃", "temp6":"27℃~35℃", //華氏溫度; "tempF1":"86H~98.6H", "tempF2":"86H~98.6H", "tempF3":"84.2H~95H", "tempF4":"80.6H~91.4H", "tempF5":"80.6H~87.8H", "tempF6":"80.6H~95H", //天氣描述; "weather1":"晴轉(zhuǎn)多云", "weather2":"晴轉(zhuǎn)多云", "weather3":"晴轉(zhuǎn)多云", "weather4":"多云", "weather5":"雷陣雨轉(zhuǎn)中雨", "weather6":"雷陣雨轉(zhuǎn)多云", //天氣描述圖片序號 "img1":"0", "img2":"1", "img3":"0", "img4":"1", "img5":"0", "img6":"1", "img7":"1", "img8":"99", "img9":"4", "img10":"8", "img11":"4", "img12":"1", //圖片名稱; "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":"多云", //風(fēng)速描述 "wind1":"西南風(fēng)3-4級", "wind2":"西南風(fēng)3-4級", "wind3":"東南風(fēng)3-4級", "wind4":"東南風(fēng)3-4級轉(zhuǎn)4-5級", "wind5":"東南風(fēng)4-5級轉(zhuǎn)西南風(fēng)3-4級", "wind6":"西南風(fēng)3-4級轉(zhuǎn)4-5級", //風(fēng)速級別描述 "fx1":"西南風(fēng)", "fx2":"西南風(fēng)", "fl1":"3-4級", "fl2":"3-4級", "fl3":"3-4級", "fl4":"3-4級轉(zhuǎn)4-5級", "fl5":"4-5級轉(zhuǎn)3-4級", "fl6":"3-4級轉(zhuǎn)4-5級", //今日穿衣指數(shù); "index":"炎熱", "index_d":"天氣炎熱,建議著短衫、短裙、短褲、薄型T恤衫等清涼夏季服裝.", //48小時穿衣指數(shù) "index48":"炎熱", "index48_d":"天氣炎熱,建議著短衫、短裙、短褲、薄型T恤衫等清涼夏季服裝.", //紫外線及48小時紫外線 "index_uv":"中等", "index48_uv":"中等", //洗車指數(shù) "index_xc":"適宜", //旅游指數(shù) "index_tr":"較不宜", //舒適指數(shù) "index_co":"很不舒適", "st1":"36", "st2":"28", "st3":"36", "st4":"28", "st5":"34", "st6":"27", //晨練指數(shù) "index_cl":"較適宜", //晾曬指數(shù) "index_ls":"適宜", //過敏指數(shù) "index_ag":"不易發(fā)"}}
我們可以通過解析JSON,獲取相應(yīng)城市的天氣數(shù)據(jù).PHP實戰(zhàn)
weather() 函數(shù)如下:PHP實戰(zhàn)
private function weather($n){ include("weather_cityId.php"); $c_name=$weather_cityId[$n]; if(!empty($c_name)){ $json=file_get_contents("http://m.weather.com.cn/data/".$c_name.".html"); return json_decode($json); } else { return null; } }
這里include 了一個城市對應(yīng)關(guān)系文件 weather_cityId.php,格式如下:PHP實戰(zhàn)
<?php $weather_cityId = array("北京"=>"101010100","上海"=>"101020100","蘇州"=>"101190401"); ?>
根據(jù)傳入的城市名,得到城市代碼,如果不為空,則調(diào)用中國天氣網(wǎng)的API進行查詢,返回json格式的數(shù)據(jù),然后進行解析并返回數(shù)據(jù),如果為空,則返回null值.PHP實戰(zhàn)
五、組織回復(fù)消息形式PHP實戰(zhàn)
判斷返回數(shù)據(jù)是否為空,若為空,則 $contentStr = "抱歉,沒有查到\"".$str_key."\"的天氣信息!";PHP實戰(zhàn)
若返回數(shù)據(jù)不為空,則:
PHP實戰(zhàn)
$contentStr = "【".$data->weatherinfo->city."天氣預(yù)報】\n".$data->weatherinfo->date_y." ".$data->weatherinfo->fchh."時發(fā)布"."\n\n實時天氣\n".$data->weatherinfo->weather1." ".$data->weatherinfo->temp1." ".$data->weatherinfo->wind1."\n\n溫馨提示:".$data->weatherinfo->index_d."\n\n明天\n".$data->weatherinfo->weather2." ".$data->weatherinfo->temp2." ".$data->weatherinfo->wind2."\n\n后天\n".$data->weatherinfo->weather3." ".$data->weatherinfo->temp3." ".$data->weatherinfo->wind3;
說明:PHP實戰(zhàn)
$data->weatherinfo->city //獲取城市名,這里為蘇州PHP實戰(zhàn)
$data->weatherinfo->date_y //獲取日期,這里為2013年7月9日PHP實戰(zhàn)
$data->weatherinfo->fchh //數(shù)據(jù)發(fā)布時間PHP實戰(zhàn)
$data->weatherinfo->weather1 //實時天氣PHP實戰(zhàn)
$data->weatherinfo->temp1 //實時溫度PHP實戰(zhàn)
$data->weatherinfo->wind1 //實時風(fēng)向和風(fēng)速PHP實戰(zhàn)
$data->weatherinfo->index_d //穿衣指數(shù)PHP實戰(zhàn)
weather2, temp2, wind2 分別代表了明天的天氣,溫度和風(fēng)向風(fēng)速,其他的以此類推.PHP實戰(zhàn)
\n //表示換行PHP實戰(zhàn)
六、測試
PHP實戰(zhàn)
PHP實戰(zhàn)
七、完整代碼
PHP實戰(zhàn)
<?php /** * wechat php test */ //define your token define("TOKEN", "zhuojin"); $wechatObj = new wechatCallbackapiTest(); $wechatObj->responseMsg(); //$wechatObj->valid(); class wechatCallbackapiTest { /*public function valid() { $echoStr = $_GET["echostr"]; //valid signature , option if($this->checkSignature()){ echo $echoStr; exit; } }*/ public function responseMsg() { //get post data, May be due to the different environments $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; //extract post data if (!empty($postStr)){ $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $RX_TYPE = trim($postObj->MsgType); switch($RX_TYPE) { case "text": $resultStr = $this->handleText($postObj); break; case "event": $resultStr = $this->handleEvent($postObj); break; default: $resultStr = "Unknow msg type: ".$RX_TYPE; break; } echo $resultStr; }else { echo ""; exit; } } public function handleText($postObj) { $fromUsername = $postObj->FromUserName; $toUsername = $postObj->ToUserName; $keyword = trim($postObj->Content); $time = time(); $textTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <Content><![CDATA[%s]]></Content> <FuncFlag>0</FuncFlag> </xml>"; if(!empty( $keyword )) { $msgType = "text"; //天氣 $str = mb_substr($keyword,-2,2,"UTF-8"); $str_key = mb_substr($keyword,0,-2,"UTF-8"); if($str == '天氣' && !empty($str_key)){ $data = $this->weather($str_key); if(empty($data->weatherinfo)){ $contentStr = "抱歉,沒有查到\"".$str_key."\"的天氣信息!"; } else { $contentStr = "【".$data->weatherinfo->city."天氣預(yù)報】\n".$data->weatherinfo->date_y." ".$data->weatherinfo->fchh."時發(fā)布"."\n\n實時天氣\n".$data->weatherinfo->weather1." ".$data->weatherinfo->temp1." ".$data->weatherinfo->wind1."\n\n溫馨提示:".$data->weatherinfo->index_d."\n\n明天\n".$data->weatherinfo->weather2." ".$data->weatherinfo->temp2." ".$data->weatherinfo->wind2."\n\n后天\n".$data->weatherinfo->weather3." ".$data->weatherinfo->temp3." ".$data->weatherinfo->wind3; } } else { $contentStr = "感謝您關(guān)注【卓錦蘇州】"."\n"."微信號:zhuojinsz"."\n"."卓越錦繡,名城蘇州,我們?yōu)槟峁┨K州本地生活指南,蘇州相關(guān)信息查詢,做最好的蘇州微信平臺."."\n"."目前平臺功能如下:"."\n"."【1】 查天氣,如輸入:蘇州天氣"."\n"."【2】 查公交,如輸入:蘇州公交178"."\n"."【3】 翻譯,如輸入:翻譯I love you"."\n"."【4】 蘇州信息查詢,如輸入:蘇州觀前街"."\n"."更多內(nèi)容,敬請期待..."; } $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); echo $resultStr; }else{ echo "Input something..."; } } public function handleEvent($object) { $contentStr = ""; switch ($object->Event) { case "subscribe": $contentStr = "感謝您關(guān)注【卓錦蘇州】"."\n"."微信號:zhuojinsz"."\n"."卓越錦繡,名城蘇州,我們?yōu)槟峁┨K州本地生活指南,蘇州相關(guān)信息查詢,做最好的蘇州微信平臺."."\n"."目前平臺功能如下:"."\n"."【1】 查天氣,如輸入:蘇州天氣"."\n"."【2】 查公交,如輸入:蘇州公交178"."\n"."【3】 翻譯,如輸入:翻譯I love you"."\n"."【4】 蘇州信息查詢,如輸入:蘇州觀前街"."\n"."更多內(nèi)容,敬請期待..."; break; default : $contentStr = "Unknow Event: ".$object->Event; break; } $resultStr = $this->responseText($object, $contentStr); return $resultStr; } public function responseText($object, $content, $flag=0) { $textTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[%s]]></Content> <FuncFlag>%d</FuncFlag> </xml>"; $resultStr = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content, $flag); return $resultStr; } private function weather($n){ include("weather_cityId.php"); $c_name=$weather_cityId[$n]; if(!empty($c_name)){ $json=file_get_contents("http://m.weather.com.cn/data/".$c_name.".html"); return json_decode($json); } else { return null; } } private function checkSignature() { $signature = $_GET["signature"]; $timestamp = $_GET["timestamp"]; $nonce = $_GET["nonce"]; $token = TOKEN; $tmpArr = array($token, $timestamp, $nonce); sort($tmpArr); $tmpStr = implode( $tmpArr ); $tmpStr = sha1( $tmpStr ); if( $tmpStr == $signature ){ return true; }else{ return false; } } } ?>
關(guān)于城市對應(yīng)關(guān)系文件 weather_cityId.php,目前更新到了2564個城市,以后還會繼續(xù)增加,請到官方網(wǎng)盤http://pan.baidu.com/s/1gfcBX6N下載吧.
PHP實戰(zhàn)
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持維易PHP.PHP實戰(zhàn)
轉(zhuǎn)載請注明本頁網(wǎng)址:
http://www.snjht.com/jiaocheng/2512.html