实用技巧公众号天气预报公众号怎么玩

微信公众平台开发天气预报功能的实例分享
&$itemTpl = &
&$item_str = &&;
&foreach ($newsArray as $item){
&$item_str .= sprintf($itemTpl, $item['Title'], $item['Description'], $item['PicUrl'], $item['Url']);
&$newsTpl = &
&$result = sprintf($newsTpl, $object-&FromUserName, $object-&ToUserName, time(), count($newsArray));
&private function logger($log_content)
第四步:使用百度车联API V3.0接口,及访问应用AK码,编号微信天气接口源码:
weatger.php
function getWeatherInfo($cityName){
if ($cityName == && || (strstr($cityName, &+&))){
return &发送天气+城市,例如'天气深圳'&; }//用户查询天气,回复关键词 规则
$url = &http://api./telematics/v3/weather?location=&.urlencode($cityName).&&output=json&ak=自已申请的百度车联API AK代码&;//构建通过百度车联API V3.0查询天气url链接
$ch = curl_init();//初始化会话 curl_setopt($ch, CURLOPT_URL, $url);//设置会话参数 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//设置会话参数 $output = curl_exec($ch);//执行curl会话
curl_close($ch);//关闭curl会话
$result = json_decode($output, true);//函数json_decode() 的功能时将json数据格式转换为数组。
&if ($result[&error&] != 0){
&return $result[&status&]; }
&$curHour = (int)date('H',time());
&$weather = $result[&results&][0];//按照微信公众号开发文档,组建设多图文回复信息
&$weatherArray[] = array(&Title& =& $weather['currentCity'].&当前天气:&.&温度:&.$weather['weather_data'][0]['temperature'].&,&.$weather['weather_data'][0]['weather'].&,&.&风力:&.$weather['weather_data'][0]['wind'].&.&, &Description& =&&&, &PicUrl& =&&/img/weather_bg.jpg&, &Url& =&&&);
&for ($i = 0; $i& count($weather[&weather_data&]); $i++) {
&$weatherArray[] = array(&Title&=&
&$weather[&weather_data&][$i][&date&].&\n&.
&$weather[&weather_data&][$i][&weather&].& &.
&$weather[&weather_data&][$i][&wind&].& &.
&$weather[&weather_data&][$i][&temperature&].&&,
&&Description&=&&&,
&&PicUrl&=&(($curHour &= 6)
&&& ($curHour& 18))?$weather[&weather_data&][$i][&dayPictureUrl&]:$weather[&weather_data&][$i][&nightPictureUrl&], &Url&=&&&);
&return $weatherA}?&
微信公众平台 TOKEN 与自己编写的微信接口文件中的 TOKEN 的值必须保持一致
编写php代码,需使用专业的php编辑工具,如EditPlus,Dreamweaver等
上述天气接口文件中,百度车联api AK代码已换成 &自已申请的百度车联API AK代码:请申请好后,自行填入
如要不明白的地方,可以关注我的百度空间.留言和我联系!
微信天气预报开发最新代码:
& http-equiv=&Content-Type& content=&text/ charset=utf& /&
&header(&Content-Type:text/charset=UTF-&);
&date_default_timezone_set(&PRC&);
&* 最开始用的是微信提供的demo老是不成功,用了这个网上下载的才成功
&//define your token
&define(&TOKEN&, &djjc&);
&$wechatObj = new wechatCallbackapiTest();
&//微信接入操作,成功接入了直接注释了就行了
&$wechatObj-&responseMsg();
&//$wechatObj-&valid();
&class wechatCallbackapiTest
&/*public function valid()
&$echoStr = $_GET[&echostr&];
&//valid signature , option
&if($this-&checkSignature()){
&echo $echoS
&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);&&&[2]&&&
【声明】:黑吧安全网()登载此文出于传递更多信息之目的,并不代表本站赞同其观点和对其真实性负责,仅适于网络安全技术爱好者学习研究使用,学习中请遵循国家相关法律法规。如有问题请联系我们,联系邮箱,我们会在最短的时间内进行处理。
上一篇:【】【】前面几篇文章对微信公众平台的开通及简单使用做了简单的介绍,但都没有涉及到实际使用中的问题,例如天气查询,公交查询,快递查询等。接下来的几篇文章将对实际生活中会经常使用到的一些功能进行开发讲解,以供读者参考。
这一篇文章将对大家每天都会关心的天气查询进行开发,例如,用户发送消息 &苏州天气&,则会返回苏州实时天气状况,以及未来两天甚至未来五天的天气状况。
二、思路分析
首先要对用户发送过来的消息进行判断,判断消息里是否含有&天气&关键字,如果含有,则需要继续提取地区信息,然后再通过中国天气网()提供的开放API进行相关地区天气查询。
三、关键字判断与地区读取
用户发送过来查询天气的消息的格式是固定好的,即 &地区+天气&,所以首先截取后两个字,判断是否为 &天气& 关键字。
使用php函数 mb_substr() 截取,关于该函数的用法:
mb_substr & 获取字符串的部分
  string mb_substr ( string $str , int $start [, int $length [, string $encoding ]] )
说明:根据字符数执行一个多字节安全的 substr() 操作。 位置是从 str 的开始位置进行计数。 第一个字符的位置是 0。第二个字符的位置是 1,以此类推。
从该 string 中提取子字符串。
str 中要使用的第一个字符的位置。正数 -& 从字符串开头指定位置开始;负数 -& 从字符串结尾指定位置开始;
str 中要使用的最大字符数。正数 -& 从 start 处开始最多包括 length 个字符;负数 -& string 末尾处的 length 个字符将会被漏掉(若 start 是负数则从字符串开头算起)。
encoding 参数为字符编码。如果省略,则使用内部字符编码。
mb_substr() 函数根据 start 和 length 参数返回 str 中指定的部分。
$str = mb_substr($keyword,-2,2,"UTF-8");
从消息的结尾数第二个字符开始截取,截取两个字符,然后加以判断是否为 &天气& 关键字。
下面进行地区提取,还是使用&mb_substr() 函数。
$str_key = mb_substr($keyword,0,-2,"UTF-8");
从消息的开头开始,截掉末尾的两个字符(天气),既得地区关键字。
然后进行判断,继而调用函数查询天气数据。
if($str == '天气' && !empty($str_key))
//调用函数查询天气数据
四、调用 weather() 函数查询
我们这里调用的是中国国家气象局提供的天气预报API接口,接口地址:.cn/data/.html
URL中的数字指代城市的编号(苏州),其他城市对应关系将在下面提供。
该接口返回信息比较全面,也是以json格式提供,格式如下:
{"weatherinfo":{//基本信息;
"city":"苏州","city_en":"suzhou",
"date_y":"日","date":"","week":"星期二","fchh":"18","cityid":"",//摄氏温度
"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":"不易发"}}
我们可以通过解析JSON,获取相应城市的天气数据。
weather() 函数如下:
private function weather($n){
include("weather_cityId.php");
$c_name=$weather_cityId[$n];
if(!empty($c_name)){
$json=file_get_contents(".cn/data/".$c_name.".html");
return json_decode($json);
return null;
这里include 了一个城市对应关系文件 weather_cityId.php,格式如下:
$weather_cityId = array("北京"=&"","上海"=&"","苏州"=&"");
根据传入的城市名,得到城市代码,如果不为空,则调用中国天气网的API进行查询,返回json格式的数据,然后进行解析并返回数据,如果为空,则返回null值。
五、组织回复消息形式
判断返回数据是否为空,若为空,则&$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;
$data-&weatherinfo-&city  //获取城市名,这里为苏州
$data-&weatherinfo-&date_y  //获取日期,这里为日
$data-&weatherinfo-&fchh  //数据发布时间
$data-&weatherinfo-&weather1  //实时天气
$data-&weatherinfo-&temp1  //实时温度
$data-&weatherinfo-&wind1  //实时风向和风速
$data-&weatherinfo-&index_d  //穿衣指数
weather2, temp2, wind2 分别代表了明天的天气,温度和风向风速,其他的以此类推。
\n  //表示换行
七、完整代码
* 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 $echoS
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);
case "event":
$resultStr = $this-&handleEvent($postObj);
$resultStr = "Unknow msg type: ".$RX_TYPE;
echo $resultStr;
public function handleText($postObj)
$fromUsername = $postObj-&FromUserN
$toUsername = $postObj-&ToUserN
$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&
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 = "抱歉,没有查到\"".$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;
$contentStr = "感谢您关注【卓锦苏州】"."\n"."微信号:zhuojinsz"."\n"."卓越锦绣,名城苏州,我们为您提供苏州本地生活指南,苏州相关信息查询,做最好的苏州微信平台。"."\n"."目前平台功能如下:"."\n"."【1】 查天气,如输入:苏州天气"."\n"."【2】 查公交,如输入:苏州公交178"."\n"."【3】 翻译,如输入:翻译I love you"."\n"."【4】 苏州信息查询,如输入:苏州观前街"."\n"."更多内容,敬请期待...";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
echo "Input something...";
public function handleEvent($object)
$contentStr = "";
switch ($object-&Event)
case "subscribe":
$contentStr = "感谢您关注【卓锦苏州】"."\n"."微信号:zhuojinsz"."\n"."卓越锦绣,名城苏州,我们为您提供苏州本地生活指南,苏州相关信息查询,做最好的苏州微信平台。"."\n"."目前平台功能如下:"."\n"."【1】 查天气,如输入:苏州天气"."\n"."【2】 查公交,如输入:苏州公交178"."\n"."【3】 翻译,如输入:翻译I love you"."\n"."【4】 苏州信息查询,如输入:苏州观前街"."\n"."更多内容,敬请期待...";
$contentStr = "Unknow Event: ".$object-&E
$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&
$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(".cn/data/".$c_name.".html");
return json_decode($json);
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;
return false;
关于城市对应关系文件&weather_cityId.php,目前更新到了2564个城市,以后还会继续增加,请到&&官方网盘(URL:)下载吧。
请关注&卓锦苏州&微信公众帐号,卓锦苏州&基于BAE 平台开发,针对于主流的微信功能进行开发测试。
您可以关注&卓锦苏州&公众帐号进行功能测试,以及获取新的应用开发。
1. 登录微信客户端,通讯录 -& 添加朋友 -& 查找公众号 -&&zhuojinsz,查找并关注。
2. 扫描二维码:
卓锦苏州&功能列表。
David Camp
我们永远相信,分享是一种美德 |&We Believe, Great People Share Knowledge...
阅读(...) 评论()你的位置:
&& 一枚没有看天气预报就出去玩的喵星人,回来后...
一枚没有看天气预报就出去玩的喵星人,回来后...
| 时间:04月21日
爱猫人士都关注了的猫图文公号
点击标题下方蓝色字关注大爱猫咪控
网友家的猫跑出去玩的时候,还是晴天
结果碰到下雨,赶紧跑了回来
铲屎的,朕出门前为什么不告诉朕会下雨!
朕的毛衣都湿透了…
铲屎的你还照!快给朕开门!
能不能麻利点?
进门后,主人也是马上找了条毛巾帮猫擦干了
大爱猫咪控关注微信号:daaimaomikong 海量萌猫图随你看,回复“喵”,随机送你一条精彩图文,每天萌不停
不知道怎么关注的?
↓↓↓点击阅读原文,快速关注!

我要回帖

更多关于 北京天气预报 的文章

 

随机推荐