Location: PHPKode > scripts > MyWeather > myweather/MyWeather.Class.php
<?php

/*********************************************************************************************
     ___       ___  ___   _   _____           _       ___   _____      ___   _____    _ 
    /   |     /   |/   | | | |  _  \         | |     /   | |  ___|    /   | |  _  \  | |
   / /| |    / /|   /| | | | | |_| |         | |    / /| | | |__     / /| | | |_| |  | |
  / /_| |   / / |__/ | | | | |  _  /      _  | |   / /_| | |  __|   / /_| | |  _  /  | |
 / /  | |  / /       | | | | | | \ \     | |_| |  / /  | | | |     / /  | | | | \ \  | | _
/_/   |_| /_/        |_| |_| |_|  \_\    \_____/ /_/   |_| |_|    /_/   |_| |_|  \_\ |_||_|

**********************************************************************************************

* Author		Amir Jafari.
* Website		www.Aloche.ir
* Email			hide@address.com
* Script Name	MyWeather Viewer
* Copyright(C)	2008 Amir Jafari, All rights reserved.

**********************************************************************************************/

class MyWeather 
{
	var $data;
	var $saved;
		
	//---
	function getWeather($zip)
	{
		$file = "tmp/".$zip.".xml";
		if( !file_exists($file) )
		{	
			$content = @file_get_contents ("http://www.wunderground.com/cgi-bin/findweather/getForecast?query=".$zip);
			if ( preg_match('/href="(.*)"/', $content, $matches) )
			{
				$cityFeed = $matches[1];
			}
			// write new data in file
			$fp = @fopen ($file, 'w');
			@fwrite ($fp, $cityFeed);
			@fclose ($fd);
		}
		else
		{
			// get data from file
			$cityFeed = @file_get_contents ($file);
		}
		
		if ( $cityFeed )
		{
			// get the feed contents
			$this->data = @file_get_contents($cityFeed);
	
			// get the needed data
			$t1 = $this->tag($this->data, "<item>", "</item>");
			$t = $this->tag($t1, "<description>", "</description>");
			$this->saved = explode ("|", $t);
				
			$return = array();
			// get location
			$state = strtoupper(substr($zip, 0, 1)).strtolower(substr($zip, 1, strlen($zip)));
			$return['Location'] = $state;
			
			// get temperture
			$tmp = explode("/", $this->saved[0]);
			$return['Temp'] = $tmp[1];
			
			// get humidity
			$hum = str_replace("Humidity: ", "", $this->saved[1]);
			$return['Humidity'] = $hum;
			
			// get preasure
			$pre = explode("/", $this->saved[2]);
			$return['Preasure'] = $pre[1];
		}
		else
		{
			$return = "Error in connection!";
		}
		
		return $return;
	}
	
	//---
	function tag($text, $tag1, $tag2)
	{
		$tag1_len = strlen($tag1);
		$seek1 = strstr($text, $tag1);
		$seek2 = strstr($text, $tag2);
		if($seek1 & $seek2) 
		{
			$pos1 = strpos($text, $tag1);
			$pos2 = strpos($text, $tag2);
			$split_len = $pos2 - $pos1;
			$text = substr($text, $pos1, $split_len);
			$text = substr($text, $tag1_len);
			return $text;
		}
	}
}
?>
Return current item: MyWeather