基于PHP的聚合数据全国天气预报API服务请求的代码样例
本代码示例是基于PHP的聚合数据全国天气预报API服务请求的代码样例,使用前你需要:
①:通过https://www.juhe.cn/docs/api/id/39 申请一个天气预报API的appkey
样例代码包含了获取支持城市列表、根据城市获取天气预报、根据IP地址请求天气预报、根据GPS坐标请求天气、城市3小时天气预报的实现。示例代码主要是解析一些常用字段,如需要完整或其他未包含的字段,可以自行参考官方的接口,进行修改。
首先:引入封装好的天气调用类 header('Content-type:text/html;charset=utf-8'); <br>
include 'class.juhe.weather.php'; //引入天气请求类 <br>
<br>
//接口基本信息配置 <br>
$appkey = '**********'; //您申请的天气查询appkey <br>
$weather = new weather($appkey);
一、获取支持的城市列表
由于支持的城市列表基本不会这么变化,大家可以获取到列表后内置到自己的应用中,就不用每次都去请求API。 $citysResult = $weather->getCitys(); <br>
if($citysResult['error_code'] == 0){ //以下可根据实际业务需求,自行改写 <br>
////////////////////////////////////////////////////////////////////// <br>
$citys = $citysResult['result']; <br>
foreach($citys as $ckey =>$c){ <br>
echo "ID:".$c['id'].",省份:".$c['province'].",城市:".$c['city'].",区域:".$c['district']."<br>"; <br>
} <br>
}else{ //以下可根据实际业务需求,自行改写 <br>
////////////////////////////////////////////////////////////////////// <br>
echo $citysResult['error_code'].":".$citysResult['reason']; <br>
}
二、根据城市/ID获取天气预报
通过城市的名称或城市的ID来获取天气预报,城市id就是获取城市支持列表中返回的字段ID $cityWeatherResult = $weather->getWeather('苏州'); <br>
if($cityWeatherResult['error_code'] == 0){ //以下可根据实际业务需求,自行改写 <br>
////////////////////////////////////////////////////////////////////// <br>
$data = $cityWeatherResult['result']; <br>
echo "=======当前天气实况=======<br>"; <br>
echo "温度:".$data['sk']['temp']." "; <br>
echo "风向:".$data['sk']['wind_direction']." (".$data['sk']['wind_strength'].")"; <br>
echo "湿度:".$data['sk']['humidity']." "; <br>
echo "<br><br>"; <br>
<br>
echo "=======未来几天天气预报=======<br>"; <br>
foreach($data['future'] as $wkey =>$f){ <br>
echo "日期:".$f['date']." ".$f['week']." ".$f['weather']." ".$f['temperature']."<br>"; <br>
} <br>
echo "<br><br>"; <br>
<br>
echo "=======相关天气指数=======<br>"; <br>
echo "穿衣指数:".$data['today']['dressing_index']." , ".$data['today']['dressing_advice']."<br>"; <br>
echo "紫外线强度:".$data['today']['uv_index']."<br>"; <br>
echo "舒适指数:".$data['today']['comfort_index']."<br>"; <br>
echo "洗车指数:".$data['today']['wash_index']; <br>
echo "<br><br>"; <br>
<br>
}else{ <br>
echo $cityWeatherRe
三、根据用户的IP地址请求对应的天气预报
通过用户的IP地址获取用户所在地的天气预报,由于IP地址解析可能会有误差,所以有时定位到的城市不一定是用户实际的所在地。 $ipWeatherResult = $weather->getWeatherByIP('58.215.154.128'); <br>
if($ipWeatherResult['error_code'] == 0){ //以下可根据实际业务需求,自行改写 <br>
////////////////////////////////////////////////////////////////////// <br>
$data = $ipWeatherResult['result']; <br>
echo "=======当前城市=======<br>"; <br>
echo $data['today']['city']; <br>
echo "<br><br>"; <br>
echo "=======当前天气实况=======<br>"; <br>
echo "温度:".$data['sk']['temp']." "; <br>
echo "风向:".$data['sk']['wind_direction']." (".$data['sk']['wind_strength'].")"; <br>
echo "湿度:".$data['sk']['humidity']." "; <br>
echo "<br><br>"; <br>
<br>
echo "=======未来几天天气预报=======<br>"; <br>
foreach($data['future'] as $wkey =>$f){ <br>
echo "日期:".$f['date']." ".$f['week']." ".$f['weather']." ".$f['temperature']."<br>"; <br>
} <br>
echo "<br><br>"; <br>
<br>
echo "=======相关天气指数=======<br>"; <br>
echo "穿衣指数:".$data['today']['dressing_index']." , ".$data['today']['dressing_advice']."<br>"; <br>
echo "紫外线强度:".$data['today']['uv_index']."<br>"; <br>
echo "舒适指数:".$data['today']['comfort_index']."<br>"; <br>
echo "洗车指数:".$data['today']['wash_index']; <br>
echo "<br><br>"; <br>
<br>
}else{ <br>
echo $ipWeatherResult['error_code'].":".$ipWeatherResult['reason']; <br>
}
四、根据GPS坐标来获取对应地区的天气
无论通过二、三、四获取的天气预报,因为聚合格式都是统一的,所以解析的流程是一致的,所以没有额外的操作,只是传参上有点的差异。 $geoWeatherResult = $weather->getWeatherByGeo(116.401394,39.916042); <br>
if($geoWeatherResult['error_code'] == 0){ //以下可根据实际业务需求,自行改写 <br>
////////////////////////////////////////////////////////////////////// <br>
$data = $geoWeatherResult['result']; <br>
echo "=======当前城市=======<br>"; <br>
echo $data['today']['city']; <br>
echo "<br><br>"; <br>
echo "=======当前天气实况=======<br>"; <br>
echo "温度:".$data['sk']['temp']." "; <br>
echo "风向:".$data['sk']['wind_direction']." (".$data['sk']['wind_strength'].")"; <br>
echo "湿度:".$data['sk']['humidity']." "; <br>
echo "<br><br>"; <br>
<br>
echo "=======未来几天天气预报=======<br>"; <br>
foreach($data['future'] as $wkey =>$f){ <br>
echo "日期:".$f['date']." ".$f['week']." ".$f['weather']." ".$f['temperature']."<br>"; <br>
} <br>
echo "<br><br>"; <br>
<br>
echo "=======相关天气指数=======<br>"; <br>
echo "穿衣指数:".$data['today']['dressing_index']." , ".$data['today']['dressing_advice']."<br>"; <br>
echo "紫外线强度:".$data['today']['uv_index']."<br>"; <br>
echo "舒适指数:".$data['today']['comfort_index']."<br>"; <br>
echo "洗车指数:".$data['today']['wash_index']; <br>
echo "<br><br>"; <br>
<br>
}else{ <br>
echo $geoWeatherResult['error_code'].":".$geoWeatherResult['reason']; <br>
}
五、获取城市三小时预报
就是城市每3小时的天气情况 $forecastResult = $weather->getForecast("苏州"); <br>
if($forecastResult['error_code'] == 0){ //以下可根据实际业务需求,自行改写 <br>
////////////////////////////////////////////////////////////////////// <br>
$data = $forecastResult['result']; <br>
foreach($data as $key => $d){ <br>
echo "日期:".$d['date']." (".$d['sh']."点-".$d['eh']."点) ".$d['weather']." ".$d['temp1']."~".$d["temp2"]."<br>"; <br>
} <br>
}else{ //以下可根据实际业务需求,自行改写 <br>
////////////////////////////////////////////////////////////////////// <br>
echo $forecastResult['error_code'].":".$forecastResult['reason']; <br>
}
通过上面的示例代码,大家应该对如果调用聚合数据天气预报API有了一个大体的了解。
最后放上class.juhe.weather.php完整代码: <!--?php <br />
// +---------------------------------------------------------------------- <br />
// | JuhePHP [ NO ZUO NO DIE ] <br />
// +---------------------------------------------------------------------- <br />
// | Copyright (c) 2010-2015 http://juhe.cn All rights reserved. <br />
// +---------------------------------------------------------------------- <br />
// | Author: Juhedata <info@juhe.cn--> <br>
// +---------------------------------------------------------------------- <br>
<br>
//---------------------------------- <br>
// 聚合数据天气预报接口请求类 <br>
//---------------------------------- <br>
class weather{ <br>
private $appkey = false; //申请的聚合天气预报APPKEY <br>
<br>
private $cityUrl = 'http://v.juhe.cn/weather/citys'; //城市列表API URL <br>
<br>
private $weatherUrl = 'http://v.juhe.cn/weather/index'; //根据城市请求天气API URL <br>
<br>
private $weatherIPUrl = 'http://v.juhe.cn/weather/ip'; //根据IP地址请求天气API URL <br>
<br>
private $weatherGeoUrl = 'http://v.juhe.cn/weather/geo'; //根据GPS坐标获取天气API URL <br>
<br>
private $forecast3hUrl = 'http://v.juhe.cn/weather/forecast3h'; //获取城市天气3小时预报API URL <br>
<br>
public function __construct($appkey){ <br>
$this->appkey = $appkey; <br>
} <br>
<br>
/** <br>
* 获取天气预报支持城市列表 <br>
* @return array <br>
*/ <br>
public function getCitys(){ <br>
$params = 'key='.$this->appkey; <br>
$content = $this->juhecurl($this->cityUrl,$params); <br>
return $this->_returnArray($content); <br>
} <br>
<br>
/** <br>
* 根据城市名称/ID获取详细天气预报 <br>
* @param string $city [城市名称/ID] <br>
* @return array <br>
*/ <br>
public function getWeather($city){ <br>
$paramsArray = array( <br>
'key' => $this->appkey, <br>
'cityname' => $city, <br>
'format' => 2 <br>
); <br>
$params = http_build_query($paramsArray); <br>
$content = $this->juhecurl($this->weatherUrl,$params); <br>
return $this->_returnArray($content); <br>
} <br>
<br>
/** <br>
* 根据IP地址获取当地天气预报 <br>
* @param string $ip [IP地址] <br>
* @return array <br>
*/ <br>
public function getWeatherByIP($ip){ <br>
$paramsArray = array( <br>
'key' => $this->appkey, <br>
'ip' => $ip, <br>
'format' => 2 <br>
); <br>
$params = http_build_query($paramsArray); <br>
$content = $this->juhecurl($this->weatherIPUrl,$params); <br>
return $this->_returnArray($content); <br>
} <br>
<br>
/** <br>
* 根据GPS坐标获取当地的天气预报 <br>
* @param string $lon [经度] <br>
* @param string $lat [纬度] <br>
* @return array <br>
*/ <br>
public function getWeatherByGeo($lon,$lat){ <br>
$paramsArray = array( <br>
'key' => $this->appkey, <br>
'lon' => $lon, <br>
'lat' => $lat, <br>
'format' => 2 <br>
); <br>
$params = http_build_query($paramsArray); <br>
$content = $this->juhecurl($this->weatherGeoUrl,$params); <br>
return $this->_returnArray($content); <br>
} <br>
<br>
/** <br>
* 获取城市三小时预报 <br>
* @param string $city [城市名称] <br>
* @return array <br>
*/ <br>
public function getForecast($city){ <br>
$paramsArray = array( <br>
'key' => $this->appkey, <br>
'cityname' => $city, <br>
'format' => 2 <br>
); <br>
$params = http_build_query($paramsArray); <br>
$content = $this->juhecurl($this->forecast3hUrl,$params); <br>
return $this->_returnArray($content); <br>
} <br>
<br>
/** <br>
* 将JSON内容转为数据,并返回 <br>
* @param string $content [内容] <br>
* @return array <br>
*/ <br>
public function _returnArray($content){ <br>
return json_decode($content,true); <br>
} <br>
<br>
/** <br>
* 请求接口返回内容 <br>
* @param string $url [请求的URL地址] <br>
* @param string $params [请求的参数] <br>
* @param int $ipost [是否采用POST形式] <br>
* @return string <br>
*/ <br>
public function juhecurl($url,$params=false,$ispost=0){ <br>
$httpInfo = array(); <br>
$ch = curl_init(); <br>
<br>
curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 ); <br>
curl_setopt( $ch, CURLOPT_USERAGENT , 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36' ); <br>
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 30 ); <br>
curl_setopt( $ch, CURLOPT_TIMEOUT , 30); <br>
curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true ); <br>
if( $ispost ) <br>
{ <br>
curl_setopt( $ch , CURLOPT_POST , true ); <br>
curl_setopt( $ch , CURLOPT_POSTFIELDS , $params ); <br>
curl_setopt( $ch , CURLOPT_URL , $url ); <br>
} <br>
else <br>
{ <br>
if($params){ <br>
curl_setopt( $ch , CURLOPT_URL , $url.'?'.$params ); <br>
}else{ <br>
curl_setopt( $ch , CURLOPT_URL , $url); <br>
} <br>
} <br>
$response = curl_exec( $ch ); <br>
if ($response === FALSE) { <br>
//echo "cURL Error: " . curl_error($ch); <br>
return false; <br>
} <br>
$httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE ); <br>
$httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) ); <br>
curl_close( $ch ); <br>
return $response; <br>
} <br>
<br>
}