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.

Posted By: nathanwilliam on September 25, 2018

CDR writing is no child’s play and only expert cdr report writers can pull that off perfectly. CDR stands for Competency Demonstration Report and is mandatory for any immigrant who wishes to work in Australia as skilled Engineer. For instance, if you want to work as an electrical engineer in Australia, you have to write your CDR for electrical engineering. Whenever you are writing a Cdr Australia you need to ask yourself that whether the reader will be able to assess your skills and competencies and will be able to judge your abilities. Adhere to the pointers given above and you’ll be able to write the perfect CDR.

Posted By: jasmineblack1107 on October 8, 2018

At GotoAssignmentHelp, we provide the best service in assignment writing help online with the help of native experts. Visit now and get seasonal discount of flat 25% off on your first order. accounting assignment help online economics assignment help cheap assignment writing service electronics engineering assignment help mechanical engineering assignment help civil engineering assignment help thesis help online

Posted By: jasmineblack1107 on October 8, 2018

Place your order now. 100% real content, 0% plagiarism, 24/7 live support. Flat 20% OFF on first order. Hurry UP! Key features – 100% original content, 100% satisfaction, all subjects covered, 1000+ professional experts. Grab 20% OFF on first order. Book Now!

help with commercial law assignment online

help with contract law assignment online

help with taxation law assignment online

help with business law assignment online

help with corporate law assignment online

help with law assignment online

Posted By: sienawilliam on October 10, 2018

We are proud of our best assignment Australia by experts because of their dedication towards providing continuous support to students by helping them meet deadlines and scoring better grades. Hiring an expert to write your work is easy but hiring the RIGHT expert to get it done is a challenge. This is because there are a multitude of online academic help services and picking the best is always going to be a trial and error method. In case of engineering and pure sciences, the data analysis’ initial three/four steps remain the same. We have some of the best management assignment help who have intensive knowledge of wide range of topics as discussed above and beyond. They feel completely pressurized. He starts disliking his school or college. But may come, he has to submit his assignment before the deadline, therefore he chooses to buy assignment available to him. To help students complete their assignments My Assignment Services is here to serve them with the best assignment help.

Posted By: sarahsmith on October 10, 2018

Hello My name is Sarah Smith and This is very nice coding. Thanks for sharing with us. Keep it up

Get the Best Of My Assignment Help in UK.

Posted By: keiratayor on October 17, 2018

Just hire our assignment experts to get your quality assignments done before deadline. Now get help with assignment.

Posted By: enawatson on October 22, 2018

Provide Mathematics Assignment help for students

You need to login to post a comment.