php 完美的rss 生成类

  1. <?php
  2. class rssGenerator_rss
  3. {
  4. var $rss_version = '2.0';
  5. var $encoding = '';
  6. var $stylesheet = '';
  7. function cData($str)
  8. {
  9. return '<![CDATA[ ' . $str . ' ]]>';
  10. }
  11. function createFeed($channel)
  12. {
  13. $selfUrl = (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] != 'on' ? 'http://' : 'https教程://');
  14. $selfUrl .= $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
  15. $rss = '<?xml version="1.0"';
  16. if (!emptyempty($this->encoding)) {
  17. $rss .= ' encoding="' . $this->encoding . '"';
  18. }
  19. $rss .= '?>' . " ";
  20. if (!emptyempty($this->stylesheet)) {
  21. $rss .= $this->stylesheet . " ";
  22. }
  23. $rss .= '<!-- Generated on ' . date('r') . ' -->' . " ";
  24. $rss .= '<rss version="' . $this->rss_version . '" xmlns:atom="http://www.w3.org/2005/Atom">' . " ";
  25. $rss .= ' <channel>' . " ";
  26. $rss .= ' <atom:link href="' . ($channel->atomLinkHref ? $channel->atomLinkHref : $selfUrl) . '" rel="self" type="application/rss+xml" />' . " ";
  27. $rss .= ' <title>' . $channel->title . '</title>' . " ";
  28. $rss .= ' <link>' . $channel->link . '</link>' . " ";
  29. $rss .= ' <description>' . $channel->description . '</description>' . " ";
  30. if (!emptyempty($channel->language)) {
  31. $rss .= ' <language>' . $channel->language . '</language>' . " ";
  32. }
  33. if (!emptyempty($channel->copyright)) {
  34. $rss .= ' <copyright>' . $channel->copyright . '</copyright>' . " ";
  35. }
  36. if (!emptyempty($channel->managingEditor)) {
  37. $rss .= ' <managingEditor>' . $channel->managingEditor . '</managingEditor>' . " ";
  38. }
  39. if (!emptyempty($channel->webMaster)) {
  40. $rss .= ' <webMaster>' . $channel->webMaster . '</webMaster>' . " ";
  41. }
  42. if (!emptyempty($channel->pubDate)) {
  43. $rss .= ' <pubDate>' . $channel->pubDate . '</pubDate>' . " ";
  44. }
  45. if (!emptyempty($channel->lastBuildDate)) {
  46. $rss .= ' <lastBuildDate>' . $channel->lastBuildDate . '</lastBuildDate>' . " ";
  47. }
  48. foreach ($channel->categories as $category) {
  49. $rss .= ' <category';
  50. if (!emptyempty($category['domain'])) {
  51. $rss .= ' domain="' . $category['domain'] . '"';
  52. }
  53. $rss .= '>' . $category['name'] . '</category>' . " ";
  54. }
  55. if (!emptyempty($channel->generator)) {
  56. $rss .= ' <generator>' . $channel->generator . '</generator>' . " ";
  57. }
  58. if (!emptyempty($channel->docs)) {
  59. $rss .= ' <docs>' . $channel->docs . '</docs>' . " ";
  60. }
  61. if (!emptyempty($channel->ttl)) {
  62. $rss .= ' <ttl>' . $channel->ttl . '</ttl>' . " ";
  63. }
  64. if (sizeof($channel->skipHours)) {
  65. $rss .= ' <skipHours>' . " ";
  66. foreach ($channel->skipHours as $hour) {
  67. $rss .= ' <hour>' . $hour . '</hour>' . " ";
  68. }
  69. $rss .= ' </skipHours>' . " ";
  70. }
  71. if (sizeof($channel->skipDays)) {
  72. $rss .= ' <skipDays>' . " ";
  73. foreach ($channel->skipDays as $day) {
  74. $rss .= ' <day>' . $day . '</day>' . " ";
  75. }
  76. $rss .= ' </skipDays>' . " ";
  77. }
  78. if (!emptyempty($channel->image)) {
  79. $image = $channel->image;
  80. $rss .= ' <image>' . " ";
  81. $rss .= ' <url>' . $image->url . '</url>' . " ";
  82. $rss .= ' <title>' . $image->title . '</title>' . " ";
  83. $rss .= ' <link>' . $image->link . '</link>' . " ";
  84. if ($image->width) {
  85. $rss .= ' <width>' . $image->width . '</width>' . " ";
  86. }
  87. if ($image->height) {
  88. $rss .= ' <height>' . $image->height . '</height>' . " ";
  89. }
  90. if (!emptyempty($image->description)) {
  91. $rss .= ' <description>' . $image->description . '</description>' . " ";
  92. }
  93. $rss .= ' </image>' . " ";
  94. }
  95. if (!emptyempty($channel->textInput)) {
  96. $textInput = $channel->textInput;
  97. $rss .= ' <textInput>' . " ";
  98. $rss .= ' <title>' . $textInput->title . '</title>' . " ";
  99. $rss .= ' <description>' . $textInput->description . '</description>' . " ";
  100. $rss .= ' <name>' . $textInput->name . '</name>' . " ";
  101. $rss .= ' <link>' . $textInput->link . '</link>' . " ";
  102. $rss .= ' </textInput>' . " ";
  103. }
  104. if (!emptyempty($channel->cloud_domain) || !emptyempty($channel->cloud_path) || !emptyempty($channel->cloud_registerProcedure) || !emptyempty($channel->cloud_protocol)) {
  105. $rss .= ' <cloud domain="' . $channel->cloud_domain . '" ';
  106. $rss .= 'port="' . $channel->cloud_port . '" path="' . $channel->cloud_path . '" ';
  107. $rss .= 'registerProcedure="' . $channel->cloud_registerProcedure . '" ';
  108. $rss .= 'protocol="' . $channel->cloud_protocol . '" />' . " ";
  109. }
  110. if (!emptyempty($channel->extraXML)) {
  111. $rss .= $channel->extraXML . " ";
  112. }
  113. foreach ($channel->items as $item) {
  114. $rss .= ' <item>' . " ";
  115. if (!emptyempty($item->title)) {
  116. $rss .= ' <title>' . $item->title . '</title>' . " ";
  117. }
  118. if (!emptyempty($item->description)) {
  119. $rss .= ' <description>' . $item->description . '</description>' . " ";
  120. }
  121. if (!emptyempty($item->link)) {
  122. $rss .= ' <link>' . $item->link . '</link>' . " ";
  123. }
  124. if (!emptyempty($item->pubDate)) {
  125. $rss .= ' <pubDate>' . $item->pubDate . '</pubDate>' . " ";
  126. }
  127. if (!emptyempty($item->author)) {
  128. $rss .= ' <author>' . $item->author . '</author>' . " ";
  129. }
  130. if (!emptyempty($item->comments)) {
  131. $rss .= ' <comments>' . $item->comments . '</comments>' . " ";
  132. }
  133. if (!emptyempty($item->guid)) {
  134. $rss .= ' <guid isPermaLink="';
  135. $rss .= ($item->guid_isPermaLink ? 'true' : 'false') . '">';
  136. $rss .= $item->guid . '</guid>' . " ";
  137. }
  138. if (!emptyempty($item->source)) {
  139. $rss .= ' <source url="' . $item->source_url . '">';
  140. $rss .= $item->source . '</source>' . " ";
  141. }
  142. if (!emptyempty($item->enclosure_url) || !emptyempty($item->enclosure_type)) {
  143. $rss .= ' <enclosure url="' . $item->enclosure_url . '" ';
  144. $rss .= 'length="' . $item->enclosure_length . '" ';
  145. $rss .= 'type="' . $item->enclosure_type . '" />' . " ";
  146. }
  147. foreach ($item->categories as $category) {
  148. $rss .= ' <category';
  149. if (!emptyempty($category['domain'])) {
  150. $rss .= ' domain="' . $category['domain'] . '"';
  151. }
  152. $rss .= '>' . $category['name'] . '</category>' . " ";
  153. }
  154. $rss .= ' </item>' . " ";
  155. }
  156. $rss .= ' </channel>' . " ";
  157. return $rss .= '</rss>';
  158. }
  159. }
  160. class rssGenerator_channel
  161. {
  162. var $atomLinkHref = '';
  163. var $title = '';
  164. var $link = '';
  165. var $description = '';
  166. var $language = '';
  167. var $copyright = '';
  168. var $managingEditor = '';
  169. var $webMaster = '';
  170. var $pubDate = '';
  171. var $lastBuildDate = '';
  172. var $categories = array();
  173. var $generator = '';
  174. var $docs = '';
  175. var $ttl = '';
  176. var $image = '';
  177. var $textInput = '';
  178. var $skipHours = array();
  179. var $skipDays = array();
  180. var $cloud_domain = '';
  181. var $cloud_port = '80';
  182. var $cloud_path = '';
  183. var $cloud_registerProcedure = '';
  184. var $cloud_protocol = '';
  185. var $items = array();
  186. var $extraXML = '';
  187. }
  188. class rssGenerator_image
  189. {
  190. var $url = '';
  191. var $title = '';
  192. var $link = '';
  193. var $width = '88';
  194. var $height = '31';
  195. var $description = '';
  196. }
  197. class rssGenerator_textInput
  198. {
  199. var $title = '';
  200. var $description = '';
  201. var $name = '';
  202. var $link = '';
  203. }
  204. class rssGenerator_item
  205. {
  206. var $title = '';
  207. var $description = '';
  208. var $link = '';
  209. var $author = '';
  210. var $pubDate = '';
  211. var $comments = '';
  212. var $guid = '';
  213. var $guid_isPermaLink = true;
  214. var $source = '';
  215. var $source_url = '';
  216. var $enclosure_url = '';
  217. var $enclosure_length = '0';
  218. var $enclosure_type = '';
  219. var $categories = array();
  220. }
  221. //开源代码phpfensi.com
  222. ?>

php使用实例如下:

  1. <?php
  2. require_once 'rss_generator.inc.php';
  3. $rss_channel = new rssGenerator_channel();
  4. $rss_channel->atomLinkHref = '';
  5. $rss_channel->title = 'My News';
  6. $rss_channel->link = 'http://phpfensi.com/news.php';
  7. $rss_channel->description = 'The latest news about web-development.';
  8. $rss_channel->language = 'en-us';
  9. $rss_channel->generator = 'PHP RSS Feed Generator';
  10. $rss_channel->managingEditor = 'editor@mysite.com (Alex Jefferson)';
  11. $rss_channel->webMaster = 'webmaster@mysite.com (Vagharshak Tozalakyan)';
  12. $item = new rssGenerator_item();
  13. $item->title = 'New website launched';
  14. $item->description = 'Today I finaly launch a new website.';
  15. $item->link = 'http://phpfensi.com';
  16. $item->guid = 'http://phpfensi.com';
  17. $item->pubDate = 'Tue, 07 Mar 2006 00:00:01 GMT';
  18. $rss_channel->items[] = $item;
  19. $item = new rssGenerator_item();
  20. $item->title = 'Another website launched';
  21. $item->description = 'Just another website launched.';
  22. $item->link = 'http://phpfensi.com';
  23. $item->guid = 'http://phpfensi.com';
  24. $item->pubDate = 'Wed, 08 Mar 2006 00:00:01 GMT';
  25. $rss_channel->items[] = $item;
  26. $rss_feed = new rssGenerator_rss();
  27. $rss_feed->encoding = 'UTF-8';
  28. $rss_feed->version = '2.0';
  29. header('Content-Type: text/xml');
  30. echo $rss_feed->createFeed($rss_channel);
  31. ?>