<?
// Fetch weather.com 

	// Please fill in the next 3 Values
	$partner_ID 	= 	"";
	$license_key 	=	"";
	$loc_id			=	"";
	$length 			=	2;				//Forecast in days 
	$xml				=	"";
	$url 				= "http://xoap.weather.com/weather/local/$loc_id?cc=*&dayf=$length&prod=xoap&par=$partner_ID&key=$license_key&unit=m";
	

	$fp = fopen($url,"r");
	while (!feof ($fp))
	$xml .= fgets($fp, 4096);
	fclose ($fp);
	
		
		// XML PARSER 
		$parser = xml_parser_create(  ); 
		xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);

		// Tag Names and values 
		xml_parse_into_struct($parser,$xml,$values,$index); 

		// Closedown parser
		xml_parser_free($parser);
		
		function translateEN_DE(&$satz)
		{
		//Translate
				$search = array('Light', 'Rain','Early','AM','Showers','Partly','Cloudy','Mostly','Sunny');
				$replace = array(" Leichte(r) ", " Regen ","Morgens",'Vormittags','Regenfälle','Teilweise','Bewölkt','Meistens','Sonnig');
				$satz = str_replace($search, $replace, $satz);
				
				return($satz) ;
		}
		
		//Collect Data
		
			for ($i=0; $i<$length; $i++)
			{	
				$sunrise[] 		= 	$values[$index['sunr'][$i]]['value'];
				$sunset []			= 	$values[$index['suns'][$i]]['value'];
				$temp_hi[]		= 	$values[$index['hi'][$i]]['value'];
				$temp_low[]		= 	$values[$index['low'][$i]]['value'];
			}
		
			//Icons 
			for ($i=0; $i<(($length*2)+1); $i++)
			{
				if (preg_match("/-/i", $values[$index['icon'][$i]]['value']))
				{
					$icons[]			= "na";
				}
				else 
				{
					$icons[]			=	$values[$index['icon'][$i]]['value'];
				}
			}
		
			//Description and translate
			for ($i=3; $i<($length*5); $i++)
			{	
				$descript[]		= translateEN_DE(&$values[$index['t'][$i]]['value']);
				$i++;			
			}
		
		//ENDE VORAUSSAGEN
		
		//Actual Weather Data
		$act[]		= 	$values[$index['tmp'][0]]['value'];
		$act[]		=	translateEN_DE(&$values[$index['t'][0]]['value']);
		$act[]		=	$values[$index['icon'][0]]['value'];
		
		
		//Collecting relevant Data
		$x=0;
		$y=1;
		for ($i=0; $i<$length; $i++)
		{
			/*
			Der fertige array sieht folgendermassen aus:
			0  => Sunrise
			1	=> Sunset 
			2	=> MAx Temperature Day
			3	=> Description Dasy
			4	=> Icon DAy
			5	=> Min Temperature Night
			6	=> Description Night
			7	=> Icon Night
			*/		
			
			//Sunrise Sunset
			$day[$i][]		= 	$sunrise[$i];
			$day[$i][]		= 	$sunset[$i];
			
			//Day
			$day[$i][]		= 	$temp_hi[$i];
			$day[$i][]		= 	$descript[$x];
			$day[$i][]		= 	$icons[$y];
			
			//Night	
			$day[$i][]		= 	$temp_low[$i];
			$day[$i][]		= 	$descript[$x+1];
			$day[$i][]		= 	$icons[$y+1];	
		$x+=2;
		$y+=2;
		}
		
		
		/*DEBUG
		echo ("<pre>");
		print_r ($act);
		echo ("</pre>");*/
?>