<?php
//data:
//link naar rss feed, te genereren op http://weather.yahoo.com/. Je geeft daar je woonplaats in en dan krijg je een nieuwsbericht van in jouw omgeving.
// Je kiest vervolgens het weer in graden Celsius
// dan klik je op de knop RSS-feed en kopieer je de URL hier
$rss = "http://weather.yahooapis.com/forecastrss?p=BEXX0013&u=c";

//link naar de afbeeldingen van de yeahoo site. Je kan dus ook je eigen afbeeldingen maken als je deze link aanpast
$imgsrc = "http://l.yimg.com/a/i/us/nws/weather/gr/";

//weercodes:
//je kan hier vertalingen of weerspreuken toevoegen. Dit is de standaard lijst van Yahoo.
$codes[0] = "tornado";
$codes[1] = "tropical storm";
$codes[2] = "hurricane";
$codes[3] = "severe thunderstorms";
$codes[4] = "thunderstorms";
$codes[5] = "mixed rain and snow";
$codes[6] = "mixed rain and sleet";
$codes[7] = "mixed snow and sleet";
$codes[8] = "freezing drizzle";
$codes[9] = "drizzle";
$codes[10] = "freezing rain";
$codes[11] = "showers";
$codes[12] = "showers";
$codes[13] = "snow flurries";
$codes[14] = "light snow showers";
$codes[15] = "blowing snow";
$codes[16] = "snow";
$codes[17] = "hail";
$codes[18] = "sleet";
$codes[19] = "dust";
$codes[20] = "foggy";
$codes[21] = "haze";
$codes[22] = "smoky";
$codes[23] = "blustery";
$codes[24] = "windy";
$codes[25] = "cold";
$codes[26] = "cloudy";
$codes[27] = "mostly cloudy"; //(night)
$codes[28] = "mostly cloudy"; //(day)
$codes[29] = "partly cloudy"; //(night)
$codes[30] = "partly cloudy"; //(day)
$codes[31] = "clear"; //(night)
$codes[32] = "sunny";
$codes[33] = "fair"; //(night)
$codes[34] = "fair"; // (day)
$codes[35] = "mixed rain and hail";
$codes[36] = "hot";
$codes[37] = "isolated thunderstorms";
$codes[38] = "scattered thunderstorms";
$codes[39] = "scattered thunderstorms";
$codes[40] = "scattered showers";
$codes[41] = "heavy snow";
$codes[42] = "scattered snow showers";
$codes[43] = "heavy snow";
$codes[44] = "partly cloudy";
$codes[45] = "thundershowers";
$codes[46] = "snow showers";
$codes[47] = "isolated thundershowers";
$codes[3200] = "not available";



//verwerking:
$xml = new SimpleXMLElement($rss, null, true);
if($xml) {

	//data ophalen
	//<yweather:condition  text="Partly Cloudy"  code="29"  temp="46"  date="Sat, 07 Mar 2009 7:20 pm CET" /> 
	$data = $xml->xpath("*/item/yweather:condition");
	foreach($data[0]->attributes() as $key =>$data) {
		$$key = $data;
	}
	
	//zien of het al licht is of niet:
	//<yweather:astronomy sunrise="7:14 am"   sunset="6:33 pm"/>
	$data = $xml->xpath("*/yweather:astronomy");
	foreach($data[0]->attributes() as $key =>$data) {
		$$key = $data;
	}
	
	$sunrise = strtotime(preg_replace("/[0-9]{1,2}\:[0-9]{1,2}\s[amp]{2}/i", $sunrise, $date));
	$sunset = strtotime(preg_replace("/[0-9]{1,2}\:[0-9]{1,2}\s[amp]{2}/i", $sunset, $date));
	$now = time();
	$moment = ($now >= $sunrise && $now <= $sunset) ? "d" : "n";
		
	
	//output
	$img = $imgsrc.$code.$moment.".png";
	
	echo('
	<div style="width: 163px; height: 140px; margin: -1px -5px -18px -5px; background:url(\''.$img.'\'); background-position: -3px 10px; text-align: center;"><strong>Temperatuur: '.$temp.'&deg;C</strong><br />'.$codes[intval($code)].'</div>
	<div>&nbsp;</div>
	');
	
	
	//IE img hack: (werkt niet met background position, daarom niet toegevoeg aan het script..
	//_background-image/* */: none; filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\''.$img.'\', sizingMethod=\'crop\');

}


/*

VOORBEELD VAN EEN RSS response:


<?xml version="1.0" encoding="UTF-8" standalone="yes" ? >
<rss version="2.0" xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#">
<channel>

<title>Yahoo! Weather - Mechelen, BE</title>
<link>http://us.rd.yahoo.com/dailynews/rss/weather/Mechelen__BE/*http://weather.yahoo.com/forecast/BEXX0013_f.html</link>
<description>Yahoo! Weather for Mechelen, BE</description>
<language>en-us</language>
<lastBuildDate>Sat, 07 Mar 2009 7:20 pm CET</lastBuildDate>
<ttl>60</ttl>
<yweather:location city="Mechelen" region=""   country="BE"/>
<yweather:units temperature="F" distance="mi" pressure="in" speed="mph"/>
<yweather:wind chill="41"   direction="210"   speed="10" />
<yweather:atmosphere humidity="81"  visibility="6.21"  pressure="29.88"  rising="0" />
<yweather:astronomy sunrise="7:14 am"   sunset="6:33 pm"/>
<image>
<title>Yahoo! Weather</title>
<width>142</width>
<height>18</height>
<link>http://weather.yahoo.com</link>
<url>http://l.yimg.com/a/i/us/nws/th/main_142b.gif</url>
</image>
<item>
<title>Conditions for Mechelen, BE at 7:20 pm CET</title>
<geo:lat>51.03</geo:lat>
<geo:long>4.48</geo:long>
<link>http://us.rd.yahoo.com/dailynews/rss/weather/Mechelen__BE/*http://weather.yahoo.com/forecast/BEXX0013_f.html</link>
<pubDate>Sat, 07 Mar 2009 7:20 pm CET</pubDate>
<yweather:condition  text="Partly Cloudy"  code="29"  temp="46"  date="Sat, 07 Mar 2009 7:20 pm CET" />
<description><![CDATA[
<img src="http://l.yimg.com/a/i/us/we/52/29.gif"/><br />
<b>Current Conditions:</b><br />
Partly Cloudy, 46 F<BR />
<BR /><b>Forecast:</b><BR />
Sat - Light Rain Late. High: 50 Low: 44<br />
Sun - Showers. High: 49 Low: 37<br />
<br />
<a href="http://us.rd.yahoo.com/dailynews/rss/weather/Mechelen__BE/*http://weather.yahoo.com/forecast/BEXX0013_f.html">Full Forecast at Yahoo! Weather</a><BR/>
(provided by The Weather Channel)<br/>
]]></description>
<yweather:forecast day="Sat" date="7 Mar 2009" low="44" high="50" text="Light Rain Late" code="11" />
<yweather:forecast day="Sun" date="8 Mar 2009" low="37" high="49" text="Showers" code="11" />
<guid isPermaLink="false">BEXX0013_2009_03_07_19_20_CET</guid>
</item>
</channel>
</rss><!-- api1.weather.re4.yahoo.com compressed/chunked Sat Mar  7 10:36:33 PST 2009 -->


*/

?>