Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Pogoda, dane z www.wunderground.com, Klasa, PHP 5
mike
post 27.03.2005, 23:24:37
Post #1





Grupa: Przyjaciele php.pl
Postów: 7 494
Pomógł: 302
Dołączył: 31.03.2004

Ostrzeżenie: (0%)
-----


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:
  1. <?php
  2. require_once( 'Weather.class.php' );
  3.  
  4. $objWeather = new Weather( 'http://www.wunderground.com/auto/rss_full/global/
  5. stations/12495.xml' ); // pogoda dla Lublina
  6. print_r( $objWeather->GetConditions() );
  7.  
  8. $objWeather2 = new Weather( 'http://www.wunderground.com/auto/rss_full/global/stations/12495.xml', false );
  9. print_r( $objWeather2->GetConditions() );
  10.  
  11. ?>

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:
  1. <?php
  2.  
  3. class Weather
  4. {
  5. private $strFilePath = '';
  6.  
  7. private $strContent  = '';
  8. private $blnMetricUnits = true;
  9. private $arrContent  = array();
  10. private $arrConditions = array();
  11.  
  12.  
  13. /**
  14.  * Konstruktor klasy
  15.  *
  16.  * @param string $strFilePath Plik kanału RSS z http://wunderground.com z pogodą dla danego miasta
  17.  * @param boolean $blnMetricUnits System metryczny: true - \"nasz\", false - brytyjski
  18.  * @return void
  19.  */
  20. public function Weather( $strFilePath, $blnMetricUnits = true )
  21. {
  22. $this->strFilePath = $strFilePath;
  23. $this->blnMetricUnits = $blnMetricUnits;
  24.  
  25. $objSimpleXMLElement = simplexml_load_file( $this->strFilePath );
  26.  
  27. $arrElements = $objSimpleXMLElement->xpath( '//channel/item/description' );
  28.  
  29. $this->strContent = ( string )$arrElements[ 0 ];
  30.  
  31. $this->ExplodeContent();
  32. $this->CreateContent();
  33. }
  34.  
  35. /**
  36.  * Wstępna obróbka informacji z kanału RSS
  37.  */
  38. private function ExplodeContent()
  39. {
  40. $arrContent = explode( '|', $this->strContent );
  41.  
  42. foreach( $arrContent as $strValue )
  43. {
  44. $arrTemp = explode( ':', trim( $strValue ), 2 );
  45.  
  46. $this->arrContent[ strtolower( str_replace( ' ', '', $arrTemp[ 0 ] ) ) ] = iconv( &#092;"UTF-8\", \"ISO-8859-2\", $arrTemp[ 1 ] );
  47. }
  48. }
  49.  
  50. /**
  51.  * Wydobycie z kanału RSS informacji o pogodzie i utworzenie tablicy zawier
  52. jące te informacje
  53.  */
  54. private function CreateContent()
  55. {
  56. foreach( $this->arrContent as $strKey => $strValue )
  57. {
  58. if( strpos( $strValue, '/' ) != 0 )
  59. {
  60. $arrTemp = explode( '/', trim( $strValue ), 2 );
  61.  
  62. $intIndex = ( $this->blnMetricUnits ) ? 1 : 0;
  63. $this->arrConditions[ $strKey ] = trim( $arrTemp[ $intIndex ] );
  64. }
  65. else
  66. {
  67. $this->arrConditions[ $strKey ] = trim( $strValue );
  68. }
  69. }
  70.  
  71. $intTime = strtotime( $this->arrConditions[ 'updated' ] );
  72. $this->arrConditions[ 'updated' ] = date( 'H:i j.m.Y', $intTime );
  73. }
  74.  
  75. /**
  76.  * Metoda zwracająca dane o pogodzie
  77.  *
  78.  * @return array
  79.  */
  80. public function GetConditions()
  81. {
  82. return $this->arrConditions;
  83. }
  84. }
  85.  
  86. ?>

Proszę o sugestie i uwagi winksmiley.jpg
Go to the top of the page
+Quote Post

Posty w temacie
- mike_mech   Pogoda, dane z www.wunderground.com   27.03.2005, 23:24:37
- - wojcieh   zrobiłem chyba wszystko tak jak powinno być, podmi...   3.04.2005, 10:33:18
- - mike_mech   1. Klasa działa tylko na PHP5 ( zapomniałem o tym...   3.04.2005, 11:09:26
- - wojcieh   niestety nie mam obsługi php5 mam serwer od firmy...   3.04.2005, 12:02:14
- - dag   da sie przerobic ten kod, zeby dzialal na wczesnie...   29.05.2005, 07:17:11
- - Bakus   Komentarz z php.net: Cytat(lajos dot arpasi at max...   29.05.2005, 14:41:22
- - banola   temat co prawda z zeszłego roku ale może ktoś mi j...   10.03.2006, 00:59:12
- - dr_bonzo   MANUAL: http://pl.php.net/manual/en/language.types...   10.03.2006, 02:17:40
- - Huan   Stworzylem plik Weather.class.php wklepałem zawart...   29.03.2006, 19:10:31
- - tiraeth   Twój serwer ma zablokowane wykorzystywanie URLi ja...   29.03.2006, 20:20:23
- - banola   ahh męcze się z tym już ponad 2 tygodnie jeżeli ...   29.03.2006, 21:25:31
- - nospor   @Banola z tego typu pytaniami zapraszam tu: http:/...   30.03.2006, 07:37:49
- - nilfheim   dopiero uczę się php, i chciałbym spytać się jak t...   19.05.2006, 13:54:34
- - Lonas   Pozwole sobie odswiezyc temat - mam obsluge php5 -...   20.01.2007, 16:37:38
- - mike_mech   Forum źle wstawiło kod. W linii 46 masz: iconv( ...   20.01.2007, 17:38:36
- - Lonas   To chyba cos sie zminilo na underworld .. wywala s...   20.01.2007, 18:53:13
- - mike_mech   Format pliku się zmienił. Teraz trzeba inaczej wył...   20.01.2007, 19:48:31


Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 16.08.2025 - 05:14