weather_forecast version 1.4 Display the weather for a location. Look up using the Google weather API.
Run
Source (PHP)
function weather_forecast ($location, $units, $header_color, $day_header_color, $day_color)
{
$weather = file_get_contents('http://www.google.com/ig/api?weather='.urlencode($location));
$xml = simplexml_load_string($weather);
if (!isset($xml->weather->forecast_conditions)) {
throw new Exception('Data could not be retreived for location '.$location);
}
$location = $xml->weather->forecast_information->city['data'];
// four day outlook
for ($i = 0; $i < 4; $i++){
if ($xml->weather->forecast_conditions->$i) {
$forecast_day[] = $xml->weather->forecast_conditions->$i->day_of_week['data'];
$forecast_condition[] = $xml->weather->forecast_conditions->$i->condition['data'];
$low = $xml->weather->forecast_conditions->$i->low['data'];
if ($units=='k')
$low = round((($low - 32)*5/9) + 273, 1);
else if ($units=='c')
$low = round(($low - 32)*5/9, 1);
$forecast_low[] = $low;
$high = $xml->weather->forecast_conditions->$i->high['data'];
if ($units=='k')
$high = round((($high - 32)*5/9) + 273, 1);
else if ($units=='c')
$high = round(($high - 32)*5/9, 1);
$forecast_high[] = $high;
$forecast_icon[] = $xml->weather->forecast_conditions->$i->icon['data'];
}
}
// current
$condition = $xml->weather->current_conditions->condition['data'];
$temp = $xml->weather->current_conditions->temp_f['data'];
if ($units=='k')
$temp = round((($temp - 32)*5/9) + 273, 1);
else if ($units=='c')
$temp = round(($temp - 32)*5/9, 1);
$icon = $xml->weather->current_conditions->icon['data'];
// build the HTML
$header = "<tr><td colspan=\"".count($forecast_day)."\" bgcolor=\"#$header_color\">";
if ($icon!='')
$header .= "<img align=\"left\" src=\"http://www.google.com$icon\">";
$header .= "<b>$location</b><br>Currently <i>$condition</i> <b>$temp$units</b>";
$header .= "</td></tr>\n<tr>";
$data = "<tr>\n";
for ($i = 0; $i < count($forecast_day); $i++){
$header .= "<td width=\"130\" bgcolor=\"#$day_header_color\"><b>$forecast_day[$i]</b></td>";
$data .= "<td bgcolor=\"#$day_color\">";
$data .= "<img src=\"http://www.google.com$forecast_icon[$i]\">";
$data .= "<br><i>$forecast_condition[$i]</i>";
$data .= "<br>high <b>$forecast_high[$i]$units</b>";
$data .= "<br>low <b>$forecast_low[$i]$units</b>";
$data .= "</td>\n";
}
$header .= "</tr>";
$data .= "</tr>";
return "<table cellpadding=\"5\" cellspacing=\"3\">\n$header\n$data\n</table>";
}
Tags
weather, forecast, google, html, widget
Tools & Information
Rate this algorithm:
![]()
93% on 14 votes
Flag: 0 Buggy | 0 Dangerous | 0 Slow | 0 Vandalized
Average run time: 0.191693708 seconds
