Creating an RSS Feed From Your Site


/ Published in: PHP

If you syndicate your content with an RSS feed it can help to drive additional traffic to the content pages.


Copy this code and paste it in your HTML
  1. // your DB connection goes here
  2. define('LINKBASE','http://yoursite.com/');
  3. $sql=$yourDB->query("SELECT pageTitle,pageLink,pageDesc,pageDate FROM pageTab ORDER BY pageDate DESC LIMIT 10");
  4. $cnt=$sql->rowCount();
  5. // NOW WE NEED TO DO THE HEADING FOR THE FEED
  6. $feedMainLink=LINKBASE.'feedname.xml';
  7. $mainDate=date("D, d M Y H:i:s O"); // format the date to the rss 2.0 standard
  8. $rssfeed='<?xml version="1.0" encoding="UTF-8"?>';
  9. // WE'LL USE HEREDOC TO BUILD THE HEADING LINES
  10. $rssfeed.=<<<HEAD
  11. <rss version="2.0">
  12. <channel>
  13. <title>TITLE of the FEED</title>
  14. <link>$feedMainLink</link>
  15. <description>A selection of the latest articles, editorials and script snippets</description>
  16. <pubDate>$mainDate</pubDate>
  17. HEAD;
  18. // NOW WE CAN LOOP OUT THE ITEMS
  19. $rowBlock=$sql->fetch(PDO::FETCH_ASSOC);
  20. for ($i=0;$i<$cnt;$i++);
  21. {
  22. $row=$rowBlock[$i];
  23. $PgLink=LINKBASE.$row['pageLink'];
  24. $pubDate=date("D, d M Y H:i:s O", strtotime($row['pageDate']));
  25. $rssfeed.=<<<ITEM
  26. <item>
  27. <title>{$row['pageTitle']}</title>
  28. <description>{$row['pageDesc']}...</description>
  29. <link>$pageLink</link>
  30. <pubDate>$pubDate</pubDate>
  31. </item>
  32. ITEM;
  33. }
  34. }
  35. $rssfeed .= '</channel>';
  36. $rssfeed .= '</rss>';
  37. file_put_contents('feedname.xml', $rssfeed);

URL: http://coboldinosaur.com/pages/creating-rss-feeds-for-your-site.html

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.