php实现的RSS生成类实例

这篇文章主要介绍了php实现的RSS生成类,实例分析了RSS生成类的原理、定义与使用技巧,非常具有实用价值,需要的朋友可以参考下

本文实例讲述了php实现的RSS生成类,分享给大家供大家参考,具体如下:

  1. class RSS
  2. {
  3. var $title;
  4. var $link;
  5. var $description;
  6. var $language = "en-us";
  7. var $pubDate;
  8. var $items;
  9. var $tags;
  10. function RSS()
  11. {
  12. $this->items = array();
  13. $this->tags = array();
  14. }
  15. function addItem($item)
  16. {
  17. $this->items[] = $item;
  18. }
  19. function setPubDate($when)
  20. {
  21. if(strtotime($when) == false)
  22. $this->pubDate = date("D, d M Y H:i:s ", $when) . "GMT";
  23. else
  24. $this->pubDate = date("D, d M Y H:i:s ", strtotime($when)) . "GMT";
  25. }
  26. function getPubDate()
  27. {
  28. if(emptyempty($this->pubDate))
  29. return date("D, d M Y H:i:s ") . "GMT";
  30. else
  31. return $this->pubDate;
  32. }
  33. function addTag($tag, $value)
  34. {
  35. $this->tags[$tag] = $value;
  36. }
  37. function out()
  38. {
  39. $out = $this->header();
  40. $out .= "<channel>\n";
  41. $out .= "<title>" . $this->title . "</title>\n";
  42. $out .= "<link>" . $this->link . "</link>\n";
  43. $out .= "<description>" . $this->description . "</description>\n";
  44. $out .= "<language>" . $this->language . "</language>\n";
  45. $out .= "<pubDate>" . $this->getPubDate() . "</pubDate>\n";
  46. foreach($this->tags as $key => $val) $out .= "<$key>$val</$key>\n";
  47. foreach($this->items as $item) $out .= $item->out();
  48. $out .= "</channel>\n";
  49. $out .= $this->footer();
  50. $out = str_replace("&", "&amp;", $out);
  51. return $out;
  52. }
  53. function serve($contentType = "application/xml")
  54. {
  55. $xml = $this->out();
  56. header("Content-type: $contentType");
  57. echo $xml;
  58. }
  59. function header()
  60. {
  61. $out = '<?xml version="1.0" encoding="utf-8"?>' . "\n";
  62. $out .= '<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">' . "\n";
  63. return $out;
  64. }
  65. function footer()
  66. {
  67. return '</rss>';
  68. }
  69. }
  70. class RSSItem
  71. {
  72. var $title;
  73. var $link;
  74. var $description;
  75. var $pubDate;
  76. var $guid;
  77. var $tags;
  78. var $attachment;
  79. var $length;
  80. var $mimetype;
  81. function RSSItem()
  82. {
  83. $this->tags = array();
  84. }
  85. function setPubDate($when)
  86. {
  87. if(strtotime($when) == false)
  88. $this->pubDate = date("D, d M Y H:i:s ", $when) . "GMT";
  89. else
  90. $this->pubDate = date("D, d M Y H:i:s ", strtotime($when)) . "GMT";
  91. }
  92. function getPubDate()
  93. {
  94. if(emptyempty($this->pubDate))
  95. return date("D, d M Y H:i:s ") . "GMT";
  96. else
  97. return $this->pubDate;
  98. }
  99. function addTag($tag, $value)
  100. {
  101. $this->tags[$tag] = $value;
  102. }
  103. function out()
  104. {
  105. $out .= "<item>\n";
  106. $out .= "<title>" . $this->title . "</title>\n";
  107. $out .= "<link>" . $this->link . "</link>\n";
  108. $out .= "<description>" . $this->description . "</description>\n";
  109. $out .= "<pubDate>" . $this->getPubDate() . "</pubDate>\n";
  110. if($this->attachment != "")
  111. $out .= "<enclosure url='{$this->attachment}' length='{$this->length}' type='{$this->mimetype}' />";
  112. if(emptyempty($this->guid)) $this->guid = $this->link;
  113. $out .= "<guid>" . $this->guid . "</guid>\n";
  114. foreach($this->tags as $key => $val) $out .= "<$key>$val</$key\n>";
  115. $out .= "</item>\n";
  116. return $out;
  117. }
  118. function enclosure($url, $mimetype, $length)
  119. {
  120. $this->attachment = $url;
  121. $this->mimetype = $mimetype;
  122. $this->length = $length;
  123. }
  124. }

使用示例如下:

  1. $feed = new RSS();
  2. $feed->title = "RSS Feed Title";
  3. $feed->link = "http://website.com";
  4. $feed->description = "Recent articles on your website.";
  5. $db->query($query);
  6. $result = $db->result;
  7. while($row = mysql_fetch_array($result, MYSQL_ASSOC))
  8. {
  9. $item = new RSSItem();
  10. $item->title = $title;
  11. $item->link = $link;
  12. $item->setPubDate($create_date);
  13. $item->description = "<![CDATA[ $html ]]>";
  14. $feed->addItem($item);
  15. }
  16. echo $feed->serve();