现在网络上用的中国天气网的接口已经过期,需要付费,于是我做了个新的。
先到http://www.heweather.com/documents/api 免费申请 你的认证key
1 $url = ‘https://api.heweather.com/x3/weather?cityid=‘.$city_id.‘&key=XXX‘; 2 $content=file_get_contents($url); 3 $data=json_decode($content,true); 4 // $data = $this->weather($city_id); 5 $contentStr = "【".$area."天气情况】"."\n"; 6 $contentStr .= "空气质量指数(AQI):"."\n"; 7 if($data[‘HeWeather data service 3.0‘][0][aqi][city][co])$contentStr .= "一氧化碳CO:".$data[‘HeWeather data service 3.0‘][0][aqi][city][co]."\n"; 8 if($data[‘HeWeather data service 3.0‘][0][aqi][city][no2])$contentStr .= "二氧化氮NO2:".$data[‘HeWeather data service 3.0‘][0][aqi][city][no2]."\n"; 9 if($data[‘HeWeather data service 3.0‘][0][aqi][city][so2])$contentStr .= "二氧化硫SO2:".$data[‘HeWeather data service 3.0‘][0][aqi][city][so2]."\n"; 10 if($data[‘HeWeather data service 3.0‘][0][aqi][city][o3])$contentStr .= "臭氧O3:".$data[‘HeWeather data service 3.0‘][0][aqi][city][o3]."\n"; 11 if($data[‘HeWeather data service 3.0‘][0][aqi][city][pm10])$contentStr .= "PM10:".$data[‘HeWeather data service 3.0‘][0][aqi][city][pm10]."\n"; 12 if($data[‘HeWeather data service 3.0‘][0][aqi][city][pm25])$contentStr .= "PM25:".$data[‘HeWeather data service 3.0‘][0][aqi][city][pm25]."\n"; 13 if($data[‘HeWeather data service 3.0‘][0][aqi][city][qlty])$contentStr .= "空气评级:".$data[‘HeWeather data service 3.0‘][0][aqi][city][qlty]."\n"; 14 $contentStr .= "\n"."天气情况:"."\n"; 15 for($x=0;$x<6;$x++){ 16 $contentStr .=$data[‘HeWeather data service 3.0‘][0][‘daily_forecast‘][$x][‘date‘]."\n"; 17 $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"; 18 $contentStr .="湿度:".$data[‘HeWeather data service 3.0‘][0][‘daily_forecast‘][$x][hum]."%"."\n"; 19 $contentStr .="气压:".$data[‘HeWeather data service 3.0‘][0][‘daily_forecast‘][$x][pres]."\n"; 20 $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"; 21 $contentStr .="能见度:".$data[‘HeWeather data service 3.0‘][0][‘daily_forecast‘][$x][vis]."KM"."\n"; 22 $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"; 23 } 24 $contentStr .= "\n"."生活建议:"."\n"; 25 $contentStr .= "舒适度:".$data[‘HeWeather data service 3.0‘][0][suggestion][comf][brf].",".$data[‘HeWeather data service 3.0‘][0][suggestion][comf][txt]."\n"; 26 $contentStr .= "洗车建议:".$data[‘HeWeather data service 3.0‘][0][suggestion][cw][brf].",".$data[‘HeWeather data service 3.0‘][0][suggestion][cw][txt]."\n"; 27 $contentStr .= "穿着建议:".$data[‘HeWeather data service 3.0‘][0][suggestion][drsg][brf].",".$data[‘HeWeather data service 3.0‘][0][suggestion][drsg][txt]."\n"; 28 $contentStr .= "感冒指数:".$data[‘HeWeather data service 3.0‘][0][suggestion][flu][brf].",".$data[‘HeWeather data service 3.0‘][0][suggestion][flu][txt]."\n"; 29 $contentStr .= "运动指数:".$data[‘HeWeather data service 3.0‘][0][suggestion][sport][brf].",".$data[‘HeWeather data service 3.0‘][0][suggestion][sport][txt]."\n"; 30 $contentStr .= "旅游指数:".$data[‘HeWeather data service 3.0‘][0][suggestion][trav][brf].",".$data[‘HeWeather data service 3.0‘][0][suggestion][trav][txt]."\n"; 31 $contentStr .= "辐射指数:".$data[‘HeWeather data service 3.0‘][0][suggestion][uv][brf].",".$data[‘HeWeather data service 3.0‘][0][suggestion][uv][txt]."\n";
CITYID 可以根据这个网站建立数据库
1 if(!$str_key)$str_key=‘上海‘; 2 $sql_tq="SELECT city_id,city_name,pro_name FROM tianqi 3 WHERE town_name=‘".$str_key."‘"; 4 $result_tq=mysql_query($sql_tq,$dbconn); 5 $count_tq = mysql_num_rows($result_tq); 6 if($count_tq>0) 7 { 8 $row_tq = mysql_fetch_array($result_tq); 9 $city_id = $row_tq[‘city_id‘]; 10 $city_name=$row_tq[‘city_name‘]; 11 $pro_name=$row_tq[‘pro_name‘]; 12 if($city_name==$pro_name AND $city_name==$str_key){ 13 $area=$str_key; 14 } 15 elseif($city_name==$pro_name AND $city_name!=$str_key){ 16 $area=$city_name." ".$str_key; 17 } 18 else{ 19 $area=$pro_name." ".$city_name." ".$str_key; 20 }