weather hacksで天気を表示!

« IPアドレスからホスト名を検索できる!IPSearch | メイン | PHPでRakuAPI。XML_Unserializer(PHP4) »

weather hacksで天気を表示!

livedoorの天気情報サービス、weather hacksのAPIを使って、サイドバーに天気を表示してみました。

結構いいコンテンツですね。

天気アイコンだけロゴの横に持ってくるレイアウトも検討中です。

プログラムとしては、PHP5の環境ならsimple_xmlの一行でxmlをオブジェクトにできるんだけど、このサイトを運営しているサーバがPHP4の環境であるため、simple_xmlは泣く泣く断念。

面倒だけれど仕方ないからpearを使うことに。Unserializerを使っています。

コードを下記に書いておきます!

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Weather Hacks</title>
<link href="main.css" rel="stylesheet" type="text/css" />
</head>
<body>
<?php
require_once("XML/Unserializer.php");
$base = "http://weather.livedoor.com/forecast/webservice/rest/v1?city=63&day=tomorrow";
$xmldoc = join("",file($base));
$opt = array (
'complexType' => 'object',
'parseAttributes' => 'true',
'attribuesArray' => '_attributes',
);
$xml = new XML_Unserializer($opt);
$xml -> unserialize($xmldoc, false);
$xml = $xml->getUnserializedData();
mb_convert_encoding($xml, "SJIS","UTF-8");
$title = $xml->title;
$img = $xml->image->url;
$url = $xml->link;
$description = $xml->description;
$date_rfc822 = $xml->forecastdate;
$date = date("m/j(D)",strtotime($date_rfc822));
$telop = $xml->telop;
$max_celsius = $xml->temperature->max->celsius;
$min_celsius = $xml->temperature->min->celsius;
echo
"<div class=\"weather\">".
"<a href=$url class=\"title\" target=\"_blank\">$title</a>".
"<div class=\"weather_content\">".
$date.
"<a href=$url target=\"_blank\"><img src=$img border=\"0\" alt=$title width=\"25px\"></a>".
$min_celsius.
"-".
$max_celsius.
$telop.
"<br />".
"<div class=\"description\">".
"</div>".
"</div>".
"</div>";

?>
</body>

この一覧は、次のエントリーを参照しています: weather hacksで天気を表示!:

» Weather HacksをPHP4でparse、CSSでレイアウト 送信元 blog.creamu.com
別のブロフで作っていたWeather Hacksのparseをこのブログにも導... [詳しくはこちら]

» Codeine and pseudoephedrine separation. 送信元 Tylenol with codeine elixir dosing.
Acetaminophen codeine phosphate. [詳しくはこちら]

アーカイブ