Location: PHPKode > scripts > PHP-XML Weather > php-xml-weather/nine_day_forecast.php
<?
// code by Jon Roig (hide@address.com - http://jonroig.com)
// copyright 2003
// released under the GPL
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<?
if (!isset($zipCode)){$zipCode = 90210;}
?>

<head>
	<title>Nine Day Advance Forecast</title>
</head>

<body>

<?php
$ch = curl_init();

curl_setopt ($ch, CURLOPT_URL, "http://ejse.com/WeatherService/Service.asmx/GetNineDayForecastInfo?zipCode=$zipCode");
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$weather_input=curl_exec ($ch);
curl_close ($ch);
$weather_data = array();
$xml_current_tag_state = '';
$day_count = 0;
global $weather_data, $xml_current_tag_state;


function startElementHandler($parser,$element_name,$element_attribs)
	{
	global $xml_current_tag_state;
	$xml_current_tag_state = $element_name;
	
	}

function endElementHandler($parser,$element_name)
	{
	global $day_count, $xml_current_tag_state;
	if ($element_name == "DAY1" OR $element_name == "DAY2" OR $element_name == "DAY3" OR $element_name == "DAY4" OR $element_name == "DAY5" OR $element_name == "DAY6" OR $element_name == "DAY7" OR $element_name == "DAY8" OR $element_name == "DAY9")
		{
		$day_count++;
		}
	$xml_current_tag_state = "";
	}

function weatherDataHandler($parser, $data)
	{
	global $weather_data, $xml_current_tag_state, $day_count;
	if($xml_current_tag_state == "")
		return;
	switch ($xml_current_tag_state)
		{
		case "DAY":
			$weather_data[$day_count]["Day"] = $data;
       		break;
		case "DATE":
			$weather_data[$day_count]["Date"] = $data;
       		break;
		case "ICONINDEX":
			$weather_data[$day_count]["IconIndex"] = $data;
       		break;
		case "FORECAST":
			$weather_data[$day_count]["Forecast"] = $data;
       		break;
		case "HIGH":
			$weather_data[$day_count]["High"] = $data;
       		break;
		case "LOW":
			$weather_data[$day_count]["Low"] = $data;
       		break;
		case "PRECIPCHANCE":
			$weather_data[$day_count]["PrecipChance"] = $data;
       		break;
		default:
			$xml_current_tag_state = $element_name;
			break;
		}


	}

if (!($xml_parser = xml_parser_create()) )
	die ("Couldn't create XML parser!");

xml_set_element_handler($xml_parser, "startElementHandler", "endElementHandler");
xml_set_character_data_handler($xml_parser, "weatherDataHandler");

xml_parse($xml_parser, $weather_input);

xml_parser_free($xml_parser);

echo "<h4>Weather for: $zipCode</h4>";
for ($i=0;$i <$day_count; ++$i)
	{
	echo "<p>";
	echo "<img src=\"weather_icons/".$weather_data[$i]["IconIndex"].".gif\"  height=\"52\" width=\"52\" alt=\"".$weather_data[$i]["Forecast"]."\"><br>";
	//echo "Day = ".$weather_data[$i]["Day"]."<br>";
	echo "Date = ".$weather_data[$i]["Date"]."<br>";
	
	echo "Forecast = ".$weather_data[$i]["Forecast"]."<br>";
	echo "High = ".str_replace("°","&deg;",$weather_data[$i]["High"])."<br>";
	echo "Low = ".str_replace("°","&deg;",$weather_data[$i]["Low"])."<br>";
	echo "PrecipChance = ".$weather_data[$i]["PrecipChance"]."<br>";
	echo "</p>";
	}


?>

</body>
</html>
Return current item: PHP-XML Weather