2010-07-16 17:29

[PHP] 取得 Weather Underground 即時氣象

如果你需要即時的氣象資訊,找 Weather Underground 是一個不錯的選擇,每30分鐘更新一次,再加上提供很方便 XML 資料連結,唯一感到美中不足的就是沒有多國語言的支持。

<?php
// ?query=[觀測點名稱],例如: Taoyuan Air Base , Taipei
$xml = simplexml_load_file(
    "http://api.wunderground.com/auto/wui/geo/WXCurrentObXML/index.xml?query=RCSS"
);

// 氣象圖示
foreach ($xml->icons->children() as $icon_set) {
    if($icon_set['name']=='Contemporary'){
        echo $icon_set->icon_url;
    }    
}
/* 
圖示造型種類:
    Default
    Smiley
    Helen
    Generic
    Old School
    Cartoon
    Mobile
    Simple
    Contemporary
    Dunkin' Donuts    
*/

echo "氣象狀況:",$xml->weather,"\n";

echo "溫度:",$xml->temp_c,"°C\n";

echo "相對濕度:",$xml->relative_humidity,"\n";

echo "風向:",$xml->wind_dir,"\n";

echo "風速:",$xml->wind_mph,"MPH\n";
echo "風速:每小時",round($xml->wind_mph*1.6093),"公里\n";
echo "風速:每秒",round($xml->wind_mph*0.447028),"公尺\n";

echo "海平面氣壓:",$xml->pressure_mb,"百帕\n";

echo "高溫指數:",$xml->heat_index_c,"°C\n";

echo "風寒指數:",$xml->windchill_c,"°C\n";

echo "水凝點:",$xml->dewpoint_c,"°C\n";

echo "能見度:",$xml->visibility_km,"公里\n";

echo "觀測時間:",date(
    'Y-m-d H:i',
    strtotime($xml->observation_time_rfc822)
),"\n";

/* 
thunderstorm rain = 雷雨
showers rain = 驟雨
light showers rain = 小驟雨

Cloudy = 多雲
Flurries = 小雪
Fog = 霧
Haze = 陰霾
Mostly Cloudy = 多雲時陰
Mostly Sunny = 晴時多雲
Partly Cloudy = 局部多雲
Partly Sunny = 多雲時晴
Freezing Rain = 凍雨
Rain = 雨
Sleet = 冰雹
Snow = 雪
Sunny = 晴朗
Unknown = 未知
Overcast = 陰天
Scattered Clouds = 疏雲 
*/


參考資料:
台灣各地-氣象觀測點
API - XML - WunderWiki

PHP: simplexml_load_file - Manual

1 回應:

來福 提到...
作者已經移除這則留言。