<?php
/**
* weather_forecast
*
* Display the weather for a location. Look up using the Google weather API.
*
* @version 1.4
* @author Contributors at eXorithm
* @link /algorithm/view/weather_forecast Listing at eXorithm
* @link /algorithm/history/weather_forecast History at eXorithm
* @license /home/show/license
*
* @param mixed $location The location to get the weather for. Can be a city, zip code, or postal code.
* @param mixed $units The units to get the temperature in.
* @param string $header_color (hex color code) Color of the header
* @param string $day_header_color (hex color code) Color of the header for each of the days
* @param string $day_color (hex color code) Color of the cells containing the forecasts
* @return string HTML
*/
function weather_forecast($location='Sydney, Australia',$units='c',$header_color='f0f0f0',$day_header_color='d0ffd0',$day_color='f0f0f0')
{
$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>";
}
?>