Klasa pobiera aktualną pogodę z serwisu
http://weather.yahoo.com/<?php
class pogoda{
private $parametry = array(); private $wartosci = array(); private $channel = '';
function __construct($miasto){
$wartosci__ = array('item/yweather:condition', 'yweather:wind', 'yweather:atmosphere', 'yweather:astronomy');
$this -> channel = $channel = simplexml_load_file($miasto);
$this -> pobierz_parametry();
$this -> pobierz_wartosci($wartosci__);
$this -> pobierz_url_obrazka();
}
private function pobierz_parametry(){
$this -> parametry = $this -> pobierz_('yweather:units');
}
private function pobierz_($param){
$wind = $this->channel -> xpath('//channel/'.$param);
foreach ($wind as $k => $v){
foreach($v -> attributes() as $k => $v){
$tmp[$k] = (string)$v;
}
}
return $tmp;
}
public function parametry(){
return (array)$this->parametry; }
public function wartosci(){
return (array)$this->wartosci; }
private function pobierz_wartosci($w){
foreach($w as $w_){
$this -> polacz($this -> pobierz_($w_));
}
return true;
}else{
return false;
}
}
private function polacz($param){
foreach($param as $p => $v){
$this -> wartosci[$p] = $v;
}
}
private function pobierz_url_obrazka(){
$this -> wartosci['url'] = 'http://l.yimg.com/us.yimg.com/i/us/we/52/'.$this -> wartosci['code'].'.gif';
}
}
?>
Obiekt klasy (przyklad dla Zielonej Góry):
<?php
$pogoda = new pogoda('http://xml.weather.yahoo.com/forecastrss?p=PLXX0043&u=c');
$parametry = $pogoda -> parametry();
$wartosci = $pogoda -> wartosci();
?>
Wynik:
Kod
Array
(
[temperature] => C
[distance] => km
[pressure] => mb
[speed] => kph
)
Array
(
[text] => Clear
[code] => 31
[temp] => 14
[date] => Mon, 16 Apr 2007 11:00 pm CEST
[chill] => 14
[direction] => 160
[speed] => 11
[humidity] => 40
[visibility] => 32000
[pressure] => 1020
[rising] => 2
[sunrise] => 6:00 am
[sunset] => 7:56 pm
[url] => http://l.yimg.com/us.yimg.com/i/us/we/52/31.gif
)
Klasa wyłącznie pod PHP5
Pozdrawiam