PHP: RSS Yahoo Weather Parser

Yahoo! Weather menyediakan informasi cuaca hampir semua lokasi di dunia. Informasi cuaca ini bisa diambil melalui RSS yang disediakan oleh Yahoo! Contohnya, informasi cuaca di kota Jakarta, bisa diakses di URL berikut :

http://weather.yahooapis.com/forecastrss?p=IDXX0022&u=c

Gmana cara ngambil datanya? untuk pengguna bahasa pemrograman PHP ini dia contoh kodenya.

[sourcecode language=”php”]
function yahooWeatherParser($source) {

//$source = “http://weather.yahooapis.com/forecastrss?p=IDXX0022&u=c”;
$strXml = @file_get_contents($source);
if($strXml == false) return false;

$objRss = simplexml_load_string($strXml);

$objWeather = new SimpleXMLElement($strXml);
$weather[‘condition’] = $objWeather->xpath(“//yweather:condition”);
$weather[‘location’] = $objWeather->xpath(“//yweather:location”);
$weather[‘units’] = $objWeather->xpath(“//yweather:units”);
$weather[‘wind’] = $objWeather->xpath(“//yweather:wind”);
$weather[‘atmosphere’] = $objWeather->xpath(“//yweather:atmosphere”);
$weather[‘astronomy’] = $objWeather->xpath(“//yweather:astronomy”);
$weather[‘forecast’] = $objWeather->xpath(“//yweather:forecast”);
$weather[‘description’] = $objRss->channel->item->description;

return $weather;
}
[/sourcecode]

Gimana cara pakenya? gini ceritanya…

[sourcecode language=”php”]
print_r(yahooWeatherParser(‘http://weather.yahooapis.com/forecastrss?p=IDXX0022&u=c’));
[/sourcecode]

1 comment / Add your comment below

Leave a Reply

Your email address will not be published. Required fields are marked *