首页 > 后端开发 > php教程 > 微信公众号天气查询接口实例,公众天气查询实例_PHP教程

微信公众号天气查询接口实例,公众天气查询实例_PHP教程

WBOY
发布: 2016-07-12 08:54:45
原创
1315 人浏览过

微信公众号天气查询接口实例,公众天气查询实例

这是来自百度API的一个天气查询的接口,支持历史7天和未来4天的天气情况查询,接口不错,分享给大家。

接口申请地址:http://apistore.baidu.com/apiworks/servicedetail/112.html

首先,获取一下apikey(貌似需要手机验证码认证下)

我调用的是历史7天和未来4天的接口,接口地址是:

http:<span>//</span><span>apis.baidu.com/apistore/weatherservice/recentweathers</span>
登录后复制

我将这个天气查询的功能集成在了微信公众平台上,支持未来4天天气查询,包括:当前温度、pm值、风向、风力、最高温度、最低温度、天气状态的查询,效果如下:

闲话不多说,我就喜欢来点直接的,核心代码如下:

<span> 1 <span>//<span>接收到文本信息
<span> 2     <span>private <span>function receiveText(<span>$object<span>){
<span> 3         <span>//<span>多个空格缩为一个
<span> 4         <span>$keyword = <span>$object-><span>Content;
<span> 5         <span>$keyword=<span>preg_replace("/\s(?=\s)/","\\1",<span>$keyword<span>);
<span> 6         <span>$funcFlag = 0<span>;
<span> 7         <span>if(<span>strpos(<span>$keyword," "<span>)){
<span> 8             <span>//<span>echo '有空格';
<span> 9             <span>$arr = <span>explode(" ",<span>$keyword<span>);
<span>10             <span>if(<span>$arr[0]=="天气"<span>){
<span>11                 <span>$dateArray = <span>array<span>();
<span>12                 <span>$ch =<span> curl_init();
<span>13                 <span>$city=<span>urlencode(<span>$arr[1<span>]);
<span>14                 <span>$url = 'http://apis.baidu.com/apistore/weatherservice/recentweathers?cityname='.<span>$city<span>;
<span>15                 <span>$header = <span>array<span>(
<span>16                     'apikey: 6512ad3a465e3a9e3d0d1da56639626c',
<span>17 <span>                );
<span>18                 curl_setopt(<span>$ch, CURLOPT_HTTPHEADER  , <span>$header<span>);
<span>19                 curl_setopt(<span>$ch, CURLOPT_RETURNTRANSFER, 1<span>);
<span>20                 curl_setopt(<span>$ch , CURLOPT_URL , <span>$url<span>);
<span>21                 <span>$res = curl_exec(<span>$ch<span>);
<span>22                 <span>$data = json_decode(<span>$res,<span>true<span>);
<span>23                 <span>$cityname=<span>$data['retData']['city'<span>];
<span>24                 <span>$citydate=<span>$data['retData']['today']['date'<span>];
<span>25                 <span>$cityweek=<span>$data['retData']['today']['week'<span>];
<span>26                 <span>$citytemp=<span>$data['retData']['today']['curTemp'<span>];
<span>27                 <span>$citypm=<span>$data['retData']['today']['aqi'<span>];
<span>28                 <span>$cityhightemp=<span>$data['retData']['today']['hightemp'<span>];
<span>29                 <span>$citylowtemp=<span>$data['retData']['today']['lowtemp'<span>];
<span>30                 <span>$citytype=<span>$data['retData']['today']['type'<span>];
<span>31                 <span>$cityfengli=<span>$data['retData']['today']['fengli'<span>];
<span>32                 <span>$cityfengxiang=<span>$data['retData']['today']['fengxiang'<span>];
<span>33                 <span>$cityforecast=<span>$data['retData']['forecast'<span>];
<span>34 
<span>35                 <span>$out="今日温度:".<span>$citylowtemp."~".<span>$cityhightemp."\n\r"."当前温度:".<span>$citytemp."\n\r"."天气情况:".<span>$citytype."\n\r"."PM2.5值:".<span>$citypm."\n\r风向风力:".<span>$cityfengxiang." ".<span>$cityfengli<span>;
<span>36                 <span>$dateArray[] = <span>array("Title"=><span>$cityname."\n\r".<span>$citydate." ".<span>$cityweek,"Description"=>"","Picurl"=>"","Url" =>""<span>);
<span>37                 <span>$dateArray[] = <span>array("Title"=><span>$out,"Description"=>"","Picurl"=>"","Url" =>""<span>);
<span>38                 <span>for(<span>$i=0;<span>$i<<span>count(<span>$cityforecast);<span>$i++<span>){
<span>39                     <span>$outstr="☀ ".<span>$cityforecast[<span>$i]["date"]." ".<span>$cityforecast[<span>$i]["week"]."\n\r气温:".<span>$cityforecast[<span>$i]["lowtemp"]."~".<span>$cityforecast[<span>$i]["hightemp"]."\n\r风力:".<span>$cityforecast[<span>$i]["fengxiang"]." ".<span>$cityforecast[<span>$i]["fengli"]."\n\r天气:".<span>$cityforecast[<span>$i]["type"<span>];
<span>40                     <span>$dateArray[] = <span>array("Title"=><span>$outstr,"Description"=>"","Picurl"=>"","Url" =>""<span>);
<span>41 <span>                }
<span>42                 <span>$resultStr = <span>$this->transmitNews(<span>$object, <span>$dateArray, <span>$funcFlag<span>);
<span>43 <span>            }
<span>44         }<span>else<span>{
<span>45             <span>//<span>echo '没有空格';
<span>46             if($keyword=="天气"){
47                 $dateArray = array();
48                 $dateArray[] = array("Title"=>"查询格式错误","Description"=>"","Picurl"=>"","Url" =>"");
49                 $dateArray[] = array("Title"=>"正确格式例:天气 北京","Description"=>"","Picurl"=>"","Url" =>"");
50                 $resultStr = $this->transmitNews($object, $dateArray, $funcFlag);
51             }
52         }
53         return $resultStr; 
54     }</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
登录后复制

查询格式:“天气 城市名”

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1119056.htmlTechArticle微信公众号天气查询接口实例,公众天气查询实例 这是来自百度API的一个天气查询的接口,支持历史7天和未来4天的天气情况查询,接口不...
相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板