Home > Backend Development > PHP Tutorial > WeChat public platform development (82) Weather forecast_PHP tutorial

WeChat public platform development (82) Weather forecast_PHP tutorial

WBOY
Release: 2016-07-13 10:36:33
Original
1190 people have browsed it

In this tutorial, we will introduce how to develop the weather forecast function on the WeChat public platform. We will use the meteorological data interface of China Weather Network to obtain weather information.

This tutorial will cover the following:

1. China Weather Network

China Weather Network provides a rich meteorological data interface. Below we introduce the main ones:

National root node interface:

http://flash.weather.com.cn/wmaps/xml/china.xml
Copy after login

<span <</span><span city </span><span quName</span><span ="广东"</span><span  pyName</span><span ="guangdong"</span><span  cityname</span><span ="广州"</span><span  state1</span><span ="2"</span><span  state2</span><span ="7"</span><span  stateDetailed</span><span ="阴转小雨"</span><span  tem1</span><span ="8"</span><span  tem2</span><span ="15"</span><span  windState</span><span ="微风"</span><span /></span>
Copy after login

Provincial node interface (taking Guangdong as an example):

http://flash.weather.com.cn/wmaps/xml/Guangdong.xml
Copy after login

<span <</span><span city </span><span cityX</span><span ="409"</span><span  cityY</span><span ="257"</span><span  cityname</span><span ="深圳"</span><span  centername</span><span ="深圳"</span><span  fontColor</span><span ="FFFFFF"</span><span  pyName</span><span ="shenzhen"</span><span  state1</span><span ="1"</span><span  state2</span><span ="1"</span><span  stateDetailed</span><span ="多云"</span><span  tem1</span><span ="12"</span><span  tem2</span><span ="18"</span><span  temNow</span><span ="12"</span><span  windState</span><span ="微风"</span><span  windDir</span><span ="东北风"</span><span  windPower</span><span ="1级"</span><span  humidity</span><span ="74%"</span><span  time</span><span ="21:15"</span><span  url</span><span ="101280601"</span><span /></span>
Copy after login

.

Real-time weather data (taking Shenzhen as an example):

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

<span {
    </span><span "</span><span weatherinfo</span><span "</span><span : {
        </span><span "</span><span city</span><span "</span>: <span "</span><span 深圳</span><span "</span><span ,
        </span><span "</span><span cityid</span><span "</span>: <span "</span><span 101280601</span><span "</span><span ,
        </span><span "</span><span temp</span><span "</span>: <span "</span><span 12</span><span "</span><span ,
        </span><span "</span><span WD</span><span "</span>: <span "</span><span 东北风</span><span "</span><span ,
        </span><span "</span><span WS</span><span "</span>: <span "</span><span 1级</span><span "</span><span ,
        </span><span "</span><span SD</span><span "</span>: <span "</span><span 75%</span><span "</span><span ,
        </span><span "</span><span WSE</span><span "</span>: <span "</span><span 1</span><span "</span><span ,
        </span><span "</span><span time</span><span "</span>: <span "</span><span 21:20</span><span "</span><span ,
        </span><span "</span><span isRadar</span><span "</span>: <span "</span><span 1</span><span "</span><span ,
        </span><span "</span><span Radar</span><span "</span>: <span "</span><span JC_RADAR_AZ9755_JB</span><span "</span><span 
    }
}</span>
Copy after login

Six-day weather data (taking Shenzhen as an example):

http://m.weather.com.cn/data/101280601.html
Copy after login

<span {
    </span><span "</span><span weatherinfo</span><span "</span><span : {
        </span><span "</span><span city</span><span "</span>: <span "</span><span 深圳</span><span "</span><span ,
        </span><span "</span><span city_en</span><span "</span>: <span "</span><span shenzhen</span><span "</span><span ,
        </span><span "</span><span date_y</span><span "</span>: <span "</span><span 2014年2月15日</span><span "</span><span ,
        </span><span "</span><span date</span><span "</span>: <span ""</span><span ,
        </span><span "</span><span week</span><span "</span>: <span "</span><span 星期六</span><span "</span><span ,
        </span><span "</span><span fchh</span><span "</span>: <span "</span><span 18</span><span "</span><span ,
        </span><span "</span><span cityid</span><span "</span>: <span "</span><span 101280601</span><span "</span><span ,
        </span><span "</span><span temp1</span><span "</span>: <span "</span><span 12℃~18℃</span><span "</span><span ,
        </span><span "</span><span temp2</span><span "</span>: <span "</span><span 16℃~21℃</span><span "</span><span ,
        </span><span "</span><span temp3</span><span "</span>: <span "</span><span 17℃~23℃</span><span "</span><span ,
        </span><span "</span><span temp4</span><span "</span>: <span "</span><span 11℃~18℃</span><span "</span><span ,
        </span><span "</span><span temp5</span><span "</span>: <span "</span><span 10℃~15℃</span><span "</span><span ,
        </span><span "</span><span temp6</span><span "</span>: <span "</span><span 12℃~17℃</span><span "</span><span ,
        </span><span "</span><span tempF1</span><span "</span>: <span "</span><span 53.6℉~64.4℉</span><span "</span><span ,
        </span><span "</span><span tempF2</span><span "</span>: <span "</span><span 60.8℉~69.8℉</span><span "</span><span ,
        </span><span "</span><span tempF3</span><span "</span>: <span "</span><span 62.6℉~73.4℉</span><span "</span><span ,
        </span><span "</span><span tempF4</span><span "</span>: <span "</span><span 51.8℉~64.4℉</span><span "</span><span ,
        </span><span "</span><span tempF5</span><span "</span>: <span "</span><span 50℉~59℉</span><span "</span><span ,
        </span><span "</span><span tempF6</span><span "</span>: <span "</span><span 53.6℉~62.6℉</span><span "</span><span ,
        </span><span "</span><span weather1</span><span "</span>: <span "</span><span 多云</span><span "</span><span ,
        </span><span "</span><span weather2</span><span "</span>: <span "</span><span 阴</span><span "</span><span ,
        </span><span "</span><span weather3</span><span "</span>: <span "</span><span 多云</span><span "</span><span ,
        </span><span "</span><span weather4</span><span "</span>: <span "</span><span 中雨</span><span "</span><span ,
        </span><span "</span><span weather5</span><span "</span>: <span "</span><span 多云</span><span "</span><span ,
        </span><span "</span><span weather6</span><span "</span>: <span "</span><span 阴</span><span "</span><span ,
        </span><span "</span><span img1</span><span "</span>: <span "</span><span 1</span><span "</span><span ,
        </span><span "</span><span img2</span><span "</span>: <span "</span><span 99</span><span "</span><span ,
        </span><span "</span><span img3</span><span "</span>: <span "</span><span 2</span><span "</span><span ,
        </span><span "</span><span img4</span><span "</span>: <span "</span><span 99</span><span "</span><span ,
        </span><span "</span><span img5</span><span "</span>: <span "</span><span 1</span><span "</span><span ,
        </span><span "</span><span img6</span><span "</span>: <span "</span><span 99</span><span "</span><span ,
        </span><span "</span><span img7</span><span "</span>: <span "</span><span 8</span><span "</span><span ,
        </span><span "</span><span img8</span><span "</span>: <span "</span><span 99</span><span "</span><span ,
        </span><span "</span><span img9</span><span "</span>: <span "</span><span 1</span><span "</span><span ,
        </span><span "</span><span img10</span><span "</span>: <span "</span><span 99</span><span "</span><span ,
        </span><span "</span><span img11</span><span "</span>: <span "</span><span 2</span><span "</span><span ,
        </span><span "</span><span img12</span><span "</span>: <span "</span><span 99</span><span "</span><span ,
        </span><span "</span><span img_single</span><span "</span>: <span "</span><span 1</span><span "</span><span ,
        </span><span "</span><span img_title1</span><span "</span>: <span "</span><span 多云</span><span "</span><span ,
        </span><span "</span><span img_title2</span><span "</span>: <span "</span><span 多云</span><span "</span><span ,
        </span><span "</span><span img_title3</span><span "</span>: <span "</span><span 阴</span><span "</span><span ,
        </span><span "</span><span img_title4</span><span "</span>: <span "</span><span 阴</span><span "</span><span ,
        </span><span "</span><span img_title5</span><span "</span>: <span "</span><span 多云</span><span "</span><span ,
        </span><span "</span><span img_title6</span><span "</span>: <span "</span><span 多云</span><span "</span><span ,
        </span><span "</span><span img_title7</span><span "</span>: <span "</span><span 中雨</span><span "</span><span ,
        </span><span "</span><span img_title8</span><span "</span>: <span "</span><span 中雨</span><span "</span><span ,
        </span><span "</span><span img_title9</span><span "</span>: <span "</span><span 多云</span><span "</span><span ,
        </span><span "</span><span img_title10</span><span "</span>: <span "</span><span 多云</span><span "</span><span ,
        </span><span "</span><span img_title11</span><span "</span>: <span "</span><span 阴</span><span "</span><span ,
        </span><span "</span><span img_title12</span><span "</span>: <span "</span><span 阴</span><span "</span><span ,
        </span><span "</span><span img_title_single</span><span "</span>: <span "</span><span 多云</span><span "</span><span ,
        </span><span "</span><span wind1</span><span "</span>: <span "</span><span 微风</span><span "</span><span ,
        </span><span "</span><span wind2</span><span "</span>: <span "</span><span 微风</span><span "</span><span ,
        </span><span "</span><span wind3</span><span "</span>: <span "</span><span 微风</span><span "</span><span ,
        </span><span "</span><span wind4</span><span "</span>: <span "</span><span 东北风3-4级</span><span "</span><span ,
        </span><span "</span><span wind5</span><span "</span>: <span "</span><span 微风</span><span "</span><span ,
        </span><span "</span><span wind6</span><span "</span>: <span "</span><span 微风</span><span "</span><span ,
        </span><span "</span><span fx1</span><span "</span>: <span "</span><span 微风</span><span "</span><span ,
        </span><span "</span><span fx2</span><span "</span>: <span "</span><span 微风</span><span "</span><span ,
        </span><span "</span><span fl1</span><span "</span>: <span "</span><span 小于3级</span><span "</span><span ,
        </span><span "</span><span fl2</span><span "</span>: <span "</span><span 小于3级</span><span "</span><span ,
        </span><span "</span><span fl3</span><span "</span>: <span "</span><span 小于3级</span><span "</span><span ,
        </span><span "</span><span fl4</span><span "</span>: <span "</span><span 3-4级</span><span "</span><span ,
        </span><span "</span><span fl5</span><span "</span>: <span "</span><span 小于3级</span><span "</span><span ,
        </span><span "</span><span fl6</span><span "</span>: <span "</span><span 小于3级</span><span "</span><span ,
        </span><span "</span><span index</span><span "</span>: <span "</span><span 较舒适</span><span "</span><span ,
        </span><span "</span><span index_d</span><span "</span>: <span "</span><span 建议着薄外套、开衫牛仔衫裤等服装。年老体弱者应适当添加衣物,宜着夹克衫、薄毛衣等。</span><span "</span><span ,
        </span><span "</span><span index48</span><span "</span>: <span "</span><span 较舒适</span><span "</span><span ,
        </span><span "</span><span index48_d</span><span "</span>: <span "</span><span 建议着薄外套、开衫牛仔衫裤等服装。年老体弱者应适当添加衣物,宜着夹克衫、薄毛衣等。</span><span "</span><span ,
        </span><span "</span><span index_uv</span><span "</span>: <span "</span><span 最弱</span><span "</span><span ,
        </span><span "</span><span index48_uv</span><span "</span>: <span "</span><span 最弱</span><span "</span><span ,
        </span><span "</span><span index_xc</span><span "</span>: <span "</span><span 适宜</span><span "</span><span ,
        </span><span "</span><span index_tr</span><span "</span>: <span "</span><span 适宜</span><span "</span><span ,
        </span><span "</span><span index_co</span><span "</span>: <span "</span><span 舒适</span><span "</span><span ,
        </span><span "</span><span st1</span><span "</span>: <span "</span><span 19</span><span "</span><span ,
        </span><span "</span><span st2</span><span "</span>: <span "</span><span 13</span><span "</span><span ,
        </span><span "</span><span st3</span><span "</span>: <span "</span><span 23</span><span "</span><span ,
        </span><span "</span><span st4</span><span "</span>: <span "</span><span 16</span><span "</span><span ,
        </span><span "</span><span st5</span><span "</span>: <span "</span><span 24</span><span "</span><span ,
        </span><span "</span><span st6</span><span "</span>: <span "</span><span 18</span><span "</span><span ,
        </span><span "</span><span index_cl</span><span "</span>: <span "</span><span 适宜</span><span "</span><span ,
        </span><span "</span><span index_ls</span><span "</span>: <span "</span><span 适宜</span><span "</span><span ,
        </span><span "</span><span index_ag</span><span "</span>: <span "</span><span 不易发</span><span "</span><span 
    }
}</span>
Copy after login

The above interface is the interface we will use for the weather forecast function.

2. WeChat public platform development

1. The city code comparison table needs to be imported into the database. The following is a screenshot after SAE import

2. Method to convert city name to city code, which will be called when the user sends the city name

<span  1</span> <span function</span> fromNameToCode(<span $name</span><span )
</span><span  2</span> <span {
</span><span  3</span>     <span $mysql_host</span> =<span  SAE_MYSQL_HOST_M;
</span><span  4</span>     <span $mysql_host_s</span> =<span  SAE_MYSQL_HOST_S;
</span><span  5</span>     <span $mysql_port</span> =<span  SAE_MYSQL_PORT;
</span><span  6</span>     <span $mysql_user</span> =<span  SAE_MYSQL_USER;
</span><span  7</span>     <span $mysql_password</span> =<span  SAE_MYSQL_PASS;
</span><span  8</span>     <span $mysql_database</span> =<span  SAE_MYSQL_DB;
</span><span  9</span> 
<span 10</span>     <span $mysql_table</span> = "weather"<span ;
</span><span 11</span>     <span $mysql_state</span> = "SELECT * FROM ".<span $mysql_table</span>." WHERE `cityName` = '".<span $name</span>."'"<span ;
</span><span 12</span>     
<span 13</span>     <span $con</span> = <span mysql_connect</span>(<span $mysql_host</span>.':'.<span $mysql_port</span>, <span $mysql_user</span>, <span $mysql_password</span>, <span true</span><span );
</span><span 14</span>     <span if</span> (!<span $con</span><span ){
</span><span 15</span>         <span die</span>('Could not connect: ' . <span mysql_error</span><span ());
</span><span 16</span> <span     }
</span><span 17</span>     <span mysql_query</span>("SET NAMES 'UTF8'"<span );
</span><span 18</span>     <span mysql_select_db</span>(<span $mysql_database</span>, <span $con</span><span );
</span><span 19</span>     <span $result</span> = <span mysql_query</span>(<span $mysql_state</span><span );
</span><span 20</span>     <span $cityCode</span> = ""<span ;
</span><span 21</span>     <span while</span>(<span $row</span> = <span mysql_fetch_array</span>(<span $result</span><span ))
</span><span 22</span> <span     {
</span><span 23</span>         <span $cityCode</span> = <span $row</span>['cityCode'<span ]; 
</span><span 24</span>         <span break</span><span ;
</span><span 25</span> <span     }
</span><span 26</span>     <span mysql_close</span>(<span $con</span><span );
</span><span 27</span>     <span return</span> <span $cityCode</span><span ;
</span><span 28</span> }
Copy after login

3. Write an interface call function to send a cURL request to the weather network and obtain data

<span  1</span> <span function</span> httpRequest(<span $url</span><span )
</span><span  2</span> <span {
</span><span  3</span>     <span $ch</span> =<span  curl_init();
</span><span  4</span>     curl_setopt(<span $ch</span>, CURLOPT_URL, <span $url</span><span );
</span><span  5</span>     curl_setopt(<span $ch</span>, CURLOPT_RETURNTRANSFER, 1<span );
</span><span  6</span>     <span $output</span> = curl_exec(<span $ch</span><span );
</span><span  7</span>     curl_close(<span $ch</span><span );
</span><span  8</span>     <span if</span> (<span $output</span> === <span FALSE</span><span ){
</span><span  9</span>         <span return</span> "cURL Error: ". curl_error(<span $ch</span><span );
</span><span 10</span> <span     }
</span><span 11</span>     <span return</span> <span $output</span><span ;
</span><span 12</span> }
Copy after login

4. Write a function to obtain weather data. Here, real-time weather and weather for the next 3 days are obtained, and the returned results are encapsulated into an array of graphic and text information

<span  1</span> <span function</span> getWeatherInfo(<span $cityName</span><span )
</span><span  2</span> <span {
</span><span  3</span>     <span $cityCode</span> = fromNameToCode(<span $cityName</span><span );
</span><span  4</span>     
<span  5</span>     <span if</span> (<span $cityCode</span> == ""<span )
</span><span  6</span> <span     {
</span><span  7</span>         <span return</span> "错误的城市名或者此城市不在数据库中!"<span ;
</span><span  8</span> <span     }
</span><span  9</span>     
<span 10</span>     <span //</span><span 获取实时天气</span>
<span 11</span>     <span $url</span> = "http://www.weather.com.cn/data/sk/".<span $cityCode</span>.".html"<span ;
</span><span 12</span>     <span $output</span> = httpRequest(<span $url</span><span );
</span><span 13</span>     <span $weather</span> = json_decode(<span $output</span>, <span true</span><span ); 
</span><span 14</span>     <span $info</span> = <span $weather</span>['weatherinfo'<span ];
</span><span 15</span> 
<span 16</span>     <span $weatherArray</span> = <span array</span><span ();
</span><span 17</span>     <span $weatherArray</span>[] = <span array</span>("Title"=>$info['city']."天气预报", "Description"=>"", "PicUrl"=>"", "Url" =>""<span );
</span><span 18</span>     <span if</span> ((int)<span $cityCode</span> < 101340000<span ){
</span><span 19</span>         <span $result</span> = "实况 温度:".<span $info</span>['temp']."℃ 湿度:".<span $info</span>['SD']." 风速:".<span $info</span>['WD'].<span $info</span>['WSE']."级"<span ;
</span><span 20</span>         <span $weatherArray</span>[] = <span array</span>("Title"=><span str_replace</span>("%", "﹪", <span $result</span>), "Description"=>"", "PicUrl"=>"", "Url" =>""<span );
</span><span 21</span> <span     }
</span><span 22</span> 
<span 23</span>     <span //</span><span 获取六日天气</span>
<span 24</span>     <span $url</span> = "http://m.weather.com.cn/data/".<span $cityCode</span>.".html"<span ;
</span><span 25</span>     <span $output</span> = httpRequest(<span $url</span><span );
</span><span 26</span>     <span $weather</span> = json_decode(<span $output</span>, <span true</span><span ); 
</span><span 27</span>     <span $info</span> = <span $weather</span>['weatherinfo'<span ];
</span><span 28</span> 
<span 29</span>     <span if</span> (!<span empty</span>(<span $info</span>['index_d'<span ])){
</span><span 30</span>         <span $weatherArray</span>[] = <span array</span>("Title" =><span $info</span>['index_d'], "Description" =>"", "PicUrl" =>"", "Url" =>""<span );
</span><span 31</span> <span     }
</span><span 32</span> 
<span 33</span>     <span $weekArray</span> = <span array</span>("日","一","二","三","四","五","六"<span );
</span><span 34</span>     <span $maxlength</span> = 3<span ;
</span><span 35</span>     <span for</span> (<span $i</span> = 1; <span $i</span> <= <span $maxlength</span>; <span $i</span>++<span ) {
</span><span 36</span>         <span $offset</span> = <span strtotime</span>("+".(<span $i</span>-1)." day"<span );
</span><span 37</span>         <span $subTitle</span> = <span date</span>("m月d日",<span $offset</span>)." 周".<span $weekArray</span>[<span date</span>('w',<span $offset</span>)]." ".<span $info</span>['temp'.<span $i</span>]." ".<span $info</span>['weather'.<span $i</span>]." ".<span $info</span>['wind'.<span $i</span><span ];
</span><span 38</span>         <span $weatherArray</span>[] = <span array</span>("Title" =><span $subTitle</span>, "Description" =>"", "PicUrl" =>"http://discuz.comli.com/weixin/weather/"."d".<span sprintf</span>("%02u",<span $info</span>['img'.((<span $i</span> *2)-1)]).".jpg", "Url" =>""<span );
</span><span 39</span> <span     }
</span><span 40</span> 
<span 41</span>     <span return</span> <span $weatherArray</span><span ;
</span><span 42</span> }
Copy after login

5. When processing the received text message in the WeChat public platform interface function, the text will be the city name, and the function to get the weather is directly called

<span   1</span> <span /*</span>
<span   2</span> <span     方倍工作室
</span><span   3</span> <span     CopyRight 2013 All Rights Reserved
</span><span   4</span> <span */</span>
<span   5</span> 
<span   6</span> <span define</span>("TOKEN", "weixin"<span );
</span><span   7</span> 
<span   8</span> <span $wechatObj</span> = <span new</span><span  wechatCallbackapiTest();
</span><span   9</span> <span if</span> (!<span isset</span>(<span $_GET</span>['echostr'<span ])) {
</span><span  10</span>     <span $wechatObj</span>-><span responseMsg();
</span><span  11</span> }<span else</span><span {
</span><span  12</span>     <span $wechatObj</span>-><span valid();
</span><span  13</span> <span }
</span><span  14</span> 
<span  15</span> <span class</span><span  wechatCallbackapiTest
</span><span  16</span> <span {
</span><span  17</span>     <span public</span> <span function</span><span  valid()
</span><span  18</span> <span     {
</span><span  19</span>         <span $echoStr</span> = <span $_GET</span>["echostr"<span ];
</span><span  20</span>         <span if</span>(<span $this</span>-><span checkSignature()){
</span><span  21</span>             <span echo</span> <span $echoStr</span><span ;
</span><span  22</span>             <span exit</span><span ;
</span><span  23</span> <span         }
</span><span  24</span> <span     }
</span><span  25</span> 
<span  26</span>     <span private</span> <span function</span><span  checkSignature()
</span><span  27</span> <span     {
</span><span  28</span>         <span $signature</span> = <span $_GET</span>["signature"<span ];
</span><span  29</span>         <span $timestamp</span> = <span $_GET</span>["timestamp"<span ];
</span><span  30</span>         <span $nonce</span> = <span $_GET</span>["nonce"<span ];
</span><span  31</span>         <span $token</span> =<span  TOKEN;
</span><span  32</span>         <span $tmpArr</span> = <span array</span>(<span $token</span>, <span $timestamp</span>, <span $nonce</span><span );
</span><span  33</span>         <span sort</span>(<span $tmpArr</span><span );
</span><span  34</span>         <span $tmpStr</span> = <span implode</span>(<span $tmpArr</span><span );
</span><span  35</span>         <span $tmpStr</span> = <span sha1</span>(<span $tmpStr</span><span );
</span><span  36</span> 
<span  37</span>         <span if</span>(<span $tmpStr</span> == <span $signature</span><span ){
</span><span  38</span>             <span return</span> <span true</span><span ;
</span><span  39</span>         }<span else</span><span {
</span><span  40</span>             <span return</span> <span false</span><span ;
</span><span  41</span> <span         }
</span><span  42</span> <span     }
</span><span  43</span> 
<span  44</span>     <span public</span> <span function</span><span  responseMsg()
</span><span  45</span> <span     {
</span><span  46</span>         <span $postStr</span> = <span $GLOBALS</span>["HTTP_RAW_POST_DATA"<span ];
</span><span  47</span>         <span if</span> (!<span empty</span>(<span $postStr</span><span )){
</span><span  48</span>             <span $this</span>->logger("R ".<span $postStr</span><span );
</span><span  49</span>             <span $postObj</span> = <span simplexml_load_string</span>(<span $postStr</span>, 'SimpleXMLElement',<span  LIBXML_NOCDATA);
</span><span  50</span>             <span $RX_TYPE</span> = <span trim</span>(<span $postObj</span>-><span MsgType);
</span><span  51</span> 
<span  52</span>             <span switch</span> (<span $RX_TYPE</span><span )
</span><span  53</span> <span             {
</span><span  54</span>                 <span case</span> "event":
<span  55</span>                     <span $result</span> = <span $this</span>->receiveEvent(<span $postObj</span><span );
</span><span  56</span>                     <span break</span><span ;
</span><span  57</span>                 <span case</span> "text":
<span  58</span>                     <span $result</span> = <span $this</span>->receiveText(<span $postObj</span><span );
</span><span  59</span>                     <span break</span><span ;
</span><span  60</span> <span             }
</span><span  61</span>             <span $this</span>->logger("T ".<span $result</span><span );
</span><span  62</span>             <span echo</span> <span $result</span><span ;
</span><span  63</span>         }<span else</span><span  {
</span><span  64</span>             <span echo</span> ""<span ;
</span><span  65</span>             <span exit</span><span ;
</span><span  66</span> <span         }
</span><span  67</span> <span     }
</span><span  68</span>     
<span  69</span>     <span private</span> <span function</span> receiveEvent(<span $object</span><span )
</span><span  70</span> <span     {
</span><span  71</span>         <span $content</span> = ""<span ;
</span><span  72</span>         <span switch</span> (<span $object</span>-><span Event)
</span><span  73</span> <span         {
</span><span  74</span>             <span case</span> "subscribe":
<span  75</span>                 <span $content</span> = "欢迎关注方倍工作室 "<span ;
</span><span  76</span>                 <span break</span><span ;
</span><span  77</span>             <span case</span> "unsubscribe":
<span  78</span>                 <span $content</span> = "取消关注"<span ;
</span><span  79</span>                 <span break</span><span ;
</span><span  80</span> <span         }
</span><span  81</span>         <span $result</span> = <span $this</span>->transmitText(<span $object</span>, <span $content</span><span );
</span><span  82</span>         <span return</span> <span $result</span><span ;
</span><span  83</span> <span     }
</span><span  84</span>   
<span  85</span>     <span private</span> <span function</span> receiveText(<span $object</span><span )
</span><span  86</span> <span     {
</span><span  87</span>         <span $keyword</span> = <span trim</span>(<span $object</span>-><span Content);
</span><span  88</span> 
<span  89</span>         <span include</span>("weather.php"<span );
</span><span  90</span>         <span $content</span> = getWeatherInfo(<span $keyword</span><span );
</span><span  91</span>         <span $result</span> = <span $this</span>->transmitNews(<span $object</span>, <span $content</span><span );
</span><span  92</span>         <span return</span> <span $result</span><span ;
</span><span  93</span> <span     }
</span><span  94</span>    
<span  95</span>     
<span  96</span>     <span private</span> <span function</span> transmitText(<span $object</span>, <span $content</span><span )
</span><span  97</span> <span     {
</span><span  98</span>         <span $textTpl</span> = "<span <xml>
</span><span  99</span> <span <ToUserName><![CDATA[%s]]></ToUserName>
</span><span 100</span> <span <FromUserName><![CDATA[%s]]></FromUserName>
</span><span 101</span> <span <CreateTime>%s</CreateTime>
</span><span 102</span> <span <MsgType><![CDATA[text]]></MsgType>
</span><span 103</span> <span <Content><![CDATA[%s]]></Content>
</span><span 104</span> </xml>"<span ;
</span><span 105</span>         <span $result</span> = <span sprintf</span>(<span $textTpl</span>, <span $object</span>->FromUserName, <span $object</span>->ToUserName, <span time</span>(), <span $content</span><span );
</span><span 106</span>         <span return</span> <span $result</span><span ;
</span><span 107</span> <span     }
</span><span 108</span> 
<span 109</span>     <span private</span> <span function</span> transmitNews(<span $object</span>, <span $arr_item</span><span )
</span><span 110</span> <span     {
</span><span 111</span>         <span if</span>(!<span is_array</span>(<span $arr_item</span><span ))
</span><span 112</span>             <span return</span><span ;
</span><span 113</span> 
<span 114</span>         <span $itemTpl</span> = "<span     <item>
</span><span 115</span> <span         <Title><![CDATA[%s]]></Title>
</span><span 116</span> <span         <Description><![CDATA[%s]]></Description>
</span><span 117</span> <span         <PicUrl><![CDATA[%s]]></PicUrl>
</span><span 118</span> <span         <Url><![CDATA[%s]]></Url>
</span><span 119</span> <span     </item>
</span><span 120</span> "<span ;
</span><span 121</span>         <span $item_str</span> = ""<span ;
</span><span 122</span>         <span foreach</span> (<span $arr_item</span> <span as</span> <span $item</span><span )
</span><span 123</span>             <span $item_str</span> .= <span sprintf</span>(<span $itemTpl</span>, <span $item</span>['Title'], <span $item</span>['Description'], <span $item</span>['PicUrl'], <span $item</span>['Url'<span ]);
</span><span 124</span> 
<span 125</span>         <span $newsTpl</span> = "<span <xml>
</span><span 126</span> <span <ToUserName><![CDATA[%s]]></ToUserName>
</span><span 127</span> <span <FromUserName><![CDATA[%s]]></FromUserName>
</span><span 128</span> <span <CreateTime>%s</CreateTime>
</span><span 129</span> <span <MsgType><![CDATA[news]]></MsgType>
</span><span 130</span> <span <Content><![CDATA[]]></Content>
</span><span 131</span> <span <ArticleCount>%s</ArticleCount>
</span><span 132</span> <span <Articles>
</span><span 133</span> <span $item_str</span><span </Articles>
</span><span 134</span> </xml>"<span ;
</span><span 135</span> 
<span 136</span>         <span $result</span> = <span sprintf</span>(<span $newsTpl</span>, <span $object</span>->FromUserName, <span $object</span>->ToUserName, <span time</span>(), <span count</span>(<span $arr_item</span><span ));
</span><span 137</span>         <span return</span> <span $result</span><span ;
</span><span 138</span> <span     }
</span><span 139</span>     
<span 140</span>     <span private</span> <span function</span> logger(<span $log_content</span><span )
</span><span 141</span> <span     {
</span><span 142</span> <span     }
</span><span 143</span> }
Copy after login

4. How to expand

1. Add zip code, area code, pinyin and other fields to the database, which can be expanded to use area code and other methods to query. Please refer to: http://www.cnblogs.com/txw1958/archive/2013/02/07/weixin- if4-weather-forecast.html

2. Users with advanced interfaces can use speech recognition + Chinese word segmentation technology to realize intelligent query of voice version. Please refer to: http://www.cnblogs.com/txw1958/archive/2013/02/06 /weixin-if40-voice-recognition.html

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/738527.htmlTechArticleIn this tutorial, we will introduce how to develop the weather forecast function on the WeChat public platform. We will use the meteorological data interface of China Weather Network to obtain weather information. This tutorial will...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template