Sunday 7 August 2011

google-weather-api_1

<?php
function getWeather() {
$requestAddress = "http://www.google.com/ig/api?weather=rajkot&hl=id";
// Downloads weather data based on location - I used my zip code.
$xml_str = file_get_contents($requestAddress,0);
// Parses XML
$xml = new SimplexmlElement($xml_str);
?>
<div>
<?php $xml->weather->current_conditions->temp_c['data'];?>&deg;
</div>

<div style="font-size: 12px; font-weight: none;"><?php $xml->weather->current_conditions->condition['data']?></div>
<br clear="both"/>
<div style="font-size: 20px;">&deg; C</div>
<?php
// Loops XML
$count = 0;
echo '<div id="weather">';
foreach($xml->weather as $item) {
foreach($item->forecast_conditions as $new) {
echo '<div>';
echo $new->day_of_week['data'];
echo '<img src="http://www.google.com/' .$new->icon['data'] . '"/><br/>';
echo $new->low['data'].' | '.$new->high['data'];
echo '</div>';
}
}
echo '</div>';
}
getWeather();
?>

No comments: