RSS Writer Class


Published in: PHP 



Website Promotion
DIRECTORY
is a crucial factor for all websites that need to gain better organic search engine rankings and increase website traffic.
Submitting your website as part of your Web Promotion strategy to our SEO friendly and high traffic Business Directory for review is an excellent way to gain a valuable backlink and increase your websites visibility online.

Submit Site


Contains two classes. RSS and RSSItem. Create a new RSS object and then populate it with RSSItems. Supports enclosures (audio/video files).

Expand | Embed | Plain Text
  1. /* E X A M P L E -----------------------------------------------
  2. $feed = new RSS();
  3. $feed->title = "RSS Feed Title";
  4. $feed->link = "http://website.com";
  5. $feed->description = "Recent articles on your website.";
  6.  
  7. $db->query($query);
  8. $result = $db->result;
  9. while($row = mysql_fetch_array($result, MYSQL_ASSOC))
  10. {
  11. $item = new RSSItem();
  12. $item->title = $title;
  13. $item->link = $link;
  14. $item->setPubDate($create_date);
  15. $item->description = "<![CDATA[ $html ]]>";
  16. $feed->addItem($item);
  17. }
  18. echo $feed->serve();
  19. ---------------------------------------------------------------- */
  20.  
  21. class RSS
  22. {
  23. var $title;
  24. var $link;
  25. var $description;
  26. var $language = "en-us";
  27. var $pubDate;
  28. var $items;
  29. var $tags;
  30.  
  31. function RSS()
  32. {
  33. $this->items = array();
  34. $this->tags = array();
  35. }
  36.  
  37. function addItem($item)
  38. {
  39. $this->items[] = $item;
  40. }
  41.  
  42. function setPubDate($when)
  43. {
  44. if(strtotime($when) == false)
  45. $this->pubDate = date("D, d M Y H:i:s ", $when) . "GMT";
  46. else
  47. $this->pubDate = date("D, d M Y H:i:s ", strtotime($when)) . "GMT";
  48. }
  49.  
  50. function getPubDate()
  51. {
  52. if(empty($this->pubDate))
  53. return date("D, d M Y H:i:s ") . "GMT";
  54. else
  55. return $this->pubDate;
  56. }
  57.  
  58. function addTag($tag, $value)
  59. {
  60. $this->tags[$tag] = $value;
  61. }
  62.  
  63. function out()
  64. {
  65. $out = $this->header();
  66. $out .= "<channel>\n";
  67. $out .= "<title>" . $this->title . "</title>\n";
  68. $out .= "<link>" . $this->link . "</link>\n";
  69. $out .= "<description>" . $this->description . "</description>\n";
  70. $out .= "<language>" . $this->language . "</language>\n";
  71. $out .= "<pubDate>" . $this->getPubDate() . "</pubDate>\n";
  72.  
  73. foreach($this->tags as $key => $val) $out .= "<$key>$val</$key>\n";
  74. foreach($this->items as $item) $out .= $item->out();
  75.  
  76. $out .= "</channel>\n";
  77.  
  78. $out .= $this->footer();
  79.  
  80. $out = str_replace("&", "&amp;", $out);
  81.  
  82. return $out;
  83. }
  84.  
  85. function serve($contentType = "application/xml")
  86. {
  87. $xml = $this->out();
  88. header("Content-type: $contentType");
  89. echo $xml;
  90. }
  91.  
  92. function header()
  93. {
  94. $out = '<?xml version="1.0" encoding="utf-8"?>' . "\n";
  95. $out .= '<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">' . "\n";
  96. return $out;
  97. }
  98.  
  99. function footer()
  100. {
  101. return '</rss>';
  102. }
  103. }
  104.  
  105. class RSSItem
  106. {
  107. var $title;
  108. var $link;
  109. var $description;
  110. var $pubDate;
  111. var $guid;
  112. var $tags;
  113. var $attachment;
  114. var $length;
  115. var $mimetype;
  116.  
  117. function RSSItem()
  118. {
  119. $this->tags = array();
  120. }
  121.  
  122. function setPubDate($when)
  123. {
  124. if(strtotime($when) == false)
  125. $this->pubDate = date("D, d M Y H:i:s ", $when) . "GMT";
  126. else
  127. $this->pubDate = date("D, d M Y H:i:s ", strtotime($when)) . "GMT";
  128. }
  129.  
  130. function getPubDate()
  131. {
  132. if(empty($this->pubDate))
  133. return date("D, d M Y H:i:s ") . "GMT";
  134. else
  135. return $this->pubDate;
  136. }
  137.  
  138. function addTag($tag, $value)
  139. {
  140. $this->tags[$tag] = $value;
  141. }
  142.  
  143. function out()
  144. {
  145. $out .= "<item>\n";
  146. $out .= "<title>" . $this->title . "</title>\n";
  147. $out .= "<link>" . $this->link . "</link>\n";
  148. $out .= "<description>" . $this->description . "</description>\n";
  149. $out .= "<pubDate>" . $this->getPubDate() . "</pubDate>\n";
  150.  
  151. if($this->attachment != "")
  152. $out .= "<enclosure url='{$this->attachment}' length='{$this->length}' type='{$this->mimetype}' />";
  153.  
  154. if(empty($this->guid)) $this->guid = $this->link;
  155. $out .= "<guid>" . $this->guid . "</guid>\n";
  156.  
  157. foreach($this->tags as $key => $val) $out .= "<$key>$val</$key\n>";
  158. $out .= "</item>\n";
  159. return $out;
  160. }
  161.  
  162. function enclosure($url, $mimetype, $length)
  163. {
  164. $this->attachment = $url;
  165. $this->mimetype = $mimetype;
  166. $this->length = $length;
  167. }
  168. }

Report this snippet 

Comments

RSS Icon Subscribe to comments
Posted By: lkrubner on September 30, 2007

In this line:

foreach($this->tags as $key => $val) $out .= "$val";

Is the newline really suppose to be inside of the tag?

Posted By: nijgnet on March 3, 2008

ln145: $out .= "\n"; should be not be a concatenation. Change to $out = "\n";

Posted By: zenoseo on November 13, 2009

Tried in145: $out .= "\n"; should be not be a concatenation. Change to $out = "\n but still not working

Posted By: Whowonthesuperbowl on February 1, 2010

Very nice, thanks for drawing this out for us. It's even cooler that it supports video, which is pretty much a must-have these days!

You need to login to post a comment.

Download royalty free graphics