您的位置:首页 > 移动开发 > 微信开发

微信公众号平台天气预报开发

2017-05-08 21:45 99 查看
获取天气的代码,然后方法放在微信中调用便可

<?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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: