Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Pogoda, dane z www.wunderground.com
Forum PHP.pl > Forum > Gotowe rozwiązania > Algorytmy, klasy, funkcje
mike
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
wojcieh
zrobiłem chyba wszystko tak jak powinno być, podmieniłem tylko dane dla wrocławia i wyskauje mi coś takiego:

Cytat
Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /srv/www/htdocs/web109/html/weather.class.php on line 5

Fatal error: Cannot instantiate non-existent class: weather in /srv/www/htdocs/web109/html/pogoda.php on line 4


co jest nie tak?? [jestem bardzo początkujący]
mike
1. Klasa działa tylko na PHP5 ( zapomniałem o tym wspomnieć winksmiley.jpg )
2. Jeżeli masz PHP5 to pokaż kod, może wkradł się jakiś błąd.
wojcieh
niestety nie mam obsługi php5 sad.gif
mam serwer od firmy t.m. benert już prawie rok i nie było na razie żadnych problemów :/
rozumiem, że nie da się przerobić tego skryptu tak, aby działał na wcześniejszych wersjach?
dag
da sie przerobic ten kod, zeby dzialal na wczesniejszych wersjach. problem tkwi w tym, ze we wczesniejszych wersjach php nie ma Simple XML i trzeba korzystac z innych narzedzi do obslugi XML.
Bakus
Komentarz z php.net:
Cytat(lajos dot arpasi at maxxlogic dot hu - 08-Oct-2004 02:31)
If you use PHP4 and miss the features of SimpleXML try MiniXML (http://minixml.psychogenic.com).
MiniXML is a php class library for generating and parsing XML.
MiniXML have similar abilities like creating XML files from Arrays and importing XML files into Arrays.
You can manipulate the XML files more easily than SimpleXML.
It saved my life:).
banola
temat co prawda z zeszłego roku ale może ktoś mi jeszcze odpisze smile.gif

więc tak, uruchomiłem sobie ładnie ten skrypt, dostaje wynik taki jak podał autor, ale od kilku dni nie mogę nauczyć się jak praktycznie wykorzystać te dane z tablicy, prosiłbym o jakiś przykład albo link do jakiegoś kursu/przewodnika/instrukcji lub ewentualnie jakby mnie ktoś mógł naprowadzić pod jakim hasłem szukać na goglach

pozdrawiam smile.gif
dr_bonzo
MANUAL: http://pl.php.net/manual/en/language.types.array.php -- O poslugiwaniu sie tablicami
Huan
Stworzylem plik Weather.class.php wklepałem zawartośc j/w (jestem poczatkujacy:)) i wyskoczylo mi cos takiego:

Warning: simplexml_load_file() [function.simplexml-load-file]: URL file-access is disabled in the server configuration in /home/users/tkbmarcin/html/test/pogoda/Weather.class.php on line 25

Warning: simplexml_load_file(http://www.wunderground.com/auto/rss_full/global/%0D%0Astations/12495.xml) [function.simplexml-load-file]: failed to open stream: no suitable wrapper could be found in /home/users/tkbmarcin/html/test/pogoda/Weather.class.php on line 25

Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity "http://www.wunderground.com/auto/rss_full/global/%0D%0Astations/12495.xml" in /home/users/tkbmarcin/html/test/pogoda/Weather.class.php on line 25

Fatal error: Call to a member function xpath() on a non-object in /home/users/tkbmarcin/html/test/pogoda/Weather.class.php on line 27

Co jest zle ?smile.gif pozdrawiam
tiraeth
Twój serwer ma zablokowane wykorzystywanie URLi jako źródła XML smile.gif
banola
ahh męcze się z tym już ponad 2 tygodnie sad.gif

jeżeli zmajstruję sobie taką przykładową tablice:

  1. <?php
  2. $test = array(
  3. 0 => 'zero',
  4. 1 => 'jeden',
  5. 2 => 'dwa',
  6. 3 => 'trzy'
  7. );
  8. echo $test[1];
  9. ?>

to wszystko jest ok smile.gif

dlaczego więc nie mogę zastosować tego samego zamieniając nazwy na te z powyższego skryptu?
nospor
@Banola z tego typu pytaniami zapraszam tu:
http://forum.php.pl/index.php?showforum=27
Nie ma co OT tutaj robic, bo ty do tablic nie umiesz się dobrać. Jak juz bedziesz zakaldal nowy topic, to pokaz jak ty probujesz sie dobrac do tych tablic smile.gif
nilfheim
dopiero uczę się php, i chciałbym spytać się jak to działa?

chciałbym zrobić coś takiego ale dotyczące newsów ze świata...
Lonas
Pozwole sobie odswiezyc temat -
mam obsluge php5 - niestety po skopiowaniu kodu otrzymuje taki komunikat :

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/users/jakarusa/public_html/CMS/Weather.class.php on line 46
mike
Forum źle wstawiło kod.
W linii 46 masz:
iconv( "UTF-8\", \"ISO-8859-2\", $arrTemp[ 1 ] );

a powinno być:
iconv( "UTF-8", "ISO-8859-2", $arrTemp[ 1 ] );


no ale to do tego to samemu można dojść.
Przecież widaż, że kolorowanie nawet się wywala.
Lonas
To chyba cos sie zminilo na underworld ..
wywala sporo bledow

http://www.coneyislandarcade.ehost.pl/CMS/test.php
mike
Format pliku się zmienił.
Teraz trzeba inaczej wyłuskiwać dane.
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.