WordPressに天気予報表示を付けてみる2

「WordPressに天気予報表示を付けてみる」のphpコード

<?php
// APIからXMLデータ取得
$city = '81';
$url = 'http://weather.livedoor.com/forecast/webservice/rest/v1?city=';

// 取得データから表示項目取得
// 今日
$xml = simplexml_load_file($url . $city . '&amp;day=today');
echo '<div>';
outputWeatherInfomation($xml);

// 明日
$xml = simplexml_load_file($url . $city . '&amp;day=tomorrow');
outputWeatherInfomation($xml);

// 明後日(地区の出力)
$xml = simplexml_load_file($url . $city . '&amp;day=dayaftertomorrow');
outputWeatherInfomation($xml);
echo '<strong>in ' . $xml->location->attributes()->pref . $xml->location->attributes()->city . '市</strong><br>';
echo '</div>';

// 市区町村
echo '<br style="clear:both;">';
//outputPointInfomation($xml);

// 取得したXMLより天気情報出力
function outputWeatherInfomation($xml) {
	echo '<div>';
	echo '<div>' . date_format(new DateTime($xml->forecastdate),'m/j') . '<br>';
	echo '<small>' . $xml->day . '</small></div>';
	echo '<img src="' . $xml->image->url . '" width="' . $xml->image->width
       . '" height="' . $xml->image->height . '" alt="' . $xml->image->title . '" /><br>';
	echo '<div>';
	echo $xml->telop . '<br>';
	if (strlen($xml->temperature->min->celsiu)>0 || strlen($xml->temperature->max->celsius)>0) {
		echo $xml->temperature->min->celsius . '~' . $xml->temperature->max->celsius.'℃<br>';
	}
	echo '</div>';
	echo '</div>';
}

// 取得したXMLより市区町村情報出力
function outputPointInfomation($xml) {
	echo '<div>';
	foreach($xml->pinpoint->location as $pnt) {
		echo '<a href="' . $pnt->link . '" target="_blank">' . $pnt->title . '</a>|';
	}
	echo '</div>';
}
?>

css等は省略。

One Comment

Add a Comment

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください