基於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>
'格式』 => 2 <br>
); <br>
$params=http_build_query($paramsArray); <br>
$content = $this->juhecurl($this->weatherIPUrl,$params); <br>
返回 $this->_returnArray($content); <br>
} <br>
<br>
/** <br>
* 依據GPS座標取得當地的天氣預報 <br>
* @param string $lon [經度] <br>
* @param string $lat [緯度] <br>
* @return array <br>
*/ <br>
公用函數 getWeatherByGeo($lon,$lat){ <br>
$paramsArray = 數組( <br>
'鑰匙' => $this->appkey, <br>
'lon' =>; $lon, <br>
'緯度' => $lat, <br>
'格式』 => 2 <br>
); <br>
$params=http_build_query($paramsArray); <br>
$content = $this->juhecurl($this->weatherGeoUrl,$params); <br>
返回 $this->_returnArray($content); <br>
} <br>
<br>
/** <br>
* 取得城市三小時預報 <br>
* @param string $city [城市名稱] <br>
* @return array <br>
*/ <br>
公用函數 getForecast($city){ <br>
$paramsArray = 數組( <br>
'鑰匙' => $this->appkey, <br>
'城市名稱' => $城市,<br>
'格式』 => 2 <br>
); <br>
$params=http_build_query($paramsArray); <br>
$content = $this->juhecurl($this->forecast3hUrl,$params); <br>
返回 $this->_returnArray($content); <br>
} <br>
<br>
/** <br>
* 將JSON內容轉換為數據,並回傳 <br>
* @param string $content [內容] <br>
* @return array <br>
*/ <br>
公用函數_returnArray($content){ <br>
回 json_decode($content,true); <br>
} <br>
<br>
/** <br>
* 請求介面回傳內容<br>
* @param string $url [請求的網址] <br>
* @param string $params[請求的參數] <br> * @param int $ipost [是否採用POST形式] <br>
* @return 字串<br>
*/ <br>
公用函數 juhecurl($url,$params=false,$ispost=0){ <br>
$httpInfo = 數組(); <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,如 Gecko)Chrome/41.0.2272.118Safari/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>
否則<br>
{ <br>
if($params){ <br>
curl_setopt($ch,CURLOPT_URL,$url.'?'.$params); <br>
}其他{ <br>
curl_setopt($ch,CURLOPT_URL,$url); <br>
} <br>
} <br>
$回應=curl_exec($ch); <br>
if ($response === FALSE) { <br>
//echo「cURL 錯誤:」。
回傳錯誤; <br>
} <br>
$httpCode=curl_getinfo($ch,CURLINFO_HTTP_CODE); <br>
$httpInfo=array_merge($httpInfo,curl_getinfo($ch)); <br>
捲曲_關閉($ch); <br>
回$響應; <br>
} <br>
<br>
} <br>