天気情報取得するには、
どこかの天気情報サイトと契約して情報をもらうか、
RSSから取得するか。
果ては、PEAR::Services_Weatherを使うか。(ムリ)
どれもこれもイマイチなのだが、
このPEAR::Services_Livedoorなら一撃です
(ノ・・)ン。。。。。。(((●コロコロッ
インストールは至って簡単。
# pear install http://p4life.jp/services_livedoor/Services_Livedoor-0.1.0.tgz
使い方も至って簡単。
$city_idというLivedoor側が設定している日本全国142箇所の都市番号を与えると、
天気情報が”配列で”取得できます。
第二引数にはいつの天気を取得するかを設定する。
パラメータ値はtoday,tomorrow,dayaftertomorrowの三種類。
取得できるデータはお天気Webサービス仕様を見て欲しい。
ソースはこんな感じ。
<? require_once ("Services/Livedoor/Weather.php"); $city_id = 57; // new Services_Livedoor_Weather Object $lwwsObj = & new Services_Livedoor_Weather(); $lwwsArray = & $lwwsObj->get( (int)$city_id, (string)'today'); print_r( $lwwsArray ); ?>
結果はこんな感じ
Array ( [author] => livedoor Weather Team. [location] => [title] => 栃木県 大田原 - 今日の天気 [link] => http://weather.livedoor.com/area/9/57.html?v=1 [forecastday] => today [day] => Sunday [forecastdate] => Sun, 26 Feb 2006 00:00:00 +0900 [publictime] => Sat, 25 Feb 2006 17:00:00 +0900 [telop] => 曇のち雨 [description] => 栃木県北部山地の積雪の多い所では、26日にかけてなだれのおこりやすい状態となる見込みですので、なだれの発生に注意して下さい。 北海道の東には高気圧があっ... [image] => Array ( [title] => 曇のち雨 [link] => http://weather.livedoor.com/area/9/57.html?v=1 [url] => http://image.weather.livedoor.com/img/icon/13.gif [width] => 50 [height] => 31 ) [temperature] => Array ( [max] => Array ( [celsius] => 8 [fahrenheit] => 46.4 ) [min] => Array ( [celsius] => 4 [fahrenheit] => 39.2 ) ) [pinpoint] => Array ( [location] => Array ( [0] => Array ( [title] => 日光市 [link] => http://weather.livedoor.com/point/city/716.html [publictime] => Sun, 26 Feb 2006 00:00:00 +0900 ) [1] => Array ( [title] => 今市市 [link] => http://weather.livedoor.com/point/city/717.html [publictime] => Sun, 26 Feb 2006 00:00:00 +0900 ) [2] => Array ( [title] => 大田原市 [link] => http://weather.livedoor.com/point/city/720.html [publictime] => Sun, 26 Feb 2006 00:00:00 +0900 ) [3] => Array ( [title] => 矢板市 [link] => http://weather.livedoor.com/point/city/721.html [publictime] => Sun, 26 Feb 2006 00:00:00 +0900 ) [4] => Array ( [title] => 那須塩原市 [link] => http://weather.livedoor.com/point/city/722.html [publictime] => Sun, 26 Feb 2006 00:00:00 +0900 ) [5] => Array ( [title] => 足尾町 [link] => http://weather.livedoor.com/point/city/730.html [publictime] => Sun, 26 Feb 2006 00:00:00 +0900 ) [6] => Array ( [title] => 栗山村 [link] => http://weather.livedoor.com/point/city/744.html [publictime] => Sun, 26 Feb 2006 00:00:00 +0900 ) [7] => Array ( [title] => 藤原町 [link] => http://weather.livedoor.com/point/city/745.html [publictime] => Sun, 26 Feb 2006 00:00:00 +0900 ) [8] => Array ( [title] => 塩谷町 [link] => http://weather.livedoor.com/point/city/746.html [publictime] => Sun, 26 Feb 2006 00:00:00 +0900 ) [9] => Array ( [title] => 湯津上村 [link] => http://weather.livedoor.com/point/city/751.html [publictime] => Sun, 26 Feb 2006 00:00:00 +0900 ) [10] => Array ( [title] => 黒羽町 [link] => http://weather.livedoor.com/point/city/752.html [publictime] => Sun, 26 Feb 2006 00:00:00 +0900 ) [11] => Array ( [title] => 那須町 [link] => http://weather.livedoor.com/point/city/37.html [publictime] => Sun, 26 Feb 2006 00:00:00 +0900 ) ) ) [copyright] => Array ( [title] => Copyright (C) 1996-2006 livedoor Co.,Ltd. All rights reserved. [link] => http://weather.livedoor.com [image] => Array ( [title] => livedoor 天気情報 [link] => http://weather.livedoor.com [url] => http://image.weather.livedoor.com/img/cmn/livedoor.gif [width] => 118 [height] => 26 ) [provider] => Array ( [0] => [1] => ) ) )
city_idは都市名("さいたま"とか)でも取得できるが、1-142までループさせる方が手っ取り早い。
気象台発表は朝5時、昼11時、夕方17時の合計3回。
発表したときにはすでに反映されているので、
一日3回全国のデータを取得すれば最新の天気が取得できる。
注意点
LWWSは最高気温、最低気温が取得できない場合がある。 descriptionは全文取得できない
欠点
ナンデ配列?