<span
<span 一、簡介
前面幾篇文章對公眾平台的開通及簡單使用做了簡單的介紹,但都沒有涉及到實際使用中的問題,例如天氣查詢,公交查詢,快遞查詢等。接下來的幾篇文章將對實際生活中會經常使用到的一些功能進行開發講解,以供讀者參考。
這一篇文章將對大家每天都會關心的天氣查詢進行開發,例如,使用者發送訊息 “蘇州天氣”,則會返回蘇州即時天氣狀況,以及未來兩天甚至未來五天的天氣狀況。
<span <span 二、思路分析
首先要對使用者發送過來的訊息進行判斷,判斷訊息裡是否含有“天氣”關鍵字,如果含有,則需要繼續提取地區資訊,然後再通過中國天氣網(http://www.weather.com.cn)提供的開放API進行相關地區天氣查詢。
<span <span 三、關鍵字判斷與地區讀取
使用者發送過來查詢天氣的訊息的格式是固定好的,即 “地區+天氣”,所以首先截取後兩個字,判斷是否為 “天氣” 關鍵字。
使用php函數 mb_substr() 截取,關於該函數的用法:
<span mb_substr — 擷取字串的部分 <span string mb_substr ( <span string <span $str , int <span $start [, int <span $length [, <span string <span $encoding<span ]] )根據字元數執行一個多位元組安全的 <span substr() 操作。 位置是從 str 的開始位置進行計數。 第一個字元的位置是 0。第二個字元的位置是 1<span ,以此類推。str從該 <span string<span 中提取子字串。startstr 中要使用的第一個字元的位置。正數 -> 從字串開頭指定位置開始;負數 -> 從字串結尾指定位置開始;lengthstr 中要使用的最大字元數。正數 -> 從 start <span 處開始最多包括 length 個字元;負數 -> string 末尾處的 length 個字元將會被漏掉(若 start 是負數則從字串開頭算起)。encodingencoding 參數為字元編碼。如果省略,則使用內部字元編碼。mb_substr() 函數根據 start 和 length 參數返回 str 中指定的部分。
$str = mb_substr($keyword,-2,2,"UTF-8");
從訊息的結尾數第二個字元開始截取,截取兩個字元,然後加以判斷是否為 “天氣” 關鍵字。
下面進行地區提取,還是使用 mb_substr() 函數。
$str_key = mb_substr($keyword,0,-2,"UTF-8");
從訊息的開頭開始,截掉末尾的兩個字元(天氣),既得地區關鍵字。
然後進行判斷,繼而調用函數查詢天氣資料。
<span if(<span $str == '天氣' && !empty($str_key)<span ){ <span //調用函數查詢天氣資料}
<span <span 四、調用 weather() 函數查詢
我們這裡調用的是中國國家氣象局提供的天氣預報API介面,介面地址:http://m.weather.com.cn/data/101190401.html
URL中的數字指代城市的編號101190401(蘇州),其他城市對應關係將在下面提供。
該介面返回資訊比較全面,也是以json格式提供,格式如下:
<span {"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":"86℉~98.6℉","tempF2":"86℉~98.6℉","tempF3":"84.2℉~95℉","tempF4":"80.6℉~91.4℉","tempF5":"80.6℉~87.8℉","tempF6":"80.6℉~95℉",//天氣描述;"weather1":"晴轉多雲","weather2":"晴轉多雲","weather3":"晴轉多雲","weather4":"多雲","weather5":"雷陣雨轉中雨","weather6":"雷陣雨轉多雲",//天氣描述圖片序號"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":"多雲",//風速描述"wind1":"西南風3-4級","wind2":"西南風3-4級","wind3":"東南風3-4級","wind4":"東南風3-4級轉4-5級","wind5":"東南風4-5級轉西南風3-4級","wind6":"西南風3-4級轉4-5級",//風速層級描述"fx1":"西南風","fx2":"西南風","fl1":"3-4級","fl2":"3-4級","fl3":"3-4級","fl4":"3-4級轉4-5級","fl5":"4-5級轉3-4級","fl6":"3-4級轉4-5級",//今日穿衣指數;"index":"炎熱","index_d":"天氣炎熱,建議著短衫、短裙、短褲、薄型T恤衫等清涼夏季服裝。",//48小時穿衣指數"index48":"炎熱","index48_d":"天氣炎熱,建議著短衫、短裙、短褲、薄型T恤衫等清涼夏季服裝。",//紫外線及48小時紫外線"index_uv":"中等","index48_uv":"中等",//洗車指數"index_xc":"適宜",//旅遊指數"index_tr":"較不宜",//舒適指數"index_co":"很不舒適","st1":"36","st2":"28","st3":"36","st4":"28","st5":"34","st6":"27",//晨練指數"index_cl":"較適宜",//晾曬指數"index_ls":"適宜",//過敏指數"index_ag":"不易發"}}
<span 我們可以通過<span 解析JSON,擷取相應城市的天氣資料。
weather() 函數如下:
<span private <span function weather(<span $n<span ){<span include("weather_cityId.php"<span );<span $c_name=<span $weather_cityId[<span $n<span ];<span if(!<span empty(<span $c_name<span )){<span $json=<span file_get_contents("http://m.weather.com.cn/data/".<span $c_name.".html"<span );<span return json_decode(<span $json<span ); } <span else<span {<span return <span null<span ; }}
這裡include 了一個城市對應關係檔案 weather_cityId.php,格式如下:
<?<span php<span $weather_cityId = <span array("北京"=>"101010100","上海"=>"101020100","蘇州"=>"101190401"<span );?>
根據傳入的城市名,得到城市代碼,如果不為空白,則調用中國天氣網的API進行查詢,返回json格式的資料,然後進行解析並返回資料,如果為空白,則返回null值。
<span <span 五、組織回複訊息形式
判斷返回資料是否為空白,若為空白,則 $contentStr = "抱歉,沒有查到\"".$str_key."\"的天氣資訊!";
若返回資料不為空白,則:
$contentStr = "【".$data->weatherinfo->city."天氣預報】\n".$data->weatherinfo->date_y." ".$data->weatherinfo->fchh."時發布"."\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;
說明:
weatherinfo->city //擷取城市名,這裡為蘇州
weatherinfo->date_y //擷取日期,這裡為2013年7月9日
weatherinfo->fchh //資料發布時間
weatherinfo->weather1 //即時天氣
weatherinfo->temp1 //即時溫度
weatherinfo->wind1 //即時風向和風速
weatherinfo->index_d //穿衣指數
<p weather2, temp2, wind2 分別代表了明天的天氣,溫度和風向風速,其他的以此類推。<p \n //表示換行
<span <span 六、測試
<span <span 七、完整代碼
<?<span php<span /*<span * * wechat php test <span */<span //<span define your token<span define("TOKEN", "zhuojin"<span );<span $wechatObj = <span new<span wechatCallbackapiTest();<span $wechatObj-><span responseMsg();<span //valid();<span class<span wechatCallbackapiTest{<span /*checkSignature()){ echo $echoStr; exit; } }<span */<span public <span function<span responseMsg() {<span //<span get post data, May be due to the different environments<span $postStr = <span $GLOBALS["HTTP_RAW_POST_DATA"<span ]; <span //<span extract post data<span if (!<span empty(<span $postStr<span )){ <span $postObj = <span simplexml_load_string(<span $postStr, 'SimpleXMLElement', <span LIBXML_NOCDATA);<span $RX_TYPE = <span trim(<span $postObj-><span MsgType); <span switch(<span $RX_TYPE<span ) {<span case "text":<span $resultStr = <span $this->handleText(<span $postObj<span ); <span break<span ;<span case "event":<span $resultStr = <span $this->handleEvent( <span $postObj<span );<span break<span ;<span default:<span $resultStr = "Unknow msg type: ". <span $RX_TYPE<span ;<span break<span ; }<span echo <span $resultStr<span ; }<span else<span {<span echo ""<span ;<span exit<span ; } }<span public <span function handleText(<span $postObj<span ) {<span $fromUsername = <span $postObj-><span FromUserName;<span $toUsername = <span $postObj-> <span ToUserName;<span $keyword = <span trim(<span $postObj-><span Content); <span $time = <span time<span ();<span $textTpl = "<span %s0"<span ; <span if(!<span empty( <span $keyword<span )) {<span $msgType = "text"<span ;<span //<span 天氣<span $str = mb_substr(<span $keyword,-2,2,"UTF-8" <span );<span $str_key = mb_substr(<span $keyword,0,-2,"UTF-8"<span );<span if(<span $str == '天氣' && ! <span empty(<span $str_key<span )){<span $data = <span $this->weather(<span $str_key<span ); <span if(<span empty(<span $data-><span weatherinfo)){<span $contentStr = "抱歉,沒有查到\"". <span $str_key."\"的天氣資訊!"<span ; } <span else<span {<span $contentStr = "【".<span $data->weatherinfo->city."天氣預報】\n". <span $data->weatherinfo->date_y." ".<span $data->weatherinfo->fchh."時發布"."\n\n即時天氣\n". <span $data->weatherinfo->weather1." ".<span $data->weatherinfo->temp1." ". <span $data->weatherinfo->wind1."\n\n溫馨提示:".<span $data->weatherinfo->index_d."\n\n明天\n". <span $data->weatherinfo->weather2." ".<span $data->weatherinfo->temp2." ". <span $data->weatherinfo->wind2."\n\n後天\n".<span $data->weatherinfo->weather3." ". <span $data->weatherinfo->temp3." ".<span $data->weatherinfo-><span wind3; } } <span else<span {<span $contentStr = "感謝您關注【卓錦蘇州】"."\n"."號:zhuojinsz"."\n". "卓越錦繡,名城蘇州,我們為您提供蘇州本地生活指南,蘇州相關資訊查詢,做最好的蘇州平台。"."\n". "目前平台功能如下:"."\n"."【1】 查天氣,如輸入:蘇州天氣"."\n"." 【2】 查公交,如輸入:蘇州公交178"."\n"."【3】 翻譯,如輸入:翻譯I love you"."\n"." 【4】 蘇州資訊查詢,如輸入:蘇州觀前街"."\n"."更多內容,敬請期待..."<span ; }<span $resultStr = <span sprintf(<span $textTpl, <span $fromUsername, <span $toUsername, <span $time, <span $msgType, <span $contentStr<span ); <span echo <span $resultStr<span ; }<span else<span {<span echo "Input something..."<span ; } }<span public <span function handleEvent(<span $object<span ) {<span $contentStr = ""<span ;<span switch (<span $object-><span Event) {<span case "subscribe":<span $contentStr = "感謝您關注【卓錦蘇州】"."\n"."號:zhuojinsz"."\n"." 卓越錦繡,名城蘇州,我們為您提供蘇州本地生活指南,蘇州相關資訊查詢,做最好的蘇州平台。"."\n"." 目前平台功能如下:"."\n"."【1】 查天氣,如輸入:蘇州天氣"."\n"." 【2】 查公交,如輸入:蘇州公交178"."\n"."【3】 翻譯,如輸入:翻譯I love you"."\n"." 【4】 蘇州資訊查詢,如輸入:蘇州觀前街"."\n"."更多內容,敬請期待..."<span ;<span break<span ; <span default :<span $contentStr = "Unknow Event: ".<span $object-><span Event;<span break<span ; }<span $resultStr = <span $this->responseText(<span $object, <span $contentStr<span );<span return <span $resultStr<span ; } <span public <span function responseText(<span $object, <span $content, <span $flag=0<span ) {<span $textTpl = "<span %s%d"<span ;<span $resultStr = <span sprintf(<span $textTpl, <span $object->FromUserName, <span $object->ToUserName, <span time(), <span $content, <span $flag<span ); <span return <span $resultStr<span ; }<span private <span function weather(<span $n<span ){<span include("weather_cityId.php"<span ); <span $c_name=<span $weather_cityId[<span $n<span ];<span if(!<span empty(<span $c_name<span )){ <span $json=<span file_get_contents("http://m.weather.com.cn/data/".<span $c_name.".html"<span ); <span return json_decode(<span $json<span ); } <span else<span {<span return <span null<span ; } }<span private <span function<span checkSignature() {<span $signature = <span $_GET["signature"<span ];<span $timestamp = <span $_GET["timestamp"<span ]; <span $nonce = <span $_GET["nonce"<span ]; <span $token =<span TOKEN;<span $tmpArr = <span array(<span $token, <span $timestamp, <span $nonce<span );<span sort(<span $tmpArr<span );<span $tmpStr = <span implode( <span $tmpArr<span );<span $tmpStr = <span sha1( <span $tmpStr<span ); <span if( <span $tmpStr == <span $signature<span ){<span return <span true<span ; }<span else<span {<span return <span false<span ; } }}?>
關於城市對應關係檔案 weather_cityId.php,目前更新到了400多個城市,以後還會繼續增加,請到QQ群213260412下載吧。
<span <span 八、關注
請關注<span <span 卓錦蘇州 公眾帳號,<span 卓錦蘇州 基於SAE 平台開發,針對於主流的功能進行開發測試。
您可以關注 卓錦蘇州 公眾帳號進行功能測試,以及擷取新的應用開發。
1. 登入用戶端,朋友們 -> 添加朋友 -> 搜號碼 -> zhuojinsz,尋找並關注。
2. 掃描二維碼:
<span 卓錦蘇州 功能列表。
<hr <span <strong David Camp<ul <li 技術交流,請加QQ群:<p 開發分享Ⅰ群:168236814<p 開發分享Ⅱ群:213260412<ul <li 業務合作,請聯絡作者QQ:562866602<li 我的號:mchina_tang<li 給我寫信:mchina_tang@qq.com<p <span <strong <strong <strong 我們永遠相信,分享是一種美德 | We Believe, Great People Share Knowledge...
以上就是公眾平台開發(五) 天氣預報功能開發_PHP教程的內容,更多相關內容請關注topic.alibabacloud.com(www.php.cn)!
http://www.bkjia.com/PHPjc/440409.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/440409.htmlTechArticle前面幾篇文章對公眾平台的開通及簡單使用做了簡單的介紹,但都沒有涉及到實際使用中的問題,例如天氣查詢,公交查詢,快遞查詢...