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.
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 :
To insert into rss.php :
// MySQL connexion
$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 version="1.0" encoding="iso-8859-1"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">';
$xml .= '<channel>';
$xml .= '<title>RSS FEED TITLE</title>'; // Title of your RSS feed. For instance : Last articles of blog xyz
$xml .= '<link>HTTP://WWW.YOURSITE.COM/</link>'; // Your website's URL
$xml .= '<description>'.$title.'</description>'; // Your website's description
$xml .= '<image>';
$xml .= '<url>YOUR_SITE/IMAGE.GIF</url>'; // An image, logo
$xml .= '<title>IMAGE TITLE</title>'; // Normally the same thing as the title before
$xml .= '<link>HTTP://WWW.YOURSITE.COM/</link>'; // your website's URL
$xml .= '<description>DESCRIPTION</description>'; // Description of you website (for image)
$xml .= '</image>';
}
$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 .= '<item>';
$xml .= '<title>'.$a['titre'].'</title>'; // Article title
$xml .= '<link>'.$lien.'</link>'; // Link to article
$xml .= '<guid isPermaLink="false">'.$lien.'</guid>'; // Permalink
$xml .= '<pubDate>'.$date2.' GMT</pubDate>';
$xml .= '<description>'.strip_tags($a['txt']).'</description>'; // Text of the article, removing HTML tags that it may contain
$xml .= '</item>';
$i++;
}
}
// end of XML
$xml .= '</channel>';
$xml .= '</rss>';
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 :
// write file
$fp = fopen("flux.xml", 'w+');
Comments
Post new comment