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: brianinfante269 on February 24, 2017

Many students have to complete their class or course in developing faculty to get career in IT. Once they need their first semester notes they easily can overcome the things that will be harder for them. Need help from Research Paper Writers they can assist many by their experience.

Posted By: SeanBirch on March 29, 2017

Great blog Click

Posted By: dissertation on March 30, 2017

Many students have to submit their assignments on time but they don't have time to make assignment like psychology dissertation help and more .

Posted By: teenathomas on April 25, 2017

The code contained post will be easily understood by those who have a deep knowledge in the programming. The thesis writing service offering the best thesis work in an affordable price. The urgent tips are easily available in an affordable rate of cost.

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: florencewillburn on May 15, 2017

Post on program coding is very much understandable one. Usual this type of coding will only be understand fpr those who have knowledge. But here the presenting style make it different one. Now I am a writer of best essay writing service reviews company and I want to know more about this writing style.

Posted By: marriagematrimonial on June 22, 2017

Nice article.I have got so many information in this field.Guruvayoor matrimony

Posted By: stevenfrayne on June 30, 2017

This is a very well written post, my compliments. I’m glad to find your post. Keep sharing this type of stuff. College physics Assignment help online

Posted By: Rachaelwooten on July 1, 2017

Goog site."snipplr":http://snipplr.com

Posted By: Rachaelwooten on July 1, 2017

This is a very well RSS post. <a href="http://www.RSS.com>RSS

Posted By: Rachaelwooten on July 1, 2017

<a href="http://www.RSS.com>Rss

Posted By: Rachaelwooten on July 1, 2017

Rss

Posted By: tomter on July 3, 2017

I am an antique article collector and this article is one of the best I have ever read. I hope to see the same amazing work from you in future. Thanks Assignment help | Need Assignment help | Marketing Assignment help | Law Assignment help

Posted By: edwardwilliam on July 19, 2017

I'm now not absolutely informed approximately this due to the fact my know-how in instructional writing associate with Law Essay Writers UK. We ensures that the students are getting enough guidance and consultancy to overcome all the hurdles involved in tedious tasks such as essays or assignments.

Posted By: elizadd on July 30, 2017

I read that Post and got it fine and informative. Please share more like that...
Read Full Report

Posted By: elizadd on July 30, 2017

Nice knowledge gaining article. This post is really the best on this valuable topic.
view publisher site

Posted By: robertfcrocker on July 30, 2017

That is really nice to hear. thank you for the update and good luck. informative post

Posted By: elizadd on July 31, 2017

I would like to thank you for the efforts you have made in writing this article. I am hoping the same best work from you in the future as well..
this link

Posted By: elizadd on August 1, 2017

Great tips and very easy to understand. This will definitely be very useful for me when I get a chance to start my blog.
view publisher site

Posted By: elizadd on August 7, 2017

I want you to thank for your time of this wonderful read!!! I definately enjoy every little bit of it and I have you bookmarked to check out new stuff of your blog a must read blog!
view publisher site

Posted By: elizadd on August 8, 2017

There's no doubt i would fully rate it after i read what is the idea about this article. You did a nice job..
good family dog breeds australia

You need to login to post a comment.