2011年11月17日木曜日

外部APIを利用したWEBサービスのサンプル(9)

気象情報(天気予報)APIの利用

表示例

LINK

API URL
http://www.google.com/ig/api?weather=Yokohama,Kanagawa

取得されるXMLのサンプル
<xml_api_reply version="1">
 <weather module_id="0" tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0">
  <forecast_information>
   <city data="Yokohama, Kanagawa Prefecture"/>
   <postal_code data="Yokohama,Kanagawa"/>
   <latitude_e6 data=""/>
   <longitude_e6 data=""/>
   <forecast_date data="2011-11-17"/>
   <current_date_time data="1970-01-01 00:00:00 +0000"/>
   <unit_system data="SI"/>
  </forecast_information>
  <current_conditions>
   <condition data="ところにより曇り"/>
   <temp_f data="63"/>
   <temp_c data="17"/>
   <humidity data="湿度 : 39%"/>
   <icon data="/ig/images/weather/jp_cloudy.gif"/>
   <wind_condition data="風: 南東 4 m/s"/>
  </current_conditions>
  <forecast_conditions>
   <day_of_week data="木"/>
   <low data="11"/>
   <high data="18"/>
   <icon data="/ig/images/weather/jp_sunny.gif"/>
   <condition data="ところにより晴れ"/>
  </forecast_conditions>
  <forecast_conditions>
   <day_of_week data="金"/>
   <low data="10"/>
   <high data="18"/>
   <icon data="/ig/images/weather/jp_cloudy.gif"/>
   <condition data="曇り"/>
   </forecast_conditions>
  <forecast_conditions>
   <day_of_week data="土"/>
   <low data="16"/>
   <high data="21"/>
   <icon data="/ig/images/weather/jp_rainysometimescloudy.gif"/>
   <condition data="雨の可能性"/>
  </forecast_conditions>
  <forecast_conditions>
   <day_of_week data="日"/>
   <low data="14"/>
   <high data="22"/>
   <icon data="/ig/images/weather/jp_rainysometimescloudy.gif"/>
   <condition data="雨の可能性"/>
  </forecast_conditions>
 </weather>
</xml_api_reply>

weather.php
<?php
 $area = "Yokohama";
 $url = "http://www.google.com/ig/api?weather=".$area;

 $xml = simplexml_load_file($url);

//forecast_information
 $city = $xml->weather->forecast_information->city->attributes();
 $forecast_date = $xml->weather->forecast_information->forecast_date->attributes();
 $unit_system = $xml->weather->forecast_information->unit_system->attributes();

//current_conditions
 $current_condition = $xml->weather->current_conditions->condition->attributes();
 $temp_c = $xml->weather->current_conditions->temp_c->attributes();
 $humidity = $xml->weather->current_conditions->humidity->attributes();
 $current_icon = $xml->weather->current_conditions->icon->attributes();
 $wind_condition = $xml->weather->current_conditions->wind_condition->attributes();

//forecast_conditions
 $day_of_week = $xml->weather->forecast_conditions->day_of_week->attributes();
 $low = $xml->weather->forecast_conditions->low->attributes();
 $high = $xml->weather->forecast_conditions->high->attributes();
 $forecast_icon = $xml->weather->forecast_conditions->icon->attributes();
 $forecast_condition = $xml->weather->forecast_conditions->conditionh->attributes();

print<<<EOF
 <html>
 <head>
 <title>google weather sample</title>
 </head>
 <body>
EOF;

 print "場所:".$city."<br />";
 print "予報日:".$forecast_date."<br />";
 print "単位:".$unit_system."<br />";
 print "<br />";
 print "現在の状況<br />";
 print "状況:".$current_condition."<br />";
 print "気温:".$temp_c."<br />";
 print "湿度:".$humidity."<br />";
 print "<img src='http://www.google.com".$current_icon."'><br />";
 print "風向:".$wind_condition."<br />";
 print "<br />";
 print "明日の天気<br />";
 print "曜日:".$day_of_week."<br />";
 print "最低気温:".$low."<br />";
 print "最高気温:".$high."<br />";
 print "<img src='http://www.google.com".$forecast_icon."'><br />";
 print "状況:".$forecast_condition."<br />";

print<<<EOF
 </body>
 </html>
EOF;
?>

0 件のコメント: