RSS Writer Class


 / Published in: PHP
 

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

  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: 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!

Posted By: FederikoNavarro on September 3, 2010

I have very similar problem. I've been trying to find any solutions in books on PHP. But this code saved my PHP assignment. Thanks)

Posted By: platonica on March 28, 2012

Very nice class, easy to read and understand thanks. I experimented with using this but keep getting 2 blank lines at the start prior to the xml tag which breaks the xml parsing. I've tried adding trim() to the output but no change and I can't see why it would be happening. Any ideas?

Posted By: AliciaLopez on April 5, 2016

I've been trying to find this solutions in my PHP books.I have a very similar problem. But this code saved my Programming assignment . Thanks)

Posted By: ayurvedam on April 26, 2017

this is a very simple and easy to understand the code.writing is a one type of art.I am a ayurveda doctor from <a href="http://chaithanyaayurvedaretreat.com/>ayurveda hospital in kerala.thank you for share this code

Posted By: vouchermedia on April 29, 2017

As a programmer i would like to say thank you for your post.Its really helpful for so many students.If you are searching for argos voucher codes for your online shopping you could get it here.

Posted By: robertfcrocker on September 14, 2017

This is very educational content and written well for a change. It's nice to see that some people still understand how to write a quality post! helpful hints

Posted By: elizadd on September 16, 2017

Your site is truly cool and this is an extraordinary moving article.
(http://buyiglikes.com/buy-likes-on-instagram/ "buy 50 instagram likes")

Posted By: elizadd on September 16, 2017

Your site is truly cool and this is an extraordinary moving article.
buy 50 instagram likes

Posted By: elizadd on September 16, 2017

I read that Post and got it fine and informative. Please share more like that...
Google plus Follower

Posted By: davidsmith123 on September 19, 2017

Thank you very much for this valuable contribution and informative. IBM C5050-380 Study Guides

Posted By: seotab on November 22, 2017

Prepare to begin a fresh beginning, with fresh pledges and undertakes! Welcome the New Year with open hands and eagerness, get some animated gifs to wish in this moment.

Posted By: maxwillor on February 7, 2018

Thanks for sharing this information with us. Keep posting. My Assignment Help

Posted By: poscad on March 20, 2018

Thanku so much for sharing this post. This is very informative news. I would like to know more informations. I hope you include more post. abaya shop pos system

Posted By: johnamber00 on April 17, 2018

Thanks for sharing such a nice piece of information to us. This is very knowledgeable for me. I am John and i am offering Assignment help to students of Australia and all over the world.

Posted By: logomines on May 4, 2018

Such a great post to know different useful information.

Posted By: sarahleach999 on June 6, 2018

That's really cool you've shared the step-by-step instructions! Now, it's becoming much easier to get understanding of how the RSS works. In case there is a need to buy discussion board post about RSS and other things related to it, you are welcome to use special professional services.

Posted By: james7595 on June 8, 2018

Great post! I am actually getting ready to across this information, is very helpful my friend. Also great blog here with all of the valuable information you have. Keep up the good work you are doing here. allassignmenthelp reviews

Posted By: williamlucas on August 9, 2018

Very nice, thanks for drawing this out for us.assignment writing service

Posted By: laurenann881 on September 12, 2018

Great post! I am stuck with my programming assignment, thanks to you for great post, We at My Assignment Help Australia provide best assignment help to university students and help them to get HD in academics.

You need to login to post a comment.