微信公众号平台天气预报开发
2017-05-08 21:45
99 查看
获取天气的代码,然后方法放在微信中调用便可
参考链接:http://www.cnblogs.com/txw1958/p/weixin-weather-forecast.html
<?php function httpRequest($url) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($ch); curl_close($ch); if ($output === FALSE){ return "cURL Error: ". curl_error($ch); } return $output; } function getcity($add){ include 'conn.inc.php'; //调用连接数据库的php文件 mysql_set_charset("utf8"); mysql_select_db($dbname); mysql_query("set names utf8"); $sql = "select cityCode from weather where cityName = '$add'"; $result = mysql_query($sql); $row = mysql_fetch_assoc($result); return $row['cityCode']; } //根据citycode获取天气接口的数据 function getWeatherInfo($cityCode){ //获取实时天气 $url = "http://www.weather.com.cn/data/sk/".$cityCode.".html"; $output = httpRequest($url); $weather = json_decode($output, true); $info = $weather['weatherinfo']; $weatherArray = array(); $weatherArray[] = array("Title"=>$info['city']."现在的天气预报", "Description"=>"", "PicUrl"=>"", "Url" =>""); if ((int)$cityCode < 101340000){ $result = "实况 温度:".$info['temp']."℃ 湿度:".$info['SD']." 风速:".$info['WD'].$info['WSE']."级"; $weatherArray[] = array("Title"=>str_replace("%", "﹪", $result), "Description"=>"", "PicUrl"=>"", "Url" =>""); } //获取六日天气 $url = "http://m.weather.com.cn/data/".$cityCode.".html"; $output = httpRequest($url); $weather = json_decode($output, true); $info = $weather['weatherinfo']; if (!empty($info['index_d'])){ $weatherArray[] = array("Title" =>$info['index_d'], "Description" =>"", "PicUrl" =>"", "Url" =>""); } $weekArray = array("日","一","二","三","四","五","六"); $maxlength = 3; for ($i = 1; $i <= $maxlength; $i++) { $offset = strtotime("+".($i-1)." day"); $subTitle = date("m月d日",$offset)." 周".$weekArray[date('w',$offset)]." ".$info['temp'.$i]." ".$info['weather'.$i]." ".$info['wind'.$i]; $weatherArray[] = array("Title" =>$subTitle, "Description" =>"", "PicUrl" =>"http://discuz.comli.com/weixin/weather/"."d".sprintf("%02u",$info['img'.(($i *2)-1)]).".jpg", "Url" =>""); } return $weatherArray; } ?>
参考链接:http://www.cnblogs.com/txw1958/p/weixin-weather-forecast.html
相关文章推荐
- 微信公众号开发之微信公共平台消息回复类实例
- 微信公众号开发之平台的选取------通过花生壳软件实现
- 微信公众号第三方平台开发PYTHON教程 PART 9
- 5.使用 公众平台测试账号 进行开发(微信公众号开发实战)
- 微信公众号红包接口开发PHP开发 CA证书出错,请登陆微信支付商户平台下载证书
- 基于phpcms v9开发的微信公众号平台管理模块
- 微信公众号平台开发(二)信息的分类.md
- 微信公众号第三方平台开发PYTHON教程 PART 7
- 微信公众号第三方平台开发PYTHON教程 PART 6
- 微信公众号第三方平台开发坑
- 微信公众号平台之自定义菜单开发
- 微信公众号第三方开发之一创建微信公众号第三方平台
- 微信公众号第三方平台开发概况
- 微信公众号第三方平台开发PYTHON教程 PART 8
- 微信公众号第三方平台开发PYTHON教程 PART 3
- 微信公众号红包接口开发PHP开发 CA证书出错,请登陆微信支付商户平台下载证书
- 网络架构、云平台和微信公众号开发接入--基于嵌入式工程师的视觉
- 微信公众号第三方平台开发PYTHON教程 PART 1
- 微信公众号第三方平台开发笔记--02获取component_verify_ticket
- 微信公众号开发平台教程