Szukałem ostatnio dobrego skryptu pogody i nie znalazłem.
Więc pozostało mi napisanie go samemu.
Klasa zaciąga dane z kanału RSS strony
www.wunderground.com oraz przedstawia dane w postaci przejrzystej tablicy.
Oto przykład zastosowania:
<?php
require_once( 'Weather.class.php' );
$objWeather = new Weather( 'http://www.wunderground.com/auto/rss_full/global/
stations/12495.xml' ); // pogoda dla Lublina
print_r( $objWeather->GetConditions() );
$objWeather2 = new Weather( 'http://www.wunderground.com/auto/rss_full/global/stations/12495.xml', false );
print_r( $objWeather2->GetConditions() );
?>
Wynik:
Cytat
Array
(
[temperature] => 4°C
[humidity] => 78%
[pressure] => 1013hPa
[conditions] =>
[winddirection] => NE
[windspeed] => 11km/h
[updated] => 23:00 28.03.2005
)
Array
(
[temperature] => 39°F
[humidity] => 78%
[pressure] => 29.93in
[conditions] =>
[winddirection] => NE
[windspeed] => 7mph
[updated] => 23:00 28.03.2005
)
I wreszcie kod klasy:
<?php
class Weather
{
private $strFilePath = '';
private $strContent = '';
private $blnMetricUnits = true;
private $arrContent = array(); private $arrConditions = array();
/**
* Konstruktor klasy
*
* @param string $strFilePath Plik kanału RSS z http://wunderground.com z pogodą dla danego miasta
* @param boolean $blnMetricUnits System metryczny: true - \"nasz\", false - brytyjski
* @return void
*/
public function Weather( $strFilePath, $blnMetricUnits = true )
{
$this->strFilePath = $strFilePath;
$this->blnMetricUnits = $blnMetricUnits;
$objSimpleXMLElement = simplexml_load_file( $this->strFilePath );
$arrElements = $objSimpleXMLElement->xpath( '//channel/item/description' );
$this->strContent = ( string )$arrElements[ 0 ];
$this->ExplodeContent();
$this->CreateContent();
}
/**
* Wstępna obróbka informacji z kanału RSS
*/
private function ExplodeContent()
{
$arrContent = explode( '|', $this->strContent );
foreach( $arrContent as $strValue )
{
$this->arrContent[ strtolower( str_replace( ' ', '', $arrTemp[ 0 ] ) ) ] = iconv
( \"UTF-8\", \"ISO-8859-2\", $arrTemp[ 1 ] ); }
}
/**
* Wydobycie z kanału RSS informacji o pogodzie i utworzenie tablicy zawier
jące te informacje
*/
private function CreateContent()
{
foreach( $this->arrContent as $strKey => $strValue )
{
if( strpos( $strValue, '/' ) != 0
) {
$intIndex = ( $this->blnMetricUnits ) ? 1 : 0;
$this->arrConditions[ $strKey ] = trim( $arrTemp[ $intIndex ] ); }
else
{
$this->arrConditions[ $strKey ] = trim( $strValue ); }
}
$intTime = strtotime( $this->arrConditions[ 'updated' ] ); $this->arrConditions[ 'updated' ] = date( 'H:i j.m.Y', $intTime ); }
/**
* Metoda zwracająca dane o pogodzie
*
* @return array
*/
public function GetConditions()
{
return $this->arrConditions;
}
}
?>
Proszę o sugestie i uwagi