[insert_php]
// Extract weather data from weather underground.
// Display only some of the data.
$url_xml = “https://api.wunderground.com/weatherstation/WXDailyHistory.asp?ID=IONTARIO859&format=XML”;
date_default_timezone_set(‘Canada/Eastern’);
$mph_to_kmh = 1.60934;
$local_file = “WundergroundDaily.txt”; // Store website to local file for reduced calls
$file_time = filemtime($local_file); // when was the file updated
$current_time = time();
$time_difference = $current_time – $file_time;
if( $time_difference > (5 * 60) ) // fetch from website if file older than seconds
{
$xml_data = file_get_contents($url_xml);
file_put_contents ($local_file, $xml_data);
}
$observations = simplexml_load_file($local_file);
$number_of_observations = intval($observations->count());
if($number_of_observations > 0) // chan’t show more than exist
{
$obs_number = $number_of_observations – 1;
$time=explode(“, “,$observations->current_observation[$obs_number]->observation_time);
$time=explode(” E”, $time[1]);
$time=strtolower($time[0]);
$time=str_pad($time, 8, “0”, STR_PAD_LEFT);
echo “\n”.$time;
$wind_speed = floatval($observations->current_observation[$obs_number]->wind_mph) * $mph_to_kmh;
$wind_speed = round($wind_speed,0);
echo str_pad($wind_speed, 5, ” “, STR_PAD_LEFT).” km/h”;
$wind_dir = $observations->current_observation[$obs_number]->wind_dir;
if ($wind_dir == “North”)
$wind_dir = “N”;
if ($wind_dir == “South”)
$wind_dir = “S”;
if ($wind_dir == “East”)
$wind_dir = “E”;
if ($wind_dir == “West”)
$wind_dir = “W”;
echo ” “.$wind_dir;
}
[/insert_php]
