微信公眾平臺(tái)開(kāi)發(fā)(五) 天氣預(yù)報(bào)功能開(kāi)發(fā)
一、簡(jiǎn)介
前面幾篇文章對(duì)微信公眾平臺(tái)的開(kāi)通及簡(jiǎn)單使用做了簡(jiǎn)單的介紹,但都沒(méi)有涉及到實(shí)際使用中的問(wèn)題,例如天氣查詢,公交查詢,快遞查詢等。接下來(lái)的幾篇文章將對(duì)實(shí)際生活中會(huì)經(jīng)常使用到的一些功能進(jìn)行開(kāi)發(fā)講解,以供讀者參考。
這一篇文章將對(duì)大家每天都會(huì)關(guān)心的天氣查詢進(jìn)行開(kāi)發(fā),例如,用戶發(fā)送消息 “蘇州天氣”,則會(huì)返回蘇州實(shí)時(shí)天氣狀況,以及未來(lái)兩天甚至未來(lái)五天的天氣狀況。
二、思路分析
首先要對(duì)用戶發(fā)送過(guò)來(lái)的消息進(jìn)行判斷,判斷消息里是否含有“天氣”關(guān)鍵字,如果含有,則需要繼續(xù)提取地區(qū)信息,然后再通過(guò)中國(guó)天氣網(wǎng)(http://www.weather.com.cn)提供的開(kāi)放API進(jìn)行相關(guān)地區(qū)天氣查詢。
三、關(guān)鍵字判斷與地區(qū)讀取
用戶發(fā)送過(guò)來(lái)查詢天氣的消息的格式是固定好的,即 “地區(qū)+天氣”,所以首先截取后兩個(gè)字,判斷是否為 “天氣” 關(guān)鍵字。
使用php函數(shù) mb_substr() 截取,關(guān)于該函數(shù)的用法:
mb_substr — 獲取字符串的部分
string mb_substr ( string $str , int $start [, int $length [, string $encoding ]] )
說(shuō)明:根據(jù)字符數(shù)執(zhí)行一個(gè)多字節(jié)安全的 substr() 操作。 位置是從 str 的開(kāi)始位置進(jìn)行計(jì)數(shù)。 第一個(gè)字符的位置是 0。第二個(gè)字符的位置是 1,以此類推。
參數(shù):
str
從該 string 中提取子字符串。
start
str 中要使用的第一個(gè)字符的位置。
正數(shù) -> 從字符串開(kāi)頭指定位置開(kāi)始;
負(fù)數(shù) -> 從字符串結(jié)尾指定位置開(kāi)始;
length
str 中要使用的最大字符數(shù)。
正數(shù) -> 從 start 處開(kāi)始最多包括 length 個(gè)字符;
負(fù)數(shù) -> string 末尾處的 length 個(gè)字符將會(huì)被漏掉(若 start 是負(fù)數(shù)則從字符串開(kāi)頭算起)。
encoding
encoding 參數(shù)為字符編碼。如果省略,則使用內(nèi)部字符編碼。
返回值:
mb_substr() 函數(shù)根據(jù) start 和 length 參數(shù)返回 str 中指定的部分。
$str = mb_substr($keyword,-2,2,"UTF-8");
從消息的結(jié)尾數(shù)第二個(gè)字符開(kāi)始截取,截取兩個(gè)字符,然后加以判斷是否為 “天氣” 關(guān)鍵字。
下面進(jìn)行地區(qū)提取,還是使用 mb_substr() 函數(shù)。
$str_key = mb_substr($keyword,0,-2,"UTF-8");
從消息的開(kāi)頭開(kāi)始,截掉末尾的兩個(gè)字符(天氣),既得地區(qū)關(guān)鍵字。
然后進(jìn)行判斷,繼而調(diào)用函數(shù)查詢天氣數(shù)據(jù)。
if($str == '天氣' && !empty($str_key))
{
//調(diào)用函數(shù)查詢天氣數(shù)據(jù)
}
四、調(diào)用 weather() 函數(shù)查詢
我們這里調(diào)用的是中國(guó)國(guó)家氣象局提供的天氣預(yù)報(bào)API接口,接口地址:http://m.weather.com.cn/data/101190401.html
URL中的數(shù)字指代城市的編號(hào)101190401(蘇州),其他城市對(duì)應(yīng)關(guān)系將在下面提供。
該接口返回信息比較全面,也是以json格式提供,格式如下:
{"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":"晴轉(zhuǎn)多云",
"weather2":"晴轉(zhuǎn)多云",
"weather3":"晴轉(zhuǎn)多云",
"weather4":"多云",
"weather5":"雷陣雨轉(zhuǎn)中雨",
"weather6":"雷陣雨轉(zhuǎn)多云",
//天氣描述圖片序號(hào)
"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級(jí)",
"wind2":"西南風(fēng)3-4級(jí)",
"wind3":"東南風(fēng)3-4級(jí)",
"wind4":"東南風(fēng)3-4級(jí)轉(zhuǎn)4-5級(jí)",
"wind5":"東南風(fēng)4-5級(jí)轉(zhuǎn)西南風(fēng)3-4級(jí)",
"wind6":"西南風(fēng)3-4級(jí)轉(zhuǎn)4-5級(jí)",
//風(fēng)速級(jí)別描述
"fx1":"西南風(fēng)",
"fx2":"西南風(fēng)",
"fl1":"3-4級(jí)",
"fl2":"3-4級(jí)",
"fl3":"3-4級(jí)",
"fl4":"3-4級(jí)轉(zhuǎn)4-5級(jí)",
"fl5":"4-5級(jí)轉(zhuǎn)3-4級(jí)",
"fl6":"3-4級(jí)轉(zhuǎn)4-5級(jí)",
//今日穿衣指數(shù);
"index":"炎熱",
"index_d":"天氣炎熱,建議著短衫、短裙、短褲、薄型T恤衫等清涼夏季服裝。",
//48小時(shí)穿衣指數(shù)
"index48":"炎熱",
"index48_d":"天氣炎熱,建議著短衫、短裙、短褲、薄型T恤衫等清涼夏季服裝。",
//紫外線及48小時(shí)紫外線
"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":"適宜",
//過(guò)敏指數(shù)
"index_ag":"不易發(fā)"}}
我們可以通過(guò)解析JSON,獲取相應(yīng)城市的天氣數(shù)據(jù)。
weather() 函數(shù)如下:
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 了一個(gè)城市對(duì)應(yīng)關(guān)系文件 weather_cityId.php,格式如下:
<?php
$weather_cityId = array("北京"=>"101010100","上海"=>"101020100","蘇州"=>"101190401");
?>
根據(jù)傳入的城市名,得到城市代碼,如果不為空,則調(diào)用中國(guó)天氣網(wǎng)的API進(jìn)行查詢,返回json格式的數(shù)據(jù),然后進(jìn)行解析并返回?cái)?shù)據(jù),如果為空,則返回null值。
五、組織回復(fù)消息形式
判斷返回?cái)?shù)據(jù)是否為空,若為空,則 $contentStr = "抱歉,沒(méi)有查到\"".$str_key."\"的天氣信息!";
若返回?cái)?shù)據(jù)不為空,則:
$contentStr = "【".$data->weatherinfo->city."天氣預(yù)報(bào)】\n".$data->weatherinfo->date_y." ".$data->weatherinfo->fchh."時(shí)發(fā)布"."\n\n實(shí)時(shí)天氣\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;
說(shuō)明:
$data->weatherinfo->city //獲取城市名,這里為蘇州
$data->weatherinfo->date_y //獲取日期,這里為2013年7月9日
$data->weatherinfo->fchh //數(shù)據(jù)發(fā)布時(shí)間
$data->weatherinfo->weather1 //實(shí)時(shí)天氣
$data->weatherinfo->temp1 //實(shí)時(shí)溫度
$data->weatherinfo->wind1 //實(shí)時(shí)風(fēng)向和風(fēng)速
$data->weatherinfo->index_d //穿衣指數(shù)
weather2, temp2, wind2 分別代表了明天的天氣,溫度和風(fēng)向風(fēng)速,其他的以此類推。
\n //表示換行
六、測(cè)試

七、完整代碼
<?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 = "抱歉,沒(méi)有查到\"".$str_key."\"的天氣信息!";
} else {
$contentStr = "【".$data->weatherinfo->city."天氣預(yù)報(bào)】\n".$data->weatherinfo->date_y." ".$data->weatherinfo->fchh."時(shí)發(fā)布"."\n\n實(shí)時(shí)天氣\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"."微信號(hào):zhuojinsz"."\n"."卓越錦繡,名城蘇州,我們?yōu)槟峁┨K州本地生活指南,蘇州相關(guān)信息查詢,做最好的蘇州微信平臺(tái)。"."\n"."目前平臺(tái)功能如下:"."\n"."【1】 查天氣,如輸入:蘇州天氣"."\n"."【2】 查公交,如輸入:蘇州公交178"."\n"."【3】 翻譯,如輸入:翻譯I love you"."\n"."【4】 蘇州信息查詢,如輸入:蘇州觀前街"."\n"."更多內(nèi)容,敬請(qǐng)期待...";
}
$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"."微信號(hào):zhuojinsz"."\n"."卓越錦繡,名城蘇州,我們?yōu)槟峁┨K州本地生活指南,蘇州相關(guān)信息查詢,做最好的蘇州微信平臺(tái)。"."\n"."目前平臺(tái)功能如下:"."\n"."【1】 查天氣,如輸入:蘇州天氣"."\n"."【2】 查公交,如輸入:蘇州公交178"."\n"."【3】 翻譯,如輸入:翻譯I love you"."\n"."【4】 蘇州信息查詢,如輸入:蘇州觀前街"."\n"."更多內(nèi)容,敬請(qǐng)期待...";
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)于城市對(duì)應(yīng)關(guān)系文件 weather_cityId.php,目前更新到了2564個(gè)城市,以后還會(huì)繼續(xù)增加,請(qǐng)到官方網(wǎng)盤(pán)http://pan.baidu.com/s/1gfcBX6N下載吧。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 使用正則表達(dá)式替換報(bào)表名稱中的特殊字符(推薦)
- Web打印解決方案之普通報(bào)表打印功能
- 解決VS2015中沒(méi)有報(bào)表項(xiàng)(ReportViewer)的方法
- 在ASP.NET 2.0中操作數(shù)據(jù)之十:使用 GridView和DetailView實(shí)現(xiàn)的主/從報(bào)表
- Java FineReport報(bào)表工具導(dǎo)出EXCEL的四種方式
- 原生js實(shí)現(xiàn)查詢天氣小應(yīng)用
- Java 調(diào)用天氣Webservice詳解及實(shí)例代碼
- php微信開(kāi)發(fā)之百度天氣預(yù)報(bào)
- Jquery獲取當(dāng)前城市的天氣信息
- 簡(jiǎn)單三步實(shí)現(xiàn)報(bào)表頁(yè)面集成天氣
相關(guān)文章
深入學(xué)習(xí)微信網(wǎng)址鏈接解封的防封原理visit_type
這篇文章主要介紹了深入學(xué)習(xí)微信網(wǎng)址鏈接解封的防封原理visit_type,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08
laravel 使用事件系統(tǒng)統(tǒng)計(jì)瀏覽量的實(shí)現(xiàn)
今天小編就為大家分享一篇laravel 使用事件系統(tǒng)統(tǒng)計(jì)瀏覽量的實(shí)現(xiàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-10-10
php利用array_search與array_column實(shí)現(xiàn)二維數(shù)組查找
這篇文章主要介紹了php利用array_search與array_column實(shí)現(xiàn)二維數(shù)組查找,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
PHP--用萬(wàn)網(wǎng)的接口實(shí)現(xiàn)域名查詢功能
PHP用萬(wàn)網(wǎng)的接口實(shí)現(xiàn)域名查詢功能,需要的朋友可以了解下2012-12-12
YII2框架中使用yii.js實(shí)現(xiàn)的post請(qǐng)求
本文給大家介紹的是簡(jiǎn)單分析下用yii2的yii\helpers\Html類和yii.js實(shí)現(xiàn)的post請(qǐng)求的方法,非常的簡(jiǎn)單,有需要的小伙伴可以參考下2017-04-04
php指定函數(shù)參數(shù)默認(rèn)值示例代碼
在php編程中,為自定義函數(shù)設(shè)定默認(rèn)值,當(dāng)用戶調(diào)用該函數(shù)時(shí),如果不給參數(shù)指定值,參數(shù)會(huì)用默認(rèn)值頂替,下面看例子2013-12-12

