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 :
Options +FollowSymlinks
RewriteEngine on
# Link for RSS feed
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 :
CREATE TABLE IF NOT EXISTS `blog` (
`id` mediumint(9) NOT NULL COMMENT ‘ID of the article’,
`texte` text NOT NULL COMMENT ‘Text of the article’,
`date` date NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COMMENT=’Articles of the blog’;
To insert into rss.php :
$host = “localhost”; // normally, localhost
$user = “USERNAME”;
$pass = “PASSWORD”;
$bdd = “DB_NAME”;
@mysql_connect($host,$user,$pass) or die(“Connexion error: “.mysql_error());
@mysql_select_db(“$bdd”) or die(“Database selection error: “.mysql_error());
// Specify content type : XML
header(“Content-Type: text/xml”);
// MySQL query that extracts content from DB
$requete = “SELECT id, date FROM blog ORDER BY date DESC LIMIT 10″; // last 10 articles
$resultat = mysql_query($requete);
// XML writing
// header :
$sm = ”;
$sm .= ‘xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation=”http://www.google.com/schemas/sitemap/0.84
http://www.google.com/schemas/sitemap/0.84/sitemap.xsd”>’;
// Articles of the blog
while ($blog=mysql_fetch_array($resultat,MYSQL_ASSOC)) {
$xml = ‘
$xml .= ‘
$xml .= ‘‘; // Title of your RSS feed. For instance : Last articles of blog xyz
$xml .= ‘
HTTP://WWW.YOURSITE.COM/‘; // Your website’s URL
$xml .= ‘
$xml .= ‘
$xml .= ‘
$xml .= ‘‘; // Normally the same thing as the title before
$xml .= ‘
HTTP://WWW.YOURSITE.COM/‘; // your website’s URL
$xml .= ‘
$xml .= ‘
}
$i = 0;
foreach($news as $a) {
if($i < 12) {
$id = $a['date'];
$datexml = explode('-', $a['date']);
$date2=date("D, d M Y H:i:s", mktime(0,0,0, $datexml[1],$datexml[2],$datexml[0]));
$lien = "http://www.yoursite.com/articles.php?id=" . $id; // Link to your article (modify as your wish)
$xml .= '
$xml .= ‘‘; // Article title
$xml .= ‘
‘.$lien.’‘; // Link to article
$xml .= ‘
$xml .= ‘
$xml .= ‘
$xml .= ‘
$i++;
}
}
// end of XML
$xml .= ‘‘;
$xml .= ‘‘;
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 :
$fp = fopen(“flux.xml”, ‘w+’);
fputs($fp, $xml);
fclose($fp);
Ce message est également disponible en : French