Thursday, October 2, 2014

การสร้าง weather code เพื่อติดที่เว็บไซต์ (PHP Create Weather on Web By Yahoo)

โค้ด weather code เพื่อแสดงอุณหภูมิที่เว็บไซต์

<?php
$result = file_get_contents('http://weather.yahooapis.com/forecastrss?p=THXX0039&u=c');
$xml = simplexml_load_string($result);

//echo htmlspecialchars($result, ENT_QUOTES, 'UTF-8');

$xml->registerXPathNamespace('yweather', 'http://xml.weather.yahoo.com/ns/rss/1.0');
$location = $xml->channel->xpath('yweather:location');

if(!empty($location)){
foreach($xml->channel->item as $item){
$current = $item->xpath('yweather:condition');
$forecast = $item->xpath('yweather:forecast');
$current = $current[0];
$output = <<<END
<p>
<span style="font-size:30pt;">{$current['temp']}&deg;C</span>
<br/>
<img src="http://s.imwx.com/v.20131006.215409/img/wxicon/100/%7B$current['code']%7D.png" style="vertical-align: middle;"/><br>
{$current['text']}
</p>

END;
}
}else{
$output = '<h1>No results found, please try a different zip code.</h1>';
}
?>

No comments:

Post a Comment