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

微信开发中查询天气

2015-08-18 13:11 561 查看
现在网络上用的中国天气网的接口已经过期,需要付费,于是我做了个新的。

先到http://www.heweather.com/documents/api 免费申请 你的认证key

$url = 'https://api.heweather.com/x3/weather?cityid='.$city_id.'&key=XXX';
$content=file_get_contents($url);
$data=json_decode($content,true);
//                                    $data = $this->weather($city_id);
$contentStr  = "【".$area."天气情况】"."\n";
$contentStr .= "空气质量指数(AQI):"."\n";
if($data['HeWeather data service 3.0'][0][aqi][city][co])$contentStr .= "一氧化碳CO:".$data['HeWeather data service 3.0'][0][aqi][city][co]."\n";
if($data['HeWeather data service 3.0'][0][aqi][city][no2])$contentStr .= "二氧化氮NO2:".$data['HeWeather data service 3.0'][0][aqi][city][no2]."\n";
if($data['HeWeather data service 3.0'][0][aqi][city][so2])$contentStr .= "二氧化硫SO2:".$data['HeWeather data service 3.0'][0][aqi][city][so2]."\n";
if($data['HeWeather data service 3.0'][0][aqi][city][o3])$contentStr .= "臭氧O3:".$data['HeWeather data service 3.0'][0][aqi][city][o3]."\n";
if($data['HeWeather data service 3.0'][0][aqi][city][pm10])$contentStr .= "PM10:".$data['HeWeather data service 3.0'][0][aqi][city][pm10]."\n";
if($data['HeWeather data service 3.0'][0][aqi][city][pm25])$contentStr .= "PM25:".$data['HeWeather data service 3.0'][0][aqi][city][pm25]."\n";
if($data['HeWeather data service 3.0'][0][aqi][city][qlty])$contentStr .= "空气评级:".$data['HeWeather data service 3.0'][0][aqi][city][qlty]."\n";
$contentStr .= "\n"."天气情况:"."\n";
for($x=0;$x<6;$x++){
$contentStr .=$data['HeWeather data service 3.0'][0]['daily_forecast'][$x]['date']."\n";
$contentStr .=$data['HeWeather data service 3.0'][0]['daily_forecast'][$x][cond][txt_d]."转".$data['HeWeather data service 3.0'][0]['daily_forecast'][$x][cond][txt_n]."\n";
$contentStr .="湿度:".$data['HeWeather data service 3.0'][0]['daily_forecast'][$x][hum]."%"."\n";
$contentStr .="气压:".$data['HeWeather data service 3.0'][0]['daily_forecast'][$x][pres]."\n";
$contentStr .="温度: ".$data['HeWeather data service 3.0'][0]['daily_forecast'][$x][tmp]['min']."~".$data['HeWeather data service 3.0'][0]['daily_forecast'][$x][tmp]['max']."摄氏度"."\n";
$contentStr .="能见度:".$data['HeWeather data service 3.0'][0]['daily_forecast'][$x][vis]."KM"."\n";
$contentStr .="风力:".$data['HeWeather data service 3.0'][0]['daily_forecast'][$x][wind][sc].", ".$data['HeWeather data service 3.0'][0]['daily_forecast'][$x]['wind']['dir'].", 风速:".$data['HeWeather data service 3.0'][0]['daily_forecast'][$x]['wind']['spd']."\n";
}
$contentStr .= "\n"."生活建议:"."\n";
$contentStr .= "舒适度:".$data['HeWeather data service 3.0'][0][suggestion][comf][brf].",".$data['HeWeather data service 3.0'][0][suggestion][comf][txt]."\n";
$contentStr .= "洗车建议:".$data['HeWeather data service 3.0'][0][suggestion][cw][brf].",".$data['HeWeather data service 3.0'][0][suggestion][cw][txt]."\n";
$contentStr .= "穿着建议:".$data['HeWeather data service 3.0'][0][suggestion][drsg][brf].",".$data['HeWeather data service 3.0'][0][suggestion][drsg][txt]."\n";
$contentStr .= "感冒指数:".$data['HeWeather data service 3.0'][0][suggestion][flu][brf].",".$data['HeWeather data service 3.0'][0][suggestion][flu][txt]."\n";
$contentStr .= "运动指数:".$data['HeWeather data service 3.0'][0][suggestion][sport][brf].",".$data['HeWeather data service 3.0'][0][suggestion][sport][txt]."\n";
$contentStr .= "旅游指数:".$data['HeWeather data service 3.0'][0][suggestion][trav][brf].",".$data['HeWeather data service 3.0'][0][suggestion][trav][txt]."\n";
$contentStr .= "辐射指数:".$data['HeWeather data service 3.0'][0][suggestion][uv][brf].",".$data['HeWeather data service 3.0'][0][suggestion][uv][txt]."\n";


CITYID 可以根据这个网站建立数据库

                 if(!$str_key)$str_key='上海';
$sql_tq="SELECT city_id,city_name,pro_name FROM tianqi
WHERE town_name='".$str_key."'";
$result_tq=mysql_query($sql_tq,$dbconn);
$count_tq = mysql_num_rows($result_tq);
if($count_tq>0)
{
$row_tq = mysql_fetch_array($result_tq);
$city_id = $row_tq['city_id'];
$city_name=$row_tq['city_name'];
$pro_name=$row_tq['pro_name'];
if($city_name==$pro_name AND $city_name==$str_key){
$area=$str_key;
}
elseif($city_name==$pro_name AND $city_name!=$str_key){
$area=$city_name." ".$str_key;
}
else{
$area=$pro_name." ".$city_name." ".$str_key;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: