PHP での簡単なマッシュアップ

こんにちは。PHP 担当の高杉です。
PHP などのWebアプリケーションを作成するときに、API(アプリケーション・プログラミング・インタフェース、Application Programming Interface)サービスを利用して、Webサイトを作成する機会が多くなっていると思います。
今回は実際に天気APIサービスを利用して、天気情報を出力してみましょう。
実際に作ってみることで、WEB APIの利用の仕方が具体的になると思います。
APIの出力データ形式には、比較的XMLが使用されることが多いのです。
ですので、今回は、PHP4でXMLを利用するためのライブラリPEAR::XML_SerializerとPHP5から利用できるようになったSimple_XMLを使用して、天気情報を出力します。
実際のやってみましょう!
天気情報APIとしては、Livedoor社提供の「weather hacks」というAPIを使用します。 http://weather.livedoor.com/weather_hacks/PEAR::XML_Serializerのインストール
PEAR::XML_Serializerのインストールはpear install --alldeps xml_serializer-beta
で行います。
--alldepsオプションを付けてインストールすると依存関係のあるパッケージもインストールしてくれます。
今回は、PEAR::XML_SerializerとSimple_XMLで今日の東京の天気を取得します。
それでは、実際のスクリプトは、以下のようになります。
PEAR::XML_Serializerでの実装
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>天気情報APIサンプル</title>
</head>
<body>
<?php
//PEAR::XML_Serializerの読込
require_once("XML/Unserializer.php");
$base_url =
"http://weather.livedoor.com/forecast/webservice/rest/v1?city=63&day=today";
$xmldoc = join("",file($base_url));
//オプションの設定
$opt = array (
'complexType' => 'object',
'parseAttributes' => 'true',
'attribuesArray' => '_attributes',
);
$xml = new XML_Unserializer($opt);
$xml -> unserialize($xmldoc, false);
//データの取得
$xml = $xml->getUnserializedData();
//地域名称取得
$place = $xml->location->pref . $xml->location->city;
//画像情報取得
$img = $xml->image->url;
$img_width = $xml->image->width;
$img_height = $xml->image->height;
//リンク先取得
$url = $xml->link;
//説明取得
$description = nl2br($xml->description);
//日付取得
$date_rfc822 = $xml->forecastdate;
$date = date("m/j(D)",strtotime($date_rfc822));
$telop = $xml->telop;
//出力
print <<< OUT_END
<div>
<div>
{$place}({$date})
<a href=$url target="_blank">
<img src=$img border="0" alt=$place hegiht="$img_height" width="$img_width">
</a>{$telop}<br>
{$description}
<br />
</div>
</div>
OUT_END;
?>
<hr />
</body>
</html>
Simple_XMLでの実装
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>天気情報APIサンプル</title>
</head>
<body>
<?php
//Simple_XMLでの実装
$base_url =
"http://weather.livedoor.com/forecast/webservice/rest/v1?city=63&day=today";
//データ取得
$xml = simplexml_load_file($base_url);
//地域名称取得
$city_name = $xml->location->attributes();
$place = $city_name["pref"] . $city_name["city"];
//画像情報取得
$img = $xml->image->url;
$img_width = $xml->image->width;
$img_height = $xml->image->height;
//リンク先取得
$url = $xml->link;
$description = nl2br($xml->description);
//日付取得
$date_rfc822 = $xml->forecastdate;
$date = date("m/j(D)",strtotime($date_rfc822));
$telop = $xml->telop;
//出力
print <<< OUT_END
<div>
<div>
{$place}({$date})
<a href=$url target="_blank">
<img src=$img border="0" alt=$place hegiht="$img_height" width="$img_width">
</a>{$telop}<br>
{$description}
<br />
</div>
</div>
OUT_END;
?>
</body>
</html>
いかかでしょうか?
このように、weather hacksのAPIを使用することで簡単に天気情報を出力できました。取得できる情報などをより詳細を知りたい場合は以下のURLをご参照ください。
http://weather.livedoor.com/weather_hacks/webservice.html
他にも天気情報を提供しているところがあります。
http://www.weathermap.co.jp/hitokuchi_rss/
上記紹介したサイトの天気情報は個人利用では無料で使用できますが、商用利用としては許可されていないものもあります。利用するときはサイトのポリシーなどに気をつけて使用しましょう。
現在、地図のAPIや天気情報のAPIなど、いろいろなAPIが公開されています。是非、天気情報以外のXMLで記述されたAPIをPHPで取得し、使用してみてください。




最近のコメント