User Tools

Site Tools


web_development:weather_conditions

I needed a way to display just the current weather conditions on a web site with no branding, no widgets, no external javascript, etc. so I created a quick RSS connector for Environment Canada.

Grab the icon pack here: http://lavana.deviantart.com/art/Flat-Weather-Icons-32021664

Extract the chosen colored icon set into the ./icons folder (relative to this script).

Find your city at http://www.weatheroffice.gc.ca/canada_e.html and click the RSS button at the bottom to get your RSS link.

Modify this script accordingly, and enjoy!

weather_conditions.php
<?php
 
// Environment Canada Weather Icon display by Robbie Ferguson
// Shows current conditions based on EC current conditions
// October 2009
 
$file = "http://text.weatheroffice.gc.ca/rss/city/on-151_e.xml"; // the RSS file to environment canada for your city
$size = "30"; // how many pixels you want your icons to be
 
function contents($parser, $data){
 
global $forecast;
if (strstr($data, "Current Conditions:")) {
$forecast[temp] = str_ireplace("Current Conditions: ", "", $data);
$forecast[temp] .= "&deg;C";
}
 
 
// set the default
if (!$forecast[what]) {
  $forecast[what] = "Unknown";
  $forecast[icon] = "na.png";
}
 
// types of conditions, in order of override (eg. "Showers" should preceed "Cloudy" in case the forecast is "Cloudy with showers" - you want the icon to be showers, not clouds)
 
  if ($forecast[what] == "Unknown") { // because we want today's forecast, not subsequent days in the loop
 
		if (stristr($data, "snow")) {
		  $forecast[what] = "Snow";
		  $forecast[icon] = "14.png";
		}
 
		if (stristr($data, "flurries")) {
		  $forecast[what] = "Flurries";
		  $forecast[icon] = "16.png";
		}
 
		if (stristr($data, "rain")) {
		  $forecast[what] = "Rain";
		  $forecast[icon] = "12.png";
		}
 
		if (stristr($data, "showers")) {
		  $forecast[what] = "Showers";
		  $forecast[icon] = "9.png";
		}
 
		if (stristr($data, "cloudy")) {
		  $forecast[what] = "Cloudy";
		  $forecast[icon] = "26.png";
		}
 
	}
 
}
 
$xml_parser = xml_parser_create();
 
xml_set_element_handler($xml_parser, "", "");
 
xml_set_character_data_handler($xml_parser, "contents");
 
$fp = fopen($file, "r");
 
$data = fread($fp, 80000);
 
if(!(xml_parse($xml_parser, $data, feof($fp)))){
    die("Error on line " . xml_get_current_line_number($xml_parser));
}
 
xml_parser_free($xml_parser);
 
fclose($fp);
 
echo '<div style="width:' . ($size+5) . 'px;text-align:center;font-size:' . (round($size/3.75)) . 'px;font-family:Verdana;">' . chr(13);
echo ' <img height="' . $size . '" width="' . $size . '" alt="' . $forecast[what] . '" title="' . $forecast[what] . '" src="icons/' . $forecast[icon] . '">' . chr(13);
echo ' <br /><strong>' . $forecast[temp] . '</strong>' . chr(13);
echo '</div>' . chr(13);
 
?>
web_development/weather_conditions.txt · Last modified: 2012/01/26 09:01 by import