rsslib.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. /*
  3. RSS Extractor and Displayer
  4. (c) 2007-2010 Scriptol.com - Licence Mozilla 1.1.
  5. rsslib.php
  6. Requirements:
  7. - PHP 5.
  8. - A RSS feed.
  9. Using the library:
  10. Insert this code into the page that displays the RSS feed:
  11. <?php
  12. require_once("rsslib.php");
  13. echo RSS_Display("http://www.xul.fr/rss.xml", 15);
  14. ? >
  15. */
  16. $RSS_Content = array();
  17. function RSS_Tags($item, $type)
  18. {
  19. $y = array();
  20. $tnl = $item->getElementsByTagName("title");
  21. $tnl = $tnl->item(0);
  22. $title = $tnl->firstChild->textContent;
  23. $tnl = $item->getElementsByTagName("link");
  24. $tnl = $tnl->item(0);
  25. $link = $tnl->firstChild->textContent;
  26. $tnl = $item->getElementsByTagName("pubDate");
  27. $tnl = $tnl->item(0);
  28. $date = $tnl->firstChild->textContent;
  29. $tnl = $item->getElementsByTagName("description");
  30. $tnl = $tnl->item(0);
  31. $description = $tnl->firstChild->textContent;
  32. $y["title"] = $title;
  33. $y["link"] = $link;
  34. $y["date"] = $date;
  35. $y["description"] = $description;
  36. $y["type"] = $type;
  37. return $y;
  38. }
  39. function RSS_Channel($channel)
  40. {
  41. global $RSS_Content;
  42. $items = $channel->getElementsByTagName("item");
  43. // Processing channel
  44. $y = RSS_Tags($channel, 0); // get description of channel, type 0
  45. array_push($RSS_Content, $y);
  46. // Processing articles
  47. foreach($items as $item)
  48. {
  49. $y = RSS_Tags($item, 1); // get description of article, type 1
  50. array_push($RSS_Content, $y);
  51. }
  52. }
  53. function RSS_Retrieve($url)
  54. {
  55. global $RSS_Content;
  56. $doc = new DOMDocument();
  57. $doc->load($url);
  58. $channels = $doc->getElementsByTagName("channel");
  59. $RSS_Content = array();
  60. foreach($channels as $channel)
  61. {
  62. RSS_Channel($channel);
  63. }
  64. }
  65. function RSS_RetrieveLinks($url)
  66. {
  67. global $RSS_Content;
  68. $doc = new DOMDocument();
  69. $doc->load($url);
  70. $channels = $doc->getElementsByTagName("channel");
  71. $RSS_Content = array();
  72. foreach($channels as $channel)
  73. {
  74. $items = $channel->getElementsByTagName("item");
  75. foreach($items as $item)
  76. {
  77. $y = RSS_Tags($item, 1); // get description of article, type 1
  78. array_push($RSS_Content, $y);
  79. }
  80. }
  81. }
  82. function RSS_Links($url, $size = 15)
  83. {
  84. global $RSS_Content;
  85. $page = "<ul>";
  86. RSS_RetrieveLinks($url);
  87. if($size > 0)
  88. $recents = array_slice($RSS_Content, 0, $size + 1);
  89. foreach($recents as $article)
  90. {
  91. $type = $article["type"];
  92. if($type == 0) continue;
  93. $title = $article["title"];
  94. $link = $article["link"];
  95. $page .= "<li><a href=\"$link\">$title</a></li>\n";
  96. }
  97. $page .="</ul>\n";
  98. return $page;
  99. }
  100. function RSS_Display($url, $size = 15, $site = 0, $withdate = 0)
  101. {
  102. global $RSS_Content;
  103. $opened = false;
  104. $page = "";
  105. $site = (intval($site) == 0) ? 1 : 0;
  106. RSS_Retrieve($url);
  107. if($size > 0)
  108. $recents = array_slice($RSS_Content, $site, $size + 1 - $site);
  109. foreach($recents as $article)
  110. {
  111. $type = $article["type"];
  112. if($type == 0)
  113. {
  114. if($opened == true)
  115. {
  116. $page .="</ul>\n";
  117. $opened = false;
  118. }
  119. $page .="<b>";
  120. }
  121. else
  122. {
  123. if($opened == false)
  124. {
  125. $page .= "<ul>\n";
  126. $opened = true;
  127. }
  128. }
  129. $title = $article["title"];
  130. $link = $article["link"];
  131. $page .= "<li><a href=\"$link\">$title</a>";
  132. if($withdate)
  133. {
  134. $date = $article["date"];
  135. $page .=' <span class="rssdate">'.$date.'</span>';
  136. }
  137. $description = $article["description"];
  138. if($description != false)
  139. {
  140. $page .= "<br><span class='rssdesc'>$description</span>";
  141. }
  142. $page .= "</li>\n";
  143. if($type==0)
  144. {
  145. $page .="</b><br />";
  146. }
  147. }
  148. if($opened == true)
  149. {
  150. $page .="</ul>\n";
  151. }
  152. return $page."\n";
  153. }
  154. ?>