微信公众平台开发(五)天气预报功能开发
一.导言
之前的文章简单介绍了微信公众平台的开通和简单使用,但都没有涉及到实际使用中的问题,比如天气查询、公交查询、快递查询等等。下面的文章将开发和解释一些在现实生活中经常使用的功能,供读者参考。
本文将开发大家每天都关心的天气查询。比如用户发消息“苏州天气”,就会返回苏州的实时天气情况和未来两天甚至五天的天气情况。
二、思维分析
首先要判断用户发送的消息中是否包含关键词“天气”。如果是,需要继续提取区域信息,然后通过中国天气网(http://www.weather.com.cn)提供的开放API查询相关区域的天气。
三、关键词判断和区域阅读
用户发来查询天气的消息格式是固定的,即“地区天气”,所以先截取最后两个字来判断是否是“天气”关键词。
使用php函数mb_substr()进行拦截。关于此功能的使用:
MB _ substr-获取字符串的一部分
String MB _ substr (string $ str,int $ start [,int $ length [,string $ encoding]]):根据字符数执行多字节安全substr()操作。该位置从字符串的起始位置开始计数。第一个字符的位置是0。第二个字符的位置是1,依此类推。
参数:
潜艇用热中子反应堆(submarine thermal reactor的缩写)
从字符串中提取子字符串。
开始
字符串中要使用的第一个字符的位置。
正数-从字符串开头的指定位置开始;
负数-从字符串末尾的指定位置开始;
长度
字符串中使用的最大字符数。
正数-从开始的最大字符长度;
字符串末尾的负数长度字符将会丢失(如果start为负数,将从字符串的开头开始计算)。
编码
编码参数是字符编码。如果省略,则使用内部字符编码。
返回值:
mb_substr()函数根据开始和长度参数返回字符串的指定部分。
$ str=MB _ substr($关键字,-2,2,' UTF-8 ');从消息末尾的第二个字符开始截取,截取两个字符,然后判断是否是“天气”关键词。
接下来,使用mb_substr()函数进行区域提取。
$str_key=mb_substr($keyword,0,-2,' UTF-8 ');
从消息开始,剪掉最后两个字符(天气),得到区域关键词。
然后判断,再调用函数查询天气数据。
If($str==' weather '!空($str_key)){ //调用函数查询天气数据}第四,调用weather()函数查询
我们这里所说的是中国国家气象局提供的天气预报API接口。接口地址是http://m.weather.com.cn/data/101190401.html
网址中的数字指的是城市编号101190401(苏州),其他城市的对应关系将在下面提供。
这个接口返回的信息很全面,也是以json格式提供的。格式如下:
{'weatherinfo':{//基本信息;City': '苏州',' city _ en' : '苏州',' date _ y ' : ' July年7月9日',' date' : ',' week' : '星期二',' fchh ' 3336 '//摄氏度温度' Temp1' :' 30 ~ 37',' Temp2' :' 30 ~ 37',' Tem3' :' 29 ~ 35',天气1': '晴到多云','天气2': '晴到多云','天气3': '晴到多云','天气4': '多云','天气5': '雷雨到中雨','天气6' /天气描述图片编号。img1' :' 0 ',' img2' :' 1 ',' img3' :' 0 ',' img4' :' 1 ',' img5' :' 0 ',' img6' 33366。Img10' :' 8 ',' img11' :' 4 ',' img12' :' 1 ',//图片名称;Img _ single' :' 1 ',' img _ title1' : '晴',' img_title2': '多云',' img_title3': '晴',' img_title4': '多云',' Img_title7': '多云',' img_title9': '雷雨',' img_title10': '中雨Index':' Hot ',' index_d': '天气炎热,建议穿短袖、短裙、短裤、薄t恤等凉爽的夏装。//48小时穿衣指数' index48': '热',' index48_d': '天气炎热,建议穿短袖、短裙、短裤、薄t恤等凉爽的夏装。//紫外线和48小时紫外线' index_uv': '中等',index48 _ uv ' : '中等',//洗车指数' index_xc': '适合',//旅游指数' index_tr': '不太适合',ST1' :' 36 ',' ST2' :' 28 ',' ST3' :' 36 ',' ST4 ' : ' 28 ',' ST5 ' 336 '
天气()功能如下:
私人函数weather($ n){ include(' weather _ city id . PHP ');$ c _ name=$ weather _ city id[$ n];if(!empty($ c _ name)){ $ JSON=file _ get _ contents(' http://m . weather.com.cn/data/')。$c_name。html ');返回JSON _ decode($ JSON);} else { return null}}这里包含一个城市对应文件weather_cityId.php,格式如下:
?PHP $ weather _ city id=array(' Beijing '=' 101010100 ',' Shanghai'='101020100 ','苏州'=' 101190401 ');根据入局城市名称,获取城市代码。如果不是空的,调用中国天气网的API查询,返回json格式的数据,然后解析返回数据。如果为空,则返回空值。
动词(verb的缩写)组织回复信息的形式
确定返回的数据是否为空。如果为空,$contentStr='抱歉,没有''的天气信息。$ str _ key。'!';
如果返回的数据不是空的,那么:
$ content str=“[”。$数据-天气信息-城市。天气预报] \ n '。$ data-weatherinfo-date _ y . ' '。$ data-weatherinfo-fchh。发布。\ N \ N实时天气。$ data-weatherinfo-weather1。''.$ data-weatherinfo-temp1。''.$ data-weatherinfo-wind1。\ n \ nTips:'。$ data-weather info-index _ d . ' \ n \ N明天\ N '。$ data-weatherinfo-weather2。''.$ data-weatherinfo-temp2。''.$ data-weatherinfo-wind2。\ n \ n后天\ n。$ data-weatherinfo-weather3。描述:
$data-weatherinfo-city//获取城市名称,这里是苏州
$data-weatherinfo-date_y//获取日期,这里是2013年7月9日
$data-weatherinfo-fchh//数据发布时间
$data-weatherinfo-weather1//实时天气
$data-weatherinfo-temp1//实时温度
$data-weatherinfo-wind1//实时风向和风速
$data-weatherinfo-index_d//穿衣指数
Weather2、temp2、wind2代表明天的天气、温度和风向等等。
\n//表示包装
不及物动词测试
七、完整代码
?php/** *微信PHP测试*///定义你的tokendefine('TOKEN ','金卓');$微信bj=新微信callbackapitet();$微信对象响应消息();//$微信对象-valid();班级微信callbackapist {/* public function valid(){ $ echoStr=$ _ GET[' echoStr '];//有效签名,选项if($ this-CheckSignature()){ echo $ echoStr;退出;} } */public function response msg(){//获取邮政数据,可能是由于环境不同$ PostStr=$ GLOBALS[' HTTP _ RAW _ POST _ DATA '];//提取帖子数据if(!empty($ PostStr)){ $ PostObj=simple XML _ load _ string($ PostStr,' SimpleXMLElement ',LIBXML _ NocDATa);$ RX _ TYPE=trim($ PostObj-MSgtype);switch($ RX _ TYPE){ case ' text ' : $ resultStr=$ this-handleText($ PostObj);打破;case ' event ' : $ resultStr=$ this-handleEvent($ PostObj);打破;默认值: $ result str=' Unknow msg type : ' .$ RX _ TYPE打破;} echo $ resultStr } else { echo退出;} }公共函数handleText($ PostObj){ $ FrOm USERNAME=$ PostObj-FrOm USERNAME;$ touser name=$ PostObj-touser name;$关键字=trim($ PostObj-Content);$ time=time();$textTpl='xml ToUserName![CData[% s]]/从用户名使用用户名![CData[% s]]/来自用户名创建时间% s/创建时间MsgType![CDATA[%s]]/MsgType内容![CData[% s]]/Content func flag 0/func flag/XML ';if(!空($关键字)){ $ msgType=' text//天气$ str=MB _ substr($关键字,-2,2,' UTF-8 ';$str_key=mb_substr($keyword,0,-2,' UTF-8 ');if($str=='天气!空($ str _ key)){ $ data=$ this-weather($ str _ key);如果(空($ data-weather info)){ $ ContentStr='抱歉,没有查到\''.$str_key .\'的天气信息!} else { $contentStr='【' .$数据-天气信息-城市。'天气预报】\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-天气信息-wind 3;} } else { $contentStr='感谢您关注【卓锦苏州】'.\n .微信号:卓锦斯\n .'卓越锦绣,名城苏州,我们为您提供苏州本地生活指南,苏州相关信息查询,做最好的苏州微信平台。'.\n .'目前平台功能如下:'.\n .'【1】 查天气,如输入:苏州天气“\n,”【2】 查公交,如输入:苏州公交178'.\n .【3】 翻译,如输入:翻译我爱你\n .'【4】 苏州信息查询,如输入:苏州观前街“\n,”更多内容,敬请期待.} $resultStr=sprintf($textTpl,$fromUsername,$ toUsername,$time,$msgType,$ content str);回应$ resultStr }其他{回应'输入某物.} }公共函数handleEvent($ object){ $ ContentStr=' ';switch($ object-Event){ case ' subscribe ' : $ contentStr='感谢您关注【卓锦苏州】'.\n .微信号:卓锦斯\n .'卓越锦绣,名城苏州,我们为您提供苏州本地生活指南,苏州相关信息查询,做最好的苏州微信平台。
'.'\n " "目前平台功能如下:'.'\n " "【1】 查天气,如输入:苏州天气'.'\n " "【2】 查公交,如输入:苏州公交178'.'\n " "【3】 翻译,如输入:翻译我爱你\n " "【4】 苏州信息查询,如输入:苏州观前街'.'\n " "更多内容,敬请期待.打破;默认值: $ ContentStr='未知事件: ' .$对象-事件;打破;} $ resultStr=$ this-responseText($ object,$ content str);返回$ resultStr}公共函数responseText($ object,$content,$ flag=0){ $ textTpl=' XML Tousername![CData[% s]]/从用户名使用用户名![CData[% s]]/来自用户名创建时间% s/创建时间MsgType![CData[文本]]/MsgType内容![CData[% s]]/Content func flag % d/func flag/XML ';$resultStr=sprintf($textTpl,$object-FromUserName,$object-ToUserName,time(),$content,$ flag);返回$ resultStr}私人函数weather($ n){ include(' weather _ city id。PHP’);$ c _ name=$ weather _ city id[$ n];if(!empty($ c _ name)){ $ JSON=file _ get _ contents(' http://m。天气。com。cn/data/').$ c _ name . html ');返回JSON _ decode($ JSON);} else {返回null} }私有函数check signature(){ $ signature=$ _ GET[' signature '];$时间戳=$_GET['时间戳'];$ nonce=$ _ GET[' nonce '];$ TOKEN=TOKEN $ TMParr=array($ TOKEN,$timestamp,$ nonce);sort($ TMParr);$tmpStr=内爆($ TMParr);$ TMPstr=sha1($ TMPstr);if($ TMPstr==$ signature){ 0返回true}否则{返回false} }?关于城市对应关系文件weather_cityId.php,目前更新到了2564个城市,以后还会继续增加,请到官方网盘http://pan.baidu.com/s/1gfcBX6N下载吧。
以上就是本文的全部内容。希望对大家的学习有帮助,支持我们。
版权声明:微信公众平台开发(五)天气预报功能开发是由宝哥软件园云端程序自动收集整理而来。如果本文侵犯了你的权益,请联系本站底部QQ或者邮箱删除。