PHP头条
热点:

微信公众平台开发(82) 天气预报


在这篇教程中,我们将介绍如何在微信公众平台上开发天气预报功能。我们将使用中国天气网的气象数据接口来获取天气信息。

这篇教程将介绍以下内容:

 

一、中国天气网

中国天气网提供了丰富的气象数据接口。下面我们介绍主要的几个:

国家根节点接口:

http://flash.weather.com.cn/wmaps/xml/china.xml

省级节点接口(以广东为例):

http://flash.weather.com.cn/wmaps/xml/Guangdong.xml

实时天气数据(以深圳为例):

http://www.weather.com.cn/data/sk/101280601.html

: : : : : : : : : : 

六日天气数据(以深圳为例):

http://m.weather.com.cn/data/101280601.html

: : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 

以上接口是我们做天气预报功能将使用到的接口。

 

二、微信公众平台开发

1. 需要将城市编码对照表导入到数据库中,以下是SAE导入后截图

2. 将城市名转为城市代码的方法,这将在用户发送城市名的时候调用

  fromNameToCode(       =      =      =      =      =      = 
      = "weather"      = "SELECT * FROM ".." WHERE `cityName` = '".."'"     
      = (.':'., , ,       (!         ('Could not connect: ' .       ("SET NAMES 'UTF8'"     (,       = (      = ""     ( = (           = ['cityCode'               (       }

3. 编写接口调用函数,用于向天气网发送cURL请求,获取数据

  httpRequest(       =     curl_setopt(, CURLOPT_URL,      curl_setopt(, CURLOPT_RETURNTRANSFER, 1      = curl_exec(     curl_close(      ( ===           "cURL Error: ". curl_error(        }

4. 编写获取天气数据的函数,这里获取实时天气和未来3日天气,并将返回结果封装成图文信息的数组

  getWeatherInfo(       = fromNameToCode(     
      ( == ""           "错误的城市名或者此城市不在数据库中!"      
     
      = "http://www.weather.com.cn/data/sk/"..".html"      = httpRequest(      = json_decode(,       = ['weatherinfo' 
      =      [] = ("Title"=>$info['city']."天气预报", "Description"=>"", "PicUrl"=>"", "Url" =>""      ((int) < 101340000          = "实况 温度:".['temp']."℃ 湿度:".['SD']." 风速:".['WD'].['WSE']."级"         [] = ("Title"=>("%", "﹪", ), "Description"=>"", "PicUrl"=>"", "Url" =>""  
     
      = "http://m.weather.com.cn/data/"..".html"      = httpRequest(      = json_decode(,       = ['weatherinfo' 
      (!(['index_d'         [] = ("Title" =>['index_d'], "Description" =>"", "PicUrl" =>"", "Url" =>""  
      = ("日","一","二","三","四","五","六"      = 3      ( = 1;  <= ; ++          = ("+".(-1)." day"          = ("m月d日",)." 周".[('w',)]." ".['temp'.]." ".['weather'.]." ".['wind'.         [] = ("Title" =>, "Description" =>"", "PicUrl" =>"http://discuz.comli.com/weixin/weather/"."d".("%02u",['img'.(( *2)-1)]).".jpg", "Url" =>""  
       }

5. 在微信公众平台接口函数中处理收到的文本消息时,文本将为城市名,直接调用获取天气的函数

 
   
 
 ("TOKEN", "weixin" 
  =   (!(['echostr'     -> }     ->  
                   = ["echostr"         (->                              
                 = ["signature"          = ["timestamp"          = ["nonce"          =          = (, ,          (          = (          = ( 
         ( ==                        }                 
                 = ["HTTP_RAW_POST_DATA"          (!(             ->logger("R ".              = (, 'SimpleXMLElement',              = (-> 
              (                   "event":
                      = ->receiveEvent(                                       "text":
                      = ->receiveText(                                   ->logger("T ".                       }              ""                    
       receiveEvent(           = ""          (->               "subscribe":
                  = "欢迎关注方倍工作室 "                               "unsubscribe":
                  = "取消关注"                            = ->transmitText(,               
       receiveText(           = (-> 
         ("weather.php"          = getWeatherInfo(          = ->transmitNews(,                
     
       transmitText(,            = "      </xml>"          = (, ->FromUserName, ->ToUserName, (),             
       transmitNews(,           (!(              
          = "      "          = ""          (                .= (, ['Title'], ['Description'], ['PicUrl'], ['Url' 
          = "         </xml>" 
          = (, ->FromUserName, ->ToUserName, (), (                
       logger(   }

 

 

四、如何扩展

1. 在数据库加入邮编、区号、拼音等字段,可以扩展成使用区号等方式查询,请参考:http://www.cnblogs.com/txw1958/archive/2013/02/07/weixin-if4-weather-forecast.html

2. 拥有高级接口的用户,可以使用结合语音识别+中文分词技术,实现语音版的智能查询,请参考:http://www.cnblogs.com/txw1958/archive/2013/02/06/weixin-if40-voice-recognition.html

www.phpzy.comtrue/php/7007.htmlTechArticle微信公众平台开发(82) 天气预报 在这篇教程中,我们将介绍如何在微信公众平台上开发天气预报功能。我们将使用中国天气网的气象数据接口来获取天气信息。 这篇教程将介绍以下内容...

相关文章

相关频道:

PHP之友评论

今天推荐