虽然现在随着智能手机的普及,查询天气已经是一件轻而易举的事情,前几天无意间发现一个百度的天气预报Api很不错,索性花了点时间加入到了自己的微信公众号当中,下面简单记录一下开发思路.
依旧是老样子,首先配置参数
1 2 3 4 5 6 7 8 |
<?php /*微信文字回复模块应用归类数据存储模块 2015-12-19 pm 11:00 */ $SendTempArr = array( '_shuoshuo' =>['keywords'=>'碎碎念','prompt'=>'发表碎碎念模式已开启啦~,赶快告诉我你的心情吧~[调皮]','Fname'=>'sendShuoshuo','next'=>'Hi,你的碎碎念已经发送过了,接下来您想干点什么呢?'], '_Music' =>['keywords'=>'找歌','prompt'=>'找歌模式已经开启了,快告诉我你想听什么歌[调皮]','Fname'=>'music','next'=>''], '_Speak' =>['keywords'=>'我要说','prompt'=>'Hi,摸摸大...请输入你想多我说的话吧,格式如下: #明哥哥#明哥哥你在干嘛呢?,其中##中间的就是标题,其他的是内容哦~聪明的你,学会了吗?','Fname'=>'Myspeak','next'=>''], '_Weather' =>['keywords'=>'天气','prompt'=>'Hi,请告诉我你要查询的城市名字吧~','Fname'=>'weather','next'=>''], ); |
这是之前写的配置项,加入了天气预报这一项,下面是实现代码:
其中的$message是微信服务器推送给开发者的用户消息数组
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
public function weather($message){ $url = 'http://api.map.baidu.com/telematics/v3/weather?location='.$message['Content'].'&output=json&ak=Isfaaj45HoG35tgG1rEv0U3G'; $weatheer = https_request($url); $weatheer = json_decode($weatheer,true); $weatheer = $weatheer['results'][0]['weather_data']; foreach ($weatheer as $k => $v){ $time = date('H',time()); if($time <18 && $time > 8){ $picurl = $v['dayPictureUrl']; }else{ $picurl = $v['nightPictureUrl']; } $TempArr[] = array( 'Title'=>$v['date'].' '.$v['weather'].''.$v['wind'], 'Description'=>$v['date'].'天气情况', 'PicUrl'=>$picurl, 'Url'=>'http://blog.he29.com' ); } return $this->replyState($TempArr,1,'ArticleImg');//回复图文小西类型 } |
这是api接口返给我们的天气数据
1 |
{"error":0,"status":"success","date":"2015-12-20","results":[{"currentCity":"北京","pm25":"231","index":[{"title":"穿衣","zs":"冷","tipt":"穿衣指数","des":"天气冷,建议着棉服、羽绒服、皮夹克加羊毛衫等冬季服装。年老体弱者宜着厚棉衣、冬大衣或厚羽绒服。"},{"title":"洗车","zs":"不宜","tipt":"洗车指数","des":"不宜洗车,未来24小时内有霾,如果在此期间洗车,会弄脏您的爱车。"},{"title":"旅游","zs":"较不宜","tipt":"旅游指数","des":"空气质量差,不适宜旅游"},{"title":"感冒","zs":"易发","tipt":"感冒指数","des":"昼夜温差大,且空气湿度较大,易发生感冒,请注意适当增减衣服,加强自我防护避免感冒。"},{"title":"运动","zs":"较不宜","tipt":"运动指数","des":"有雾或霾天气,较冷,建议适当停止户外运动,选择在室内进行运动,以避免吸入过多雾中有害物质,损害健康。"},{"title":"紫外线强度","zs":"最弱","tipt":"紫外线强度指数","des":"属弱紫外线辐射天气,无需特别防护。若长期在户外,建议涂擦SPF在8-12之间的防晒护肤品。"}],"weather_data":[{"date":"周日 12月20日 (实时:-2℃)","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/wu.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/wu.png","weather":"雾","wind":"微风","temperature":"4 ~ -5℃"},{"date":"周一","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/mai.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/wu.png","weather":"霾转雾","wind":"微风","temperature":"5 ~ -4℃"},{"date":"周二","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/mai.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/wu.png","weather":"霾转雾","wind":"微风","temperature":"4 ~ -4℃"},{"date":"周三","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/mai.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/yin.png","weather":"霾转阴","wind":"微风","temperature":"5 ~ -3℃"}]}]} |
最后结果如图所示:
最后不得不吐槽一下, 图片好丑啊~~~