Dynamic RSS feeds

RSS feeds became essential for blogs and any other website that publishes content on a regulary basis. It's a good way to broadcast your content and allow your visitors to follow your blog by suscribing to your feeds.

It allows also to submit your RSS feeds in RSS directory, which will give you deep links (other links than your homepage), in addition to bring new visitors from the directory because you show them interesting content before they have access to your website.

Most of CMS (as Joomla, Wordpress, Dotclear, etc.) already have an embeded script that'll generate RSS feeds for you. But if you own a dynamic website, scripted « by hand », you'll have to do it by yourself...

Let's say that you own a dynamic PHP website that provides articles from time to time. We'll create a PHP function to extract articles from the database, then to write them in a XML file, which will be your RSS feed. This way, your RSS reed will update itself automatically.

GENERATE RSS FEED AUTOMATICALLY WITH URL REWRITING

To do it, we'll use URL Rewriting, because it allows to do all that in a much cleaner way, by simulate the RSS file on demand, instead of constantly editing it in a real RSS file.

Create a .htaccess that you will place to root of your website, or edit the .htaccess file you may already own :

  1. Options +FollowSymlinks
  2. RewriteEngine on
  3. # Link for RSS feed
  4. RewriteRule ^rss\.xml$  /rss.php [L,NC]

With that rewriting, rss.php file will generate automatically the XML content of the RSS feed; it'll simulate the file.

Create a file named rss.php. We extract, in the following example, last articles from an imaginary blog.

Structure of the table used in the example :

  1. CREATE TABLE IF NOT EXISTS `blog` (
  2. `id` mediumint(9) NOT NULL COMMENT 'ID of the article',
  3. `texte` text NOT NULL COMMENT 'Text of the article',
  4. `date` date NOT NULL
  5. ) ENGINE=MyISAM DEFAULT CHARSET=latin1 COMMENT='Articles of the blog';

To insert into rss.php :

  1. // MySQL connexion
  2. $host = "localhost"; // normally, localhost
  3. $user = "USERNAME";
  4. $pass = "PASSWORD";
  5. $bdd = "DB_NAME";
  6.  
  7. @mysql_connect($host,$user,$pass) or die("Connexion error: ".mysql_error());
  8. @mysql_select_db("$bdd") or die("Database selection error: ".mysql_error());
  9.  
  10. // Specify content type : XML
  11. header("Content-Type: text/xml");
  12.  
  13. // MySQL query that extracts content from DB
  14. $requete = "SELECT id, date FROM blog ORDER BY date DESC LIMIT 10"; // last 10 articles
  15. $resultat = mysql_query($requete);
  16.  
  17. // XML writing
  18. // header :
  19. $sm = '';
  20. $sm .= 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  21. xsi:schemaLocation="http://www.google.com/schemas/sitemap/0.84
  22. http://www.google.com/schemas/sitemap/0.84/sitemap.xsd">';
  23.  
  24. // Articles of the blog
  25. while ($blog=mysql_fetch_array($resultat,MYSQL_ASSOC)) {
  26. $xml = '<?xml version="1.0" encoding="iso-8859-1"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">';
  27. $xml .= '<channel>';
  28. $xml .= '<title>RSS FEED TITLE</title>'; // Title of your RSS feed. For instance : Last articles of blog xyz
  29. $xml .= '<link>HTTP://WWW.YOURSITE.COM/</link>'; // Your website's URL
  30. $xml .= '<description>'.$title.'</description>'; // Your website's description
  31.  
  32. $xml .= '<image>';
  33. $xml .= '<url>YOUR_SITE/IMAGE.GIF</url>'; // An image, logo
  34. $xml .= '<title>IMAGE TITLE</title>'; // Normally the same thing as the title before
  35. $xml .= '<link>HTTP://WWW.YOURSITE.COM/</link>'; // your website's URL
  36. $xml .= '<description>DESCRIPTION</description>'; // Description of you website (for image)
  37. $xml .= '</image>';
  38. }
  39.  
  40. $i = 0;
  41.  
  42.  
  43. foreach($news as $a) {
  44.  
  45. if($i < 12) {
  46. $id = $a['date'];
  47. $datexml = explode('-', $a['date']);
  48. $date2=date("D, d M Y H:i:s", mktime(0,0,0, $datexml[1],$datexml[2],$datexml[0]));
  49.  
  50. $lien = "http://www.yoursite.com/articles.php?id=" . $id; // Link to your article (modify as your wish)
  51.  
  52. $xml .= '<item>';
  53. $xml .= '<title>'.$a['titre'].'</title>'; // Article title
  54. $xml .= '<link>'.$lien.'</link>'; // Link to article
  55. $xml .= '<guid isPermaLink="false">'.$lien.'</guid>'; // Permalink
  56. $xml .= '<pubDate>'.$date2.' GMT</pubDate>';
  57. $xml .= '<description>'.strip_tags($a['txt']).'</description>'; // Text of the article, removing HTML tags that it may contain
  58. $xml .= '</item>';  
  59.  
  60. $i++;
  61. }
  62. }
  63.  
  64. // end of XML
  65. $xml .= '</channel>';
  66. $xml .= '</rss>';
  67. echo $xml;

Then, when you will try to reach http://www.votresite.com/rss.xml, rss.php will be called an a XML file will be generated and shown, with 10 last articles of your blog.

If you wish, it's also possible to make a function with this code, and instead of simulating the RSS file with URL Rewriting, you can directly write into a rss.xml file. All you have to do is write something like this, to write in a real file :

  1. // write file
  2. $fp = fopen("flux.xml", 'w+');
  3. fputs($fp, $xml);
  4. fclose($fp);

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <actionscript>, <apache>, <bash>, <dos>, <html>, <javascript>, <mysql>, <php>, <xml>.
  • Every instance heading tags will be modified to include an id attribute for anchor linking.
  • Image links with 'rel="lightbox"' in the <a> tag will appear in a Lightbox when clicked on.
  • Image links with 'rel="lightshow"' in the <a> tag will appear in a Lightbox slideshow when clicked on.
  • Links to HTML content with 'rel="lightframe"' in the <a> tag will appear in a Lightbox when clicked on.
  • Links to video content with 'rel="lightvideo"' in the <a> tag will appear in a Lightbox when clicked on.
  • Links to inline or modal content with 'rel="lightmodal"' in the <a> tag will appear in a Lightbox when clicked on.
  • Links to specified hosts will have a rel="nofollow" added to them.

  • Insert <!--tableofcontents [list: ol; title: Table of Contents; minlevel: 2; maxlevel: 3; attachments: yes;]--> to insert a mediawiki style collapsible table of contents. Arguments within [] are optional.

More information about formatting options