1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > php微信公众号天气预报 微信公众号平台天气预报开发

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

时间:2020-09-16 10:19:24

相关推荐

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 = "/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 = "/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" =>"/weixin/weather/"."d".sprintf("%02u",$info['img'.(($i *2)-1)]).".jpg", "Url" =>"");

}

return $weatherArray;

}

?>

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。