Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> RSS Maker, Klasa, PHP 4 i 5
bela
post 10.10.2004, 19:18:47
Post #1


Administrator PHPedia.pl


Grupa: Developerzy
Postów: 1 102
Pomógł: 2
Dołączył: 14.09.2003

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


Dziś popołudniu nie miałem co robić i napisałem klasa do generowania plików rss.
  1. <?php
  2. /**
  3.  * Klasa do generowania pliku RSS
  4.  * @author Piotr 'bela_666' Belina
  5.  * @package belacms
  6.  */
  7.  
  8. /**
  9.  * Klasa do generowania pliku RSS
  10.  */
  11.  
  12. class RSS {
  13.  
  14. /**
  15.  * Zmienna ustawiająca kodowanie znaków
  16.  */
  17. var $strCharset = &#092;"iso-8859-2\";
  18.  
  19. /**
  20.  * Adres pliku
  21.  */
  22. var $strURL;
  23.  
  24. /**
  25.  * Tytuł RSSa
  26.  */
  27. var $strTitle;
  28.  
  29. /**
  30.  * Adres strony
  31.  */
  32. var $strLink;
  33.  
  34. /**
  35.  * Opis kanału
  36.  */
  37. var $strDesc;
  38.  
  39. /**
  40.  * Adres obrazka
  41.  */
  42. var $mixURLImage = 0;
  43.  
  44. /**
  45.  * Adres strony pod obrazkiem
  46.  * Jeżeli $mixURLImage nie jest równy 0, to ta zmienna też musi być
  47.  */
  48. var $strImageLink;
  49.  
  50. /**
  51.  * Tablica z poszczególnymi itemami
  52.  */
  53. var $arrItems = array();
  54.  
  55. /**
  56.  * Konstruktor
  57.  */
  58. function RSS (){
  59. }
  60.  
  61. /**
  62.  * Funkcja wyświetlająca początek pliku
  63.  */
  64. function startRSS(){
  65. header(&#092;"Content-type: text/xml; charset=\" . $this->strCharset);
  66. print('<'.'?xml version=\"1.0\" encoding=\"' . $this->strCharset . '\" ?' . '>'.&#092;"n\");
  67. print('<rdf:RDF
  68. xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"
  69. xmlns=\"http://purl.org/rss/1.0/\">'.&#092;"n\");
  70. print('<channel rdf:about=\"' . $this->strURL .'\">'.&#092;"n\");
  71. print('<title>'. $this->strTitle .'</title>'.&#092;"n\");
  72. print('<link>'. $this->strLink .'</link>'.&#092;"n\");
  73. print('<description>'. $this->strDesc .'</description>'.&#092;"n\");
  74.  
  75. if($this->mixURLImage) {
  76. print('<image rdf:about=\"' . $this->mixURLImage . '\">'.&#092;"n\");
  77. print('<title>'. $strTitle .'</title>'.&#092;"n\");
  78. print('<link>'. $strImageLink .'</link>'.&#092;"n\");
  79. print('<url>'. $mixURLImage .'</url>'.&#092;"n\");
  80. print('</image>'.&#092;"n\");
  81. }
  82. }
  83.  
  84. /**
  85.  * Funkcja dodaje Item do tablicy
  86.  */
  87. function addItem($strItemLink, $strItemTitle, $strItemDesc) {
  88. $this->arrItems[] = '<item rdf:about=\"'. $strItemLink .'\">'.&#092;"n\".
  89. '<title>'.$strItemTitle.'</title>'.&#092;"n\".
  90. '<link>'.$strItemLink.'</link>'.&#092;"n\".
  91. '<description>'.$strItemDesc.'</description>'.&#092;"n\".
  92. '</item>'.&#092;"n\";
  93. }
  94.  
  95. /**
  96.  * Funkcja wyświetlająca wszystkie Itemy
  97.  */
  98. function showAll() {
  99. foreach($this->arrItems as $strItem) {
  100. print($strItem);
  101. }
  102. print('</channel>'.&#092;"n\");
  103. print '</rdf:RDF>'.&#092;"n\";
  104. }
  105. }
  106. ?>


A oto przykład wykorzystania jej
  1. <?php
  2. /* Przykład wykorzystania klasy RSS */
  3. require_once(&#092;"rss_class.php\");
  4.  
  5. $rss = new rss();
  6.  
  7. $rss->strURL = &#092;"http://localhost/rss_class/example.php\";
  8. $rss->strTitle = &#092;"BelaCMS RSS\";
  9. $rss->strLink = &#092;"http://localhost/belacms/index.php\";
  10. $rss->strDesc = &#092;"Na tym kanale będą zamieszczane informacje dotyczące BelaCMS\";
  11.  
  12. $rss->startRSS();
  13.  
  14. $rss->addItem(&#092;"http://localhost/mmcms/index.php\", \"Pierwsza wersja alfa\", \"Właśnie ukazała się pierwsza wersja alfa\");
  15.  
  16. $rss->showAll();
  17. ?>


Klasa ta jest zgodna ze specyfikacją 1.0

Zapraszam do testowania, zgłaszania propozycji zmian.


--------------------
Go to the top of the page
+Quote Post
Bakus
post 10.10.2004, 20:43:42
Post #2


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%)
-----


Zmienił bym tylko jedną rzecz, co jak na nadwornego marudę i tak jest dobrym wynikiem smile.gif
Nie dawałbym możliwości bezpiśredniego ustawinia zmiennych...
Czyli nie $klasa->zmienna "coś";
tylko
'$klasa->set_zmienna("cos");
Na tym koeniec smile.gif

9+/10 ale odrazu mówię, że jej nie testowałem, tylko przeglądnąłem kodzik... snitch.gif


--------------------
Powrót do przeszłości :)
Go to the top of the page
+Quote Post
bela
post 10.10.2004, 22:48:58
Post #3


Administrator PHPedia.pl


Grupa: Developerzy
Postów: 1 102
Pomógł: 2
Dołączył: 14.09.2003

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


Cytat
Nie dawałbym możliwości bezpiśredniego ustawinia zmiennych...


gdyby to było php5 to ok, ale w php4 nie ma przecież modyfikatorów dostępu dry.gif


--------------------
Go to the top of the page
+Quote Post
matid
post 12.10.2004, 20:56:32
Post #4





Grupa: Zarejestrowani
Postów: 362
Pomógł: 0
Dołączył: 18.02.2004
Skąd: Knurów

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


Moja wersja na PHP5:

rss.class.php:
  1. <?php
  2.  
  3.  
  4. /**
  5.  * Generator kanałów RSS
  6.  *
  7.  * @author Mateusz 'matid' Drożdżyński
  8.  * @copyright Mateusz Drożdżyński
  9.  * @version 1.0
  10.  */
  11. class RSS
  12. {
  13. /**
  14.  * Nazwa kanału RSS
  15.  */
  16. private $strTitle;
  17.  
  18. /**
  19.  * URL kanału RSS
  20.  */
  21. private $strURL;
  22.  
  23. /**
  24.  * Link do strony
  25.  */
  26. private $strLink;
  27.  
  28. /**
  29.  * Opis kanału RSS
  30.  */
  31. private $strDescription;
  32.  
  33. /**
  34.  * System kodowania znaków
  35.  */
  36. private $strCharset;
  37.  
  38. /**
  39.  * Output
  40.  */
  41. private $strOutput;
  42.  
  43. /**
  44.  * RSS::RSS
  45.  */
  46. function RSS()
  47. {
  48. $this->strTitle = '';
  49. $this->strURL = '';
  50. $this->strLink = '';
  51. $this->strDescription = '';
  52. $this->strCharset = 'ISO-8859-2';
  53. }
  54.  
  55. /**
  56.  * Ustawia nazwę kanału RSS
  57.  *
  58.  * @access public
  59.  * @param str $strTitle - nazwa kanału RSS
  60.  * @return true
  61.  */
  62. public function setTitle( $strTitle )
  63. {
  64. $this->strTitle = $strTitle;
  65. return true;
  66. }
  67.  
  68. /**
  69.  * Ustawia URL kanału RSS
  70.  *
  71.  * @access public
  72.  * @param str $strURL - URL kanału RSS
  73.  * @return true;
  74.  */
  75. public function setURL( $strURL )
  76. {
  77. $this->strURL = $strURL;
  78. return true;
  79. }
  80.  
  81. /**
  82.  * Ustawia link do strony
  83.  *
  84.  * @access public
  85.  * @param str $strLink - link do strony
  86.  * @return true;
  87.  */
  88. public function setLink( $strLink )
  89. {
  90. $this->strLink = $strLink;
  91. return true;
  92. }
  93.  
  94. public function setCharset( $strCharset )
  95. {
  96. $this->strCharset = $strCharset;
  97. }
  98.  
  99. /**
  100.  * Ustawia opis kanału RSS
  101.  *
  102.  * @access public
  103.  * @param str $strDescription - opis kanału
  104.  * @return true;
  105.  */
  106. public function setDescription( $strDescription )
  107. {
  108. $this->strDescription = $strDescription;
  109. return true;
  110. }
  111.  
  112. /**
  113.  * Dodaje dane do buforu przeglądarki
  114.  *
  115.  * @access private
  116.  * @param str $strOutput - dane do dodania
  117.  * @return true
  118.  */
  119. private function addOutput( $strOutput )
  120. {
  121. $this->strOutput .= $strOutput;
  122. return true;
  123. }
  124.  
  125. /**
  126.  * Pobiera bufor przeglądarki
  127.  *
  128.  * @access public
  129.  * @return str
  130.  */
  131. public function getOutput( )
  132. {
  133. return $this->strOutput;
  134. }
  135.  
  136. /**
  137.  * Konstruuje wyjściowy kod
  138.  *
  139.  * @access public
  140.  * @return str
  141.  */
  142. public function fetchOutput()
  143. {
  144. //
  145. $this->addOutput( '<'.'?xml version=\"1.0\" encoding=\"' . $this->strCharset . '\" ?' . '>' . &#092;"n\" );
  146. $this->addOutput( '<rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" xmlns=\"http://purl.org/rss/1.0/\">' . &#092;"n\" );
  147.  $this->addOutput( '<channel rdf:about=\"' . $this->strURL . '\">' . &#092;"n\" );
  148.  $this->addOutput( '<title>' . $this->strTitle . '</title>' . &#092;"n\" );
  149. $this->addOutput( '<link>' . $this->strLink .'</link>' . &#092;"n\" );
  150. $this->addOutput( '<description>' . $this->strDescription . '</description>' . &#092;"n\" );
  151. $this->addOutput( '<items><rdf:Seq>' . &#092;"n\" );
  152.  
  153. foreach( $this->arrItems as $arrItem )
  154. {
  155. $this->addOutput( '<rdf:li rdf:resource=\"' . $arrItem['link'] . '\" />' . &#092;"n\" );
  156. }
  157.  
  158. $this->addOutput( '</rdf:Seq></items>' . &#092;"n\" );
  159. $this->addOutput( '</channel>' . &#092;"n\" );
  160.  
  161. foreach( $this->arrItems as $arrItem )
  162. {
  163. $this->addOutput( '<item rdf:about=\"' . $arrItem['link'] . '\">' . &#092;"n\" );
  164. $this->addOutput( '<link>' . $arrItem['link'] . '</link>' . &#092;"n\" );
  165. $this->addOutput( '<title>' . $arrItem['title'] . '</title>' . &#092;"n\" );
  166. $this->addOutput( '<description>' . $arrItem['description'] . '</description>' . &#092;"n\" );
  167. $this->addOutput( '</item>' . &#092;"n\" );
  168. }
  169.  
  170. $this->addOutput( '</rdf:RDF>'.&#092;"n\" );
  171.  
  172. return $this->getOutput();
  173. }
  174.  
  175. /**
  176.  * Wyświeta bufor przeglądarki
  177.  *
  178.  * @access public
  179.  * @return true
  180.  */
  181. public function printOutput()
  182. {
  183. header(&#092;"Content-type: text/xml; charset=\" . $this->strCharset);
  184. $this->fetchOutput();
  185. echo $this->getOutput();
  186. return true;
  187. }
  188.  
  189. /**
  190.  * Dodaje pozycję do kanału RSS
  191.  *
  192.  * @access public
  193.  * @return true
  194.  */
  195. public function addItem($strItemLink, $strItemTitle, $strItemDescription ) {
  196. $this->arrItems[] = array( 
  197. 'link' => $strItemLink,
  198. 'title' => $strItemTitle,
  199. 'description' => $strItemDescription
  200. );
  201. return true;
  202. }
  203.  
  204.  
  205. }
  206.  
  207. ?>


rss.test.php:
  1. <?php
  2.  
  3. require_once( 'rss.class.php' );
  4.  
  5. $RSS = new RSS();
  6.  
  7. $RSS->setURL( &#092;"http://something.net\" );
  8. $RSS->setTitle( &#092;"RSS\" );
  9. $RSS->setLink( &#092;"http://something.com\" );
  10. $RSS->setDescription( &#092;"Channel description\" );
  11.  
  12. $RSS->addItem(&#092;"http://something.net\", \"Text\", \"Long Text\");
  13. $RSS->addItem( 'http://something.com', &#092;"Nowość\", 'test');
  14.  
  15. $RSS->printOutput();
  16.  
  17. ?>

Proszę o opinie i ew. sugestie.

Ten post edytował matid 12.10.2004, 20:59:38
Go to the top of the page
+Quote Post
Fipaj
post 25.02.2005, 18:19:14
Post #5





Grupa: Zarejestrowani
Postów: 691
Pomógł: 0
Dołączył: 19.01.2005
Skąd: Warszawa

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


O ile pamiętam, w jednym pliku RSS może być tylko 15 <item>'ów.

Czyli trzeba przerobić funkcję showAll (odnoszę się do 1 skryptu). A dokładniej trzeba przerobić pętlę foreach... tylko jak to zrobić??


--------------------
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: 24.09.2024 - 12:43