login  Naam:   Wachtwoord: 
Registreer je!
 Scripts:

Scripts > PHP > XML en PHP > RSS parser en reader (PHP4)

RSS parser en reader (PHP4)

Auteur: Tri - 08 januari 2005 - 14:59 - Gekeurd door: Dennisvb - Hits: 9975 - Aantal punten: 4.83 (3 stemmen)





Alle staat duidelijk in het script moet je moet wijzigen.

Code:
  1. <?php
  2. /*
  3.  
  4. RSS parser
  5. A RSS document parser which can be also used as a reader.
  6. v1.01
  7.  
  8. NOTE: Make sure you define a URL to a RSS file to parse
  9. and read.
  10.  
  11. by Tri Pham (tri[at]tripham.nl)
  12. http://www.tripham.nl/
  13.  
  14. */
  15.  
  16. /************************************************************
  17. * EDIT RSS_FILE and MAX_HEADLINES *
  18. ************************************************************/
  19. // How many headlines do you want?
  20. define('MAX_HEADLINES', 5);
  21. define('RSS_FILE', 'http://www.nos.nl/export/nosnieuws-rss.xml');
  22. define('OPEN','Error: opening RSS file');
  23.  
  24.  
  25. // Class definition
  26. class RSS_Parser {
  27.  
  28. /*
  29. Flag tells which tag we enter.
  30. Allowed values:
  31.  
  32. $iFlag = 1, we entered <channel>
  33. $iFlag = 2, we entered <item>
  34. $iFlag = 3, we entered <image>
  35. */
  36. var $iFlag;
  37.  
  38. // Which tag did we enter? Ask $sTag!
  39. var $sTag;
  40.  
  41. // Incremented each time we leave a tag.
  42. // How many items have we left?
  43. // When $iCount equals the maximum headlines, the script exits.
  44. var $iCount;
  45.  
  46. // How many news lines do you want?
  47. var $iMax_lines;
  48.  
  49. // <item> iformation
  50. var $sTitle;
  51. var $sDescription;
  52. var $sLink;
  53. var $sPubdate;
  54. // </item>
  55.  
  56. // <sChannel> information
  57. var $sChannel_copyright;
  58. var $sChannel_managingeditor;
  59. var $sChannel_language;
  60. var $sChannel_lastbuilddate;
  61. // </sChannel>
  62.  
  63.  
  64. /**********************************************************
  65. *Function: set_max_headlines
  66.  
  67. *Description: Initialize $iMax_lines, which tells us how
  68. many headlines the user wants.
  69.  
  70. *Parameters:
  71. $iMax An integer which contains the maximum head-
  72. lines defined by the user.
  73. ***********************************************************/
  74.  
  75. function set_max_headlines($pMax) {
  76. // is the parameter an integer?
  77. if(is_int($pMax) AND $pMax > 0) {
  78. $this->iMax_lines = $pMax;
  79. }
  80. }
  81.  
  82.  
  83. /**********************************************************
  84. *Function: startElement
  85.  
  86. *Description: Initialize $iFlag which will tell us which
  87. tag we entered.
  88.  
  89. *Parameters:
  90. $pParser The resource identifier used by PHP inter-
  91. nally.
  92. $pTagname This variable will contain the name of the
  93. closing tag.
  94. $pAttributes It will contain an array of the attributes,
  95. which are specified in the document.
  96. Not every RSS document contains attributes,
  97. so this feature is a bonus ;)
  98. ***********************************************************/
  99.  
  100. function startElement($pParser, $pTagname, $pAttributes) {
  101. $this->sTag = $pTagname; // might seem useless at first sight
  102.  
  103. if($this->sTag == 'CHANNEL') {
  104. $this->iFlag = 1;
  105. }
  106. elseif($this->sTag == 'ITEM') {
  107. $this->iFlag = 2;
  108.  
  109. }
  110. elseif($this->sTag == 'IMAGE'){
  111. $this->iFlag = 3;
  112. }
  113. }
  114.  
  115.  
  116. /**********************************************************
  117. *Function: endElement
  118.  
  119. *Description: Function to process the information
  120. collected out of the tags.
  121.  
  122. *Parameters:
  123. $pParser The resource identifier used by PHP internally.
  124. $pTagname This variable will contain the name of the
  125. closing tag.
  126. ***********************************************************/
  127.  
  128. function endElement($pParser, $pTagname) {
  129.  
  130. // If we enter </item>
  131. if($pTagname == 'ITEM') {
  132. // Check if we have reached the maximum
  133. if($this->iCount == $this->iMax_lines) {
  134. exit();
  135. }
  136. if($this->sDescription == '') {
  137. $this->sDescription = 'unknown';
  138. }
  139. if($this->sPubdate == '') {
  140. $this->sPubdate = 'unknown';
  141. }
  142.  
  143. // Show the news
  144. printf('<p><b><a href=\"%s\">%s</a></b><br>', trim($this->sLink), htmlspecialchars(trim($this->sTitle)));
  145. printf('<b>Description:</b> %s</p>', htmlspecialchars(trim($this->sDescription)));
  146.  
  147.  
  148. // Empty the variables for new values
  149. unset($this->sTitle);
  150. unset($this->sDescription);
  151. unset($this->sLink);
  152. unset($this->sPubdate);
  153.  
  154. // Update counter asscociated with the maximum headlines
  155. $this->iCount++;
  156. }
  157. // If we leave </channel>
  158. elseif($pTagname == 'CHANNEL') {
  159. if($this->sChannel_copyright == '') {
  160. $this->sChannel_copyright = 'unknown';
  161. }
  162. if($this->sChannel_managingeditor == '') {
  163. $this->sChannel_managingeditor = 'unknown';
  164. }
  165. if($this->sChannel_language == '') {
  166. $this->sChannel_language = 'unknown';
  167. }
  168. if($this->sChannel_lastbuilddate == '') {
  169. $this->sChannel_lastbuilddate = 'unknown';
  170. }
  171. }
  172. }
  173.  
  174.  
  175. /**********************************************************
  176. *Function: characterData
  177.  
  178. *Description: The actual feed processing happens here.
  179. We collect the data and process them.
  180.  
  181. *Parameters:
  182. $pParser The resource identifier used by PHP internally.
  183. $pTagname This variable will contain the name of the
  184. closing tag.
  185. ***********************************************************/
  186. function characterData($pParser, $pData) {
  187.  
  188. if($this->iFlag == 1) { // entering <sChannel>
  189. // Here is our sTag used for :)
  190. switch($this->sTag) {
  191. case 'LANGUAGE':
  192. $this->sChannel_language .= $pData;
  193. break;
  194. case 'LASTBUILDDATE':
  195. $this->sChannel_lastbuilddate .= $pData;
  196. break;
  197. case 'COPYRIGHT':
  198. $this->sChannel_copyright .= $pData;
  199. break;
  200. case 'MANAGINGEDITOR':
  201. $this->sChannel_managingeditor .= $pData;
  202. break;
  203. }
  204. }
  205. elseif($this->iFlag == 2) {
  206. switch($this->sTag) {
  207. // entering <item>
  208. case 'TITLE':
  209. $this->sTitle .= $pData;
  210. break;
  211. case 'DESCRIPTION':
  212. $this->sDescription .= $pData;
  213. break;
  214. case 'LINK':
  215. $this->sLink .= $pData;
  216. break;
  217. case 'PUBDATE':
  218. $this->sPubdate .= $pData;
  219. break;
  220. }
  221. }
  222. }
  223. } // End of class definition.
  224.  
  225.  
  226. // Create XML support and setup xml parser.
  227. $cRss_parser = new RSS_Parser();
  228. xml_set_object($xp, $cRss_parser);
  229.  
  230. // Ignore whitespaces, allows the parser to skip "space"
  231. // characters in the xml document.
  232. xml_parser_set_option($xp, XML_OPTION_SKIP_WHITE, TRUE);
  233.  
  234. // Setup the event handlers.
  235. xml_set_element_handler($xp, 'startElement', 'endElement');
  236. xml_set_character_data_handler($xp, 'characterData');
  237.  
  238. // Set maximum headlines.
  239. $cRss_parser->set_max_headlines(MAX_HEADLINES);
  240. unset($data);
  241.  
  242. // Open xml file which contains the RSS info.
  243. $fp = fopen(RSS_FILE,'r');
  244. if($fp) {
  245. fflush($fp);
  246.  
  247. while(!feof($fp)) {
  248. // Read it in chunks of 4 kilobytes.
  249. $data = fread($fp, 4096);
  250.  
  251. // If the document contains an error,
  252. // HELP!!!
  253. // else calm down.
  254. if(!(xml_parse($xp, trim($data), feof($fp))))
  255. die(sprintf("XML error: %s at line %d",
  256. }
  257.  
  258. // Close file pointer and free up XML parser.
  259. fclose($fp);
  260. }
  261. else {
  262. // Error opening RSS file.
  263. die(sprintf("%s %s", OPEN, RSS_FILE));
  264. }
  265. ?>
Download code! Download code (.txt)

 Stemmen
Niet ingelogd.

 Reacties
Post een reactie
Lees de reacties (1)
© 2002-2024 Sitemasters.be - Regels - Laadtijd: 0.04s