Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> 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
wojcieh
post 3.04.2005, 10:33:18
Post #2





Grupa: Zarejestrowani
Postów: 3
Pomógł: 0
Dołączył: 3.04.2005
Skąd: Wrocław

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


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]


--------------------
aklasa makrofotografia anteny tv
Go to the top of the page
+Quote Post
mike
post 3.04.2005, 11:09:26
Post #3





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

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


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.
Go to the top of the page
+Quote Post
wojcieh
post 3.04.2005, 12:02:14
Post #4





Grupa: Zarejestrowani
Postów: 3
Pomógł: 0
Dołączył: 3.04.2005
Skąd: Wrocław

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


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?


--------------------
aklasa makrofotografia anteny tv
Go to the top of the page
+Quote Post
dag
post 29.05.2005, 07:17:11
Post #5





Grupa: Zarejestrowani
Postów: 180
Pomógł: 0
Dołączył: 24.12.2003

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


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.


--------------------

------------------------------------------------------------------------------------------------------
Go to the top of the page
+Quote Post
Bakus
post 29.05.2005, 14:41:22
Post #6


Administrator serwera


Grupa: Przyjaciele php.pl
Postów: 909
Pomógł: 0
Dołączył: 12.08.2003
Skąd: /var/www/wroclaw.php

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


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:).


--------------------
Powrót do przeszłości :)
Go to the top of the page
+Quote Post
banola
post 10.03.2006, 00:59:12
Post #7





Grupa: Zarejestrowani
Postów: 2
Pomógł: 0
Dołączył: 10.03.2006

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


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


--------------------
TORUŃ
Go to the top of the page
+Quote Post
dr_bonzo
post 10.03.2006, 02:17:40
Post #8





Grupa: Przyjaciele php.pl
Postów: 5 724
Pomógł: 259
Dołączył: 13.04.2004
Skąd: N/A

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


MANUAL: http://pl.php.net/manual/en/language.types.array.php -- O poslugiwaniu sie tablicami


--------------------
Nie lubię jednorożców.
Go to the top of the page
+Quote Post
Huan
post 29.03.2006, 19:10:31
Post #9





Grupa: Zarejestrowani
Postów: 3
Pomógł: 0
Dołączył: 29.03.2006

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


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
Go to the top of the page
+Quote Post
tiraeth
post 29.03.2006, 20:20:23
Post #10





Grupa: Przyjaciele php.pl
Postów: 1 789
Pomógł: 41
Dołączył: 30.10.2003
Skąd: Wrocław

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


Twój serwer ma zablokowane wykorzystywanie URLi jako źródła XML smile.gif
Go to the top of the page
+Quote Post
banola
post 29.03.2006, 21:25:31
Post #11





Grupa: Zarejestrowani
Postów: 2
Pomógł: 0
Dołączył: 10.03.2006

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


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?


--------------------
TORUŃ
Go to the top of the page
+Quote Post
nospor
post 30.03.2006, 07:37:49
Post #12





Grupa: Moderatorzy
Postów: 36 557
Pomógł: 6315
Dołączył: 27.12.2004




@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


--------------------

"Myśl, myśl, myśl..." - Kubuś Puchatek || "Manual, manual, manual..." - Kubuś Programista
"Szukaj, szukaj, szukaj..." - Kubuś Odkrywca || "Debuguj, debuguj, debuguj..." - Kubuś Developer

Go to the top of the page
+Quote Post
nilfheim
post 19.05.2006, 13:54:34
Post #13





Grupa: Zarejestrowani
Postów: 8
Pomógł: 0
Dołączył: 5.04.2006

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


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...


--------------------
Remember, remember the fifth of November
The gunpowder treason and plot.
I see no reason why gunpowder treason
Should ever be forgot.
Go to the top of the page
+Quote Post
Lonas
post 20.01.2007, 16:37:38
Post #14





Grupa: Zarejestrowani
Postów: 576
Pomógł: 14
Dołączył: 9.11.2005

Ostrzeżenie: (20%)
X----


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


--------------------
Go to the top of the page
+Quote Post
mike
post 20.01.2007, 17:38:36
Post #15





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

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


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.
Go to the top of the page
+Quote Post
Lonas
post 20.01.2007, 18:53:13
Post #16





Grupa: Zarejestrowani
Postów: 576
Pomógł: 14
Dołączył: 9.11.2005

Ostrzeżenie: (20%)
X----


To chyba cos sie zminilo na underworld ..
wywala sporo bledow

http://www.coneyislandarcade.ehost.pl/CMS/test.php


--------------------
Go to the top of the page
+Quote Post
mike
post 20.01.2007, 19:48:31
Post #17





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

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


Format pliku się zmienił.
Teraz trzeba inaczej wyłuskiwać dane.
Go to the top of the page
+Quote Post

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: 12.06.2025 - 22:51