Location: PHPKode > scripts > Weather.com API Class > Weather.class.php
<?php
/**
 * Weather
 * 
 * Class to comunicate with Weather.com API to receive weather informations.
 * 
 * @method string|array getResult       return weather information
 * 
 * @see    http://www.weather.com
 * @author Rafael Santiago
 * @since 19/07/2010
 */
class Weather {
	private $resultados = array();
	private $locId;
	private $partnerId;
	private $key;
	private $xml;
	
	function __construct($locId, $partnerId, $key) {
		$this->locId = $locId;
		$this->partnerId = $partnerId;
		$this->key = $key;
		
		try {
			$url = 'http://xoap.weather.com/weather/local/'.$this->locId.'?cc=*&link=xoap&prod=xoap&par='.$this->partnerId.'&key='.$this->key.'&unit=m';
			$ch = curl_init();
			curl_setopt ($ch, CURLOPT_URL, $url);
			curl_setopt ($ch, CURLOPT_HEADER, 0);
			ob_start();
			curl_exec ($ch);
			curl_close ($ch);
			$this->xml = ob_get_contents();
			ob_end_clean();
			
			$this->process();
			
			return true;
		}
		catch (Exception $e){
			return false;
		}
	}
	
	private function process(){
		$xml = simplexml_load_string($this->xml);
		$valores = $this->xml2array($xml);
		
		$this->resultados["LATITUDE"] = $valores["children"]["loc"]["children"]["lat"]["value"];
		$this->resultados["LONGITUDE"] = $valores["children"]["loc"]["children"]["lon"]["value"];
		$this->resultados["SUNRISE"] = $valores["children"]["loc"]["children"]["sunr"]["value"];
		$this->resultados["SUNSET"] = $valores["children"]["loc"]["children"]["suns"]["value"];
		$this->resultados["TEMPERATURE"] = $valores["children"]["cc"]["children"]["tmp"]["value"];
		$this->resultados["FEELSLIKE"] = $valores["children"]["cc"]["children"]["flik"]["value"];
		$this->resultados["DESCRIPTION"] = $valores["children"]["cc"]["children"]["t"]["value"];
		$this->resultados["IMAGECODE"] = $valores["children"]["cc"]["children"]["icon"]["value"];
		$this->resultados["WINDSPEED"] = $valores["children"]["cc"]["children"]["wind"]["children"]["s"]["value"];
		$this->resultados["WINDDIRECTION"] = $valores["children"]["cc"]["children"]["wind"]["children"]["t"]["value"];
		$this->resultados["HUMIDITY"] = $valores["children"]["cc"]["children"]["hmid"]["value"];
		$this->resultados["UVLEVEL"] = $valores["children"]["cc"]["children"]["uv"]["children"]["i"]["value"];
	}
	
	private function xml2array($xml) {
		$arXML=array();
		$arXML['name']=trim($xml->getName());
		$arXML['value']=trim((string)$xml);
		$t=array();
		foreach($xml->attributes() as $name => $value) $t[$name]=trim($value);
		$arXML['attr']=$t;
		$t=array();
		foreach($xml->children() as $name => $xmlchild) $t[$name]=$this->xml2array($xmlchild);
		$arXML['children']=$t;
		return($arXML);
	}
	
	/*
	*	Returns weather information.
	*
	*	Possible values: 
	*	LATITUTE | LONGITUDE | SUNRISE | SUNSET | TEMPERATURE | FEELSLIKE | DESCRIPTION | IMAGECODE | WINDSPEED | WINDDIRECTION | HUMIDITY | UVLEVEL
	*
	*	@param array|string $op      Weather information variable (String or array of Strings)
	*	@return array|string
	* 	@author Rafael Santiago
	* 	@since 19/07/2010
	*
	*/
	public function getResult($op = 'TEMPERATURE'){
		if (is_array($op)){
			$retorno = array();
			foreach ($op as $ops){
				$retorno[$ops] = $this->getResult($ops);
			}
			return $retorno;
		}
		else {
			return $this->resultados[$op];
		}
	}
	
}
?>
Return current item: Weather.com API Class