首页 > 微信应用  > 

微信开发

微信开发
这篇文章主要为大家详细介绍了微信开发之百度天气预报的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

这篇文章主要为大家详细介绍了php微信开发之百度天气预报的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了php微信百度天气预报的开发代码,供大家参考,具体内容如下

1.登录百度ak申请:http://www.php.cn/

2.实现天气信息功能

Weather.php 

<?php /** * 使用百度天气预报接口获取城市天气信息案例实现 */ //获取城市天气信息 function getWeatherInfo($cityName){ if($cityName == "" || (strstr($cityName,"+"))){ return "发送城市加天气,例如北京天气"; } //获取到的ak $ak = your ak; //获取到的sk $sk = your sk; //调用接口 $url = &#39;http://api.map.baidu.com/telematics/v3/weather?ak=%s&location=%s&output=%s&sk=%s&#39;; $uri = &#39;/telematics/v3/weather&#39;; $location = $cityName; $output = &#39;json&#39;; $querystring_arrays = array( &#39;ak&#39; => $ak, &#39;location&#39; => $location, &#39;output&#39; => $output ); $querystring = http_build_query($querystring_arrays); //生成sn $sn = md5(urlencode($uri.&#39;?&#39;.$querystring.$sk)); $targetUrl = sprintf($url,$ak,urlencode($location),$output,$sn); $ch = curl_init(); curl_setopt($ch,CURLOPT_URL,$targetUrl); curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); $result = curl_exec($ch); curl_close($ch); $result = json_decode($result,true); if($result["error"]!=0){ return $result["status"]; } $curHour = (int)date(&#39;H&#39;,time()); $weather = $result["results"][0]; $weatherArray[]=array("Title"=>$weather[&#39;currentCity&#39;]."天气预报","Description"=>"","PicUrl"=>"","Url"=>""); for($i = 0;$i<count($weather["weather_data"]);$i++){ $weatherArray[] = array("Title"=> $weather["weather_data"][$i]["data"]."\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 $weatherArray; }

微信开发由讯客互联微信应用栏目发布,感谢您对讯客互联的认可,以及对我们原创作品以及文章的青睐,非常欢迎各位朋友分享到个人网站或者朋友圈,但转载请说明文章出处“微信开发