php調用阿裏雲天氣預報
weather.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>天氣查詢</title> </head> <body> <form action="weather.php" method="get"> <input type="text" name="area"> <input type="submit" name="查詢"></form> </body> </html>
<input type="text" name="area"> area傳值到php頁面 想要查詢天氣的地名
建一個weather.php文件
內容:
<?php header("Content-Type:text/html;charset=UTF-8"); date_default_timezone_set("PRC"); // $host = "http://ali-weather.showapi.com/ip-to-weather"; //通過ip地址查詢天氣 $host= "http://ali-weather.showapi.com/area-to-weather"; //通過地區名字查詢天氣 $method = "GET"; $appcode = "ff90fe45686e4cb8b3e260f2692798b4";//需要到阿裏雲購買服務,獲取appcode $headers = array(); array_push($headers, "Authorization:APPCODE " . $appcode); array_push($headers, "Content-Type: application/x-www-form-urlencoded; charset=utf-8");$querys="area=".$_GET[‘area‘];//獲取html頁面傳遞過來的地區名字 $bodys = ""; $url = $host . "?" . $querys; $curl = curl_init(); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); curl_setopt($curl, CURLOPT_FAILONERROR, false); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_HEADER, true); if (1 == strpos("$".$host, "https://")) { curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); } curl_setopt($curl, CURLOPT_POSTFIELDS, $bodys); $response = curl_exec($curl); $header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE); //echo $header_size; $headers = substr($response, 0, $header_size); //echo $headers;$body = substr($response, $header_size); //解析 $body對象 $weather=json_decode($body); echo "<img src=".$weather->showapi_res_body->f1->day_weather_pic." /><br>"; echo "城市: ".$weather->showapi_res_body->cityInfo->c5."<br/>"; echo "白天天氣: ".$weather->showapi_res_body->f1->day_weather."<br/>"; echo "晚上天氣: ".$weather->showapi_res_body->f1->night_weather."<br/>"; echo "最低溫度: ".$weather->showapi_res_body->f1->night_air_temperature."<br/>"; echo "最高溫度: ".$weather->showapi_res_body->f1->day_air_temperature."<br/>"; echo "PM2.5: ".$weather->showapi_res_body->now->aqiDetail->pm2_5." ".$weather->showapi_res_body->now->aqiDetail->quality."<br/>"; ?>
效果截圖
難點:主要在解析json返回的格式上面
$body 返回數據:
{"showapi_res_code":0,"showapi_res_error":"","showapi_res_body":{"time":"20170811073000","ret_code":0,"now":{"aqiDetail":{"co":"0.764","num":"166","so2":"7","area":"杭州","o3":"9","no2":"44","quality":"優質","aqi":"47","pm10":"46","pm2_5":"27","o3_8h":"19","primary_pollutant":""},"weather_code":"01","temperature_time":"08:30","wind_direction":"南風","wind_power":"2級","sd":"74%","aqi":"47","weather":"多雲","weather_pic":"http://app1.showapi.com/weather/icon/day/01.png","temperature":"29"},"cityInfo":{"c6":"zhejiang","c5":"杭州","c4":"hangzhou","c3":"杭州","c9":"中國","c8":"china","c7":"浙江","c17":"+8","c16":"AZ9571","c1":"101210101","c2":"hangzhou","longitude":120.165,"c11":"0571","latitude":30.319,"c10":"1","c12":"310000","c15":"43"},"f1":{"day_weather":"多雲","night_weather":"多雲","night_weather_code":"01","air_press":"1004 hPa","jiangshui":"2%","night_wind_power":"微風 <5.4m/s","day_wind_power":"微風 <5.4m/s","day_weather_code":"01","sun_begin_end":"05:23|18:45","ziwaixian":"很強","day_weather_pic":"http://app1.showapi.com/weather/icon/day/01.png","weekday":5,"night_air_temperature":"27","day_air_temperature":"36","day_wind_direction":"東南風","day":"20170811","night_weather_pic":"http://app1.showapi.com/weather/icon/night/01.png","night_wind_direction":"東南風"},"f3":{"day_weather":"多雲","night_weather":"多雲","night_weather_code":"01","air_press":"1004 hPa","jiangshui":"1%","night_wind_power":"3-4級 5.5~7.9m/s","day_wind_power":"3-4級 5.5~7.9m/s","day_weather_code":"01","sun_begin_end":"05:25|18:44","ziwaixian":"很強","day_weather_pic":"http://app1.showapi.com/weather/icon/day/01.png","weekday":7,"night_air_temperature":"28","day_air_temperature":"37","day_wind_direction":"東南風","day":"20170813","night_weather_pic":"http://app1.showapi.com/weather/icon/night/01.png","night_wind_direction":"西南風"},"f2":{"day_weather":"雷陣雨","night_weather":"多雲","night_weather_code":"01","air_press":"1004 hPa","jiangshui":"82%","night_wind_power":"3-4級 5.5~7.9m/s","day_wind_power":"微風 <5.4m/s","day_weather_code":"04","sun_begin_end":"05:24|18:45","ziwaixian":"很強","day_weather_pic":"http://app1.showapi.com/weather/icon/day/04.png","weekday":6,"night_air_temperature":"27","day_air_temperature":"32","day_wind_direction":"東南風","day":"20170812","night_weather_pic":"http://app1.showapi.com/weather/icon/night/01.png","night_wind_direction":"東南風"},"showapi_treemap":true}}
對於新手需要調整好json格式才好進行解析 ,在ide工具上按照{ } 和 ,進行一個層次拆分 然後就比較好解析 記得縮進。
php調用阿裏雲天氣預報