您好,欢迎访问一九零五行业门户网

微信公众平台开发(五) 天气预报功能开发

前面几篇文章对微信公众平台的开通及简单使用做了简单的介绍,但都没有涉及到实际使用中的问题,例如天气查询,公交查询,快递查询等。接下来的几篇文章将对实际生活中会经常使用到的一些功能进行开发讲解,以供读者参考。
这一篇文章将对大家每天都会关心的天气查询进行开发,例如,用户发送消息 “苏州天气”,则会返回苏州实时天气状况,以及未来两天甚至未来五天的天气状况。
首先要对用户发送过来的消息进行判断,判断消息里是否含有“天气”关键字,如果含有,则需要继续提取地区信息,然后再通过中国天气网(http://www.weather.com.cn)提供的开放api进行相关地区天气查询。
用户发送过来查询天气的消息的格式是固定好的,即 “地区+天气”,所以首先截取后两个字,判断是否为 “天气” 关键字。
使用php函数 mb_substr() 截取,关于该函数的用法:
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);
从消息的开头开始,截掉末尾的两个字符(天气),既得地区关键字。
然后进行判断,继而调用函数查询天气数据。
我们这里调用的是中国国家气象局提供的天气预报api接口,接口地址:http://m.weather.com.cn/data/101190401.html
url中的数字指代城市的编号101190401(苏州),其他城市对应关系将在下面提供。
该接口返回信息比较全面,也是以json格式提供,格式如下:
//基本信息;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:不易发}}
weather() 函数如下:
这里include 了一个城市对应关系文件 weather_cityid.php,格式如下:
101190401
根据传入的城市名,得到城市代码,如果不为空,则调用中国天气网的api进行查询,返回json格式的数据,然后进行解析并返回数据,如果为空,则返回null值。
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//穿衣指数

date_y. .weatherinfo->fchh.时发布.\n\n实时天气\n.weatherinfo->weather1. .weatherinfo->temp1. .weatherinfo->wind1.\n\n温馨提示:.weatherinfo->index_d.\n\n明天\n.weatherinfo->weather2. .weatherinfo->temp2. .weatherinfo->wind2.\n\n后天\n.weatherinfo->weather3. .weatherinfo->temp3. .weatherinfo-> 搜号码 -> zhuojinsz,查找并关注。
2. 扫描二维码:
卓锦苏州 功能列表。
we believe, great people share knowledge...

其它类似信息

推荐信息