Generating RSS Feeds using SimpleXML in PHP


/ Published in: PHP
Save to your folder(s)

Generating RSS Feeds using SimpleXML in PHP


Copy this code and paste it in your HTML
  1. // create simplexml object
  2. $xml = new SimpleXMLElement('<rss version="2.0" encoding="utf-8"></rss>');
  3.  
  4. // static channel data
  5. $xml->addChild('channel');
  6. $xml->channel->addChild('title', 'FRD Videos RSS Feeds');
  7. $xml->channel->addChild('link', 'http://www.ste.com/feeds/');
  8. $xml->channel->addChild('description', 'Latest Videos at FRD');
  9. $xml->channel->addChild('language', 'en-us');
  10. $xml->channel->addChild('webMaster', '[email protected]');
  11. $xml->channel->addChild('generator', 'PHP SimpleXML');
  12. $xml->channel->addChild('docs', 'http://www.site.com/videos.xml');
  13. $xml->channel->addChild('pubDate', date(DATE_RSS));
  14. // static channel data ( we get more from db in section below)
  15.  
  16. // query database for article data
  17. ...
  18. // query database for article data
  19.  
  20. // Here we get more date from database for our xml
  21.  
  22. while ($row = mysql_fetch_assoc($result)) {
  23. // add item element for each article
  24. $item = $xml->channel->addChild('item');
  25. $item->addChild('title', htmlentities($row['title']));
  26. $item->addChild('link', 'http://www.site.com/'.$row['id'].'.html');
  27. $item->addChild('description', htmlentities($row['description']));
  28. $item->addChild('pubDate', date(DATE_RSS, strtotime($row['date_added'])));
  29. }
  30.  
  31. // Here we get more date from database for our xml
  32.  
  33. // save the xml to a file
  34. if($xml->asXML('videos.xml'))
  35. {
  36. header('Location:videos.xml');
  37. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.