代码示例是基于PHP的聚合数据全国景象预报API做事要求的代码样例,利用前你须要:

①:通过https://www.juhe.cn/docs/api/id/39 申请一个景象预报API的appkey

样例代码包含了获取支持城市列表、根据城市获取景象预报、根据IP地址要求景象预报、根据GPS坐标要求景象、城市3小时景象预报的实现。
示例代码紧张是解析一些常用字段,如须要完全或其他未包含的字段,可以自行参考官方的接口,进行修正。

php日历天气预报PHP挪用全国气象预告接口查询气象示例 Bootstrap

首先:引入封装好的景象调用类

header('Content-type:text/html;charset=utf-8');include 'class.juhe.weather.php'; //引入景象要求类//接口基本信息配置$appkey = ''; //您申请的景象查询appkey$weather = new weather($appkey);

一、获取支持的城市列表

由于支持的城市列表基本不会这么变革,大家可以获取到列表后内置到自己的运用中,就不用每次都去要求API。

$citysResult = $weather->getCitys();if($citysResult['error_code'] == 0){ //以下可根据实际业务需求,自行改写 $citys = $citysResult['result']; foreach($citys as $ckey =>$c){ echo \"大众ID:\公众.$c['id'].\公众,省份:\"大众.$c['province'].\"大众,城市:\"大众.$c['city'].\"大众,区域:\"大众.$c['district'].\公众<br>\"大众; }}else{ //以下可根据实际业务需求,自行改写 echo $citysResult['error_code'].\"大众:\"大众.$citysResult['reason'];}

二、根据城市/ID获取景象预报

通过城市的名称或城市的ID来获取景象预报,城市id便是获取城市支持列表中返回的字段ID

$cityWeatherResult = $weather->getWeather('苏州');if($cityWeatherResult['error_code'] == 0){ //以下可根据实际业务需求,自行改写 $data = $cityWeatherResult['result']; echo \"大众=======当前景象实况=======<br>\"大众; echo \"大众温度:\公众.$data['sk']['temp'].\"大众 \"大众; echo \"大众风向:\公众.$data['sk']['wind_direction'].\"大众 (\"大众.$data['sk']['wind_strength'].\公众)\公众; echo \"大众湿度:\"大众.$data['sk']['humidity'].\"大众 \公众; echo \"大众<br><br>\"大众; echo \公众=======未来几天景象预报=======<br>\"大众; foreach($data['future'] as $wkey =>$f){ echo \公众日期:\"大众.$f['date'].\"大众 \"大众.$f['week'].\"大众 \"大众.$f['weather'].\"大众 \"大众.$f['temperature'].\公众<br>\"大众; } echo \"大众<br><br>\"大众; echo \"大众=======干系景象指数=======<br>\"大众; echo \"大众穿衣指数:\"大众.$data['today']['dressing_index'].\"大众 , \公众.$data['today']['dressing_advice'].\"大众<br>\"大众; echo \公众紫外线强度:\"大众.$data['today']['uv_index'].\"大众<br>\"大众; echo \"大众舒适指数:\公众.$data['today']['comfort_index'].\"大众<br>\公众; echo \"大众洗车指数:\公众.$data['today']['wash_index']; echo \"大众<br><br>\"大众;}else{ echo $cityWeatherRe}

三、根据用户的IP地址要求对应的景象预报

通过用户的IP地址获取用户所在地的景象预报,由于IP地址解析可能会有偏差,以是有时定位到的城市不一定是用户实际的所在地。

$ipWeatherResult = $weather->getWeatherByIP('58.215.154.128');if($ipWeatherResult['error_code'] == 0){ //以下可根据实际业务需求,自行改写 ////////////////////////////////////////////////////////////////////// $data = $ipWeatherResult['result']; echo \"大众=======当前城市=======<br>\公众; echo $data['today']['city']; echo \"大众<br><br>\"大众; echo \"大众=======当前景象实况=======<br>\"大众; echo \"大众温度:\"大众.$data['sk']['temp'].\公众 \公众; echo \"大众风向:\公众.$data['sk']['wind_direction'].\"大众 (\"大众.$data['sk']['wind_strength'].\"大众)\"大众; echo \"大众湿度:\"大众.$data['sk']['humidity'].\"大众 \"大众; echo \公众<br><br>\公众; echo \"大众=======未来几天景象预报=======<br>\公众; foreach($data['future'] as $wkey =>$f){ echo \公众日期:\"大众.$f['date'].\"大众 \"大众.$f['week'].\"大众 \公众.$f['weather'].\"大众 \"大众.$f['temperature'].\"大众<br>\"大众; } echo \"大众<br><br>\公众; echo \"大众=======干系景象指数=======<br>\"大众; echo \"大众穿衣指数:\"大众.$data['today']['dressing_index'].\公众 , \"大众.$data['today']['dressing_advice'].\"大众<br>\"大众; echo \公众紫外线强度:\"大众.$data['today']['uv_index'].\公众<br>\公众; echo \"大众舒适指数:\公众.$data['today']['comfort_index'].\"大众<br>\"大众; echo \"大众洗车指数:\公众.$data['today']['wash_index']; echo \"大众<br><br>\"大众;}else{ echo $ipWeatherResult['error_code'].\"大众:\"大众.$ipWeatherResult['reason'];}

四、根据GPS坐标来获取对应地区的景象

无论通过二、三、四获取的景象预报,由于聚合格式都是统一的,以是解析的流程是同等的,以是没有额外的操作,只是传参上有点的差异。

$geoWeatherResult = $weather->getWeatherByGeo(116.401394,39.916042);if($geoWeatherResult['error_code'] == 0){ //以下可根据实际业务需求,自行改写 ////////////////////////////////////////////////////////////////////// $data = $geoWeatherResult['result']; echo \公众=======当前城市=======<br>\公众; echo $data['today']['city']; echo \"大众<br><br>\"大众; echo \公众=======当前景象实况=======<br>\"大众; echo \"大众温度:\"大众.$data['sk']['temp'].\"大众 \"大众; echo \"大众风向:\"大众.$data['sk']['wind_direction'].\"大众 (\"大众.$data['sk']['wind_strength'].\"大众)\公众; echo \公众湿度:\"大众.$data['sk']['humidity'].\"大众 \"大众; echo \"大众<br><br>\"大众; echo \"大众=======未来几天景象预报=======<br>\公众; foreach($data['future'] as $wkey =>$f){ echo \公众日期:\公众.$f['date'].\"大众 \"大众.$f['week'].\"大众 \公众.$f['weather'].\"大众 \公众.$f['temperature'].\公众<br>\"大众; } echo \公众<br><br>\公众; echo \公众=======干系景象指数=======<br>\"大众; echo \"大众穿衣指数:\"大众.$data['today']['dressing_index'].\公众 , \"大众.$data['today']['dressing_advice'].\公众<br>\公众; echo \公众紫外线强度:\"大众.$data['today']['uv_index'].\"大众<br>\公众; echo \"大众舒适指数:\"大众.$data['today']['comfort_index'].\公众<br>\"大众; echo \"大众洗车指数:\公众.$data['today']['wash_index']; echo \公众<br><br>\"大众;}else{ echo $geoWeatherResult['error_code'].\"大众:\"大众.$geoWeatherResult['reason'];}

五、获取城市三小时预报

便是城市每3小时的景象情形

$forecastResult = $weather->getForecast(\"大众苏州\公众);if($forecastResult['error_code'] == 0){ //以下可根据实际业务需求,自行改写 ////////////////////////////////////////////////////////////////////// $data = $forecastResult['result']; foreach($data as $key => $d){ echo \"大众日期:\"大众.$d['date'].\"大众 (\"大众.$d['sh'].\"大众点-\公众.$d['eh'].\"大众点) \"大众.$d['weather'].\公众 \"大众.$d['temp1'].\"大众~\"大众.$d[\"大众temp2\"大众].\"大众<br>\"大众; }}else{ //以下可根据实际业务需求,自行改写 ////////////////////////////////////////////////////////////////////// echo $forecastResult['error_code'].\"大众:\"大众.$forecastResult['reason'];}

通过上面的示例代码,大家该当对如果调用聚合数据景象预报API有了一个大体的理解。

末了放上class.juhe.weather.php完全代码:

<?php// +----------------------------------------------------------------------// | JuhePHP [ NO ZUO NO DIE ]// +----------------------------------------------------------------------// | Copyright (c) 2010-2015 http://juhe.cn All rights reserved.// +----------------------------------------------------------------------// | Author: Juhedata <info@juhe.cn-->// +----------------------------------------------------------------------//----------------------------------// 聚合数据景象预报接口要求类//----------------------------------class weather{ private $appkey = false; //申请的聚合景象预报APPKEY private $cityUrl = 'http://v.juhe.cn/weather/citys'; //城市列表API URL private $weatherUrl = 'http://v.juhe.cn/weather/index'; //根据城市要求景象API URL private $weatherIPUrl = 'http://v.juhe.cn/weather/ip'; //根据IP地址要求景象API URL private $weatherGeoUrl = 'http://v.juhe.cn/weather/geo'; //根据GPS坐标获取景象API URL private $forecast3hUrl = 'http://v.juhe.cn/weather/forecast3h'; //获取城市景象3小时预报API URL public function __construct($appkey){ $this->appkey = $appkey; } / 获取景象预报支持城市列表 @return array / public function getCitys(){ $params = 'key='.$this->appkey; $content = $this->juhecurl($this->cityUrl,$params); return $this->_returnArray($content); } / 根据城市名称/ID获取详细景象预报 @param string $city [城市名称/ID] @return array / public function getWeather($city){ $paramsArray = array( 'key' => $this->appkey, 'cityname' => $city, 'format' => 2 ); $params = http_build_query($paramsArray); $content = $this->juhecurl($this->weatherUrl,$params); return $this->_returnArray($content); } / 根据IP地址获取当地景象预报 @param string $ip [IP地址] @return array / public function getWeatherByIP($ip){ $paramsArray = array( 'key' => $this->appkey, 'ip' => $ip, 'format' => 2 ); $params = http_build_query($paramsArray); $content = $this->juhecurl($this->weatherIPUrl,$params); return $this->_returnArray($content); } / 根据GPS坐标获取当地的景象预报 @param string $lon [经度] @param string $lat [纬度] @return array / public function getWeatherByGeo($lon,$lat){ $paramsArray = array( 'key' => $this->appkey, 'lon' => $lon, 'lat' => $lat, 'format' => 2 ); $params = http_build_query($paramsArray); $content = $this->juhecurl($this->weatherGeoUrl,$params); return $this->_returnArray($content); } / 获取城市三小时预报 @param string $city [城市名称] @return array / public function getForecast($city){ $paramsArray = array( 'key' => $this->appkey, 'cityname' => $city, 'format' => 2 ); $params = http_build_query($paramsArray); $content = $this->juhecurl($this->forecast3hUrl,$params); return $this->_returnArray($content); } / 将JSON内容转为数据,并返回 @param string $content [内容] @return array / public function _returnArray($content){ return json_decode($content,true); } / 要求接口返回内容 @param string $url [要求的URL地址] @param string $params [要求的参数] @param int $ipost [是否采取POST形式] @return string / public function juhecurl($url,$params=false,$ispost=0){ $httpInfo = array(); $ch = curl_init(); curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 ); 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' ); curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 30 ); curl_setopt( $ch, CURLOPT_TIMEOUT , 30); curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true ); if( $ispost ) { curl_setopt( $ch , CURLOPT_POST , true ); curl_setopt( $ch , CURLOPT_POSTFIELDS , $params ); curl_setopt( $ch , CURLOPT_URL , $url ); } else { if($params){ curl_setopt( $ch , CURLOPT_URL , $url.'?'.$params ); }else{ curl_setopt( $ch , CURLOPT_URL , $url); } } $response = curl_exec( $ch ); if ($response === FALSE) { //echo \"大众cURL Error: \公众 . curl_error($ch); return false; } $httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE ); $httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) ); curl_close( $ch ); return $response; }}