您的位置:首页 > 其它

易信公众平台开发从入门到精通之天气预报

2014-06-03 18:24 141 查看

天气预报

易信公众平台支持图文消息的返回,今天我们就说说如何使用易信公众平台实现一个图文天气预报。

 

天气预报API接口

 

1百度天气API

百度天气预报API接口可以提供当天以及未来三天的天气预报,其URL请求地址为:

“http://api.map.baidu.com/telematics/v3/weather?location=乌鲁木齐&output=xml& ak=08af1b05fd08a6d1f15bbd0d062c1127”,我们对其中的参数进行简单的说明:location参数为必填项,表示要进行天气预报的城市,可以是城市名,也可以是经纬度信息,如“location=87.3624,43.4687”;output参数表示输出的数据格式,当output设置为“json”时,输出的为json格式的数据,不设置或者设置为“xml”时,输出xml格式的数据;ak参数为用户注册的百度接口的秘钥,读者可以使用本例中的秘钥,也可以自己申请秘钥,百度官方秘钥申请地址:http://lbsyun.baidu.com/apiconsole/key?application=key。
       我们将该URL地址键入浏览器的地址栏中,回车,会得到接口返回给我们的数据,数据内容如下,
  <?xmlversion="1.0" encoding="utf-8" ?>

- <CityWeatherResponse>

 <status>success</status>

 <date>2014-04-12</date>

- <results>

 <currentCity>乌鲁木齐市</currentCity>

- <weather_data>

 <date>周六(今天, 实时:12℃)</date>

 <dayPictureUrl>http://api.map.baidu.com/images/weather/day/duoyun.png</dayPictureUrl>

 <nightPictureUrl>http://api.map.baidu.com/images/weather/night/duoyun.png</nightPictureUrl>

 <weather>多云</weather>

 <wind>西北风微风</wind>

  <temperature>17~ 8℃</temperature>

 <date>周日</date>

 <dayPictureUrl>http://api.map.baidu.com/images/weather/day/duoyun.png</dayPictureUrl>

 <nightPictureUrl>http://api.map.baidu.com/images/weather/night/zhongyu.png</nightPictureUrl>

 <weather>多云转中雨</weather>

 <wind>微风</wind>

 <temperature>17 ~ 3℃</temperature>

 <date>周一</date>

 <dayPictureUrl>http://api.map.baidu.com/images/weather/day/dayu.png</dayPictureUrl>

 <nightPictureUrl>http://api.map.baidu.com/images/weather/night/dayu.png</nightPictureUrl>

 <weather>中到大雨</weather>

 <wind>西北风4-5级</wind>

 <temperature>6 ~ 1℃</temperature>

 <date>周二</date>

 <dayPictureUrl>http://api.map.baidu.com/images/weather/day/xiaoyu.png</dayPictureUrl>

 <nightPictureUrl>http://api.map.baidu.com/images/weather/night/duoyun.png</nightPictureUrl>

 <weather>小雨转多云</weather>

 <wind>西北风3-4级</wind>

 <temperature>9 ~ 1℃</temperature>

 </weather_data>

 </results>

 </CityWeatherResponse>

将output参数改为“json”,则返回的json格式数据内容如下:

{

   "error":0,

   "status":"success",

   "date":"2014-04-12",

   "results":[

        {

           "currentCity":"\u4e4c\u9c81\u6728\u9f50",

           "weather_data":[

               {

                   "date":"\u5468\u516d(\u4eca\u5929,\u5b9e\u65f6\uff1a12\u2103)",

                    "dayPictureUrl":"http:\/\/api.map.baidu.com\/images\/weather\/day\/duoyun.png",

                   "nightPictureUrl":"http:\/\/api.map.baidu.com\/images\/weather\/night\/duoyun.png",

                   "weather":"\u591a\u4e91",

                   "wind":"\u897f\u5317\u98ce\u5fae\u98ce",

                   "temperature":"17 ~ 8\u2103"

               },

               {

                   "date":"\u5468\u65e5",

                   "dayPictureUrl":"http:\/\/api.map.baidu.com\/images\/weather\/day\/duoyun.png",

                   "nightPictureUrl":"http:\/\/api.map.baidu.com\/images\/weather\/night\/zhongyu.png",

                   "weather":"\u591a\u4e91\u8f6c\u4e2d\u96e8",

                   "wind":"\u5fae\u98ce",

                   "temperature":"17 ~ 3\u2103"

               },

               {

                   "date":"\u5468\u4e00",

                   "dayPictureUrl":"http:\/\/api.map.baidu.com\/images\/weather\/day\/dayu.png",

                   "nightPictureUrl":"http:\/\/api.map.baidu.com\/images\/weather\/night\/dayu.png",

                   "weather":"\u4e2d\u5230\u5927\u96e8",

                   "wind":"\u897f\u5317\u98ce4-5\u7ea7",

                   "temperature":"6 ~ 1\u2103"

               },

               {

                    "date":"\u5468\u4e8c",

                   "dayPictureUrl":"http:\/\/api.map.baidu.com\/images\/weather\/day\/xiaoyu.png",

                   "nightPictureUrl":"http:\/\/api.map.baidu.com\/images\/weather\/night\/duoyun.png",

                   "weather":"\u5c0f\u96e8\u8f6c\u591a\u4e91",

                   "wind":"\u897f\u5317\u98ce3-4\u7ea7",

                   "temperature":"9 ~ 1\u2103"

               }

            ]

        }

    ]

}

返回的参数较多,我们列出如下表格说明:

 

参数名称
参数含义
error
错误信息
status
返回结果状态信息
date
查询的当前时间
results
currentCity
当前预报的城市
weather_data
date
天气预报时间
dayPictureUrl
白天的天气预报图片url
nightPictureUrl
晚上的天气预报图片url
weather
天气情况
wind
风力
temperature
温度
 

 

封装天气请求类

我们新增一个天气请求类WeatherQuery,此类只有一个方法getWeatherMsg,实现通过城市关键字调用百度天气预报API接口,将返回的结果格式化,用于我们易信消息的返回,代码如下:

class WeatherQuery

{

         publicfunction getWeatherMsg($cityName){

        //如果城市名不为空

       if(!empty($cityName)){

           $weatherUrl="http://api.map.baidu.com/telematics/v3/weather?location=".$cityName."&output=json&ak=08af1b05fd08a6d1f15bbd0d062c1127";

               $apiReturnStr=file_get_contents($weatherUrl);

               $weatherArray =json_decode($apiReturnStr,true);

           

               //print_r( $weatherArray);

               if($weatherArray[status]=='success'){

                    $weatherInfo= $weatherArray[results][0][weather_data];

                    $reContentArr[] =array('Title'=>$weatherArray[results][0][currentCity]."天气预报","Description"=>"", "PicUrl"=>"","Url" =>"");

                              $maxDay =4;//返回三天的天气预报

                    for ($i = 0; $i < $maxDay; $i++){

                   $offset = strtotime("+".$i." day");

                         $subTitle = date("m月d日",$offset)." ".$weatherInfo[$i][date]."".$weatherInfo[$i][weather]." ".$weatherInfo[$i][wind]."".$weatherInfo[$i][temperature];

                         $reContentArr[] =array("Title" =>$subTitle, "Description"=>"", "PicUrl" =>$weatherInfo[$i][dayPictureUrl],"Url" =>"");//这里我们使用白天的天气图片

                    }

               return $reContentArr;

           }

        }

       

    }

}

 

 

调用代码

以上我们已经将主要功能封装完了,测试代码:

     <?php

require_once dirname(__FILE__) . '/YiXinCore.class.php';

class MyYiXin extends YiXinCore{

     protected functiononTextReceive(){

              $content =$this->getDataVal("content");

              $keyarr = explode("",$content);

 

              switch($keyarr[0]) {

                       case '点歌':

                                 $this->song($keyarr[1]);

                                 break;

                       case '天气':

                                 $this->weatherInter($keyarr[1]);  //天气预报

                                 break;

                       case '空气':

                                 $this->airQualityInter($keyarr[1]);  //空气质量

                                 break;

其他代码省略…

}

 

         //天气查询

         privatefunction weatherInter($city){

                   require_oncedirname(__FILE__) . '/WeatherQuery.class.php';

                   $weather_query= new WeatherQuery();

                   $weatherInfo= $weather_query->getWeatherMsg($city);

                   $this-> responseNewsMsg($weatherInfo);

         }

 

到此,我们的易信图文天气预报就已经基本实现了,我们将代码部署到服务器上,关联公众号URL后,就可以做测试了。

显示效果如下:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐