php调用阿里云天气预报
Posted open88
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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调用阿里云天气预报的主要内容,如果未能解决你的问题,请参考以下文章