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 :


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 :

// 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 .= ‘‘;
$xml .= ‘RSS FEED TITLE‘; // Title of your RSS feed. For instance : Last articles of blog xyz
$xml .= ‘HTTP://WWW.YOURSITE.COM/‘; // Your website’s URL
$xml .= ‘‘.$title.’‘; // Your website’s description

$xml .= ‘‘;
$xml .= ‘YOUR_SITE/IMAGE.GIF‘; // An image, logo
$xml .= ‘IMAGE TITLE‘; // Normally the same thing as the title before
$xml .= ‘HTTP://WWW.YOURSITE.COM/‘; // your website’s URL
$xml .= ‘DESCRIPTION‘; // Description of you website (for image)
$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 .= ‘‘.$a[‘titre’].’‘; // Article title
$xml .= ‘‘.$lien.’‘; // Link to article
$xml .= ‘‘.$lien.’‘; // Permalink
$xml .= ‘‘.$date2.’ GMT‘;
$xml .= ‘‘.strip_tags($a[‘txt’]).’‘; // Text of the article, removing HTML tags that it may contain
$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 :

// write file
$fp = fopen(“flux.xml”, ‘w+’);
fputs($fp, $xml);
fclose($fp);

Ce message est également disponible en : French

4 thoughts on “Dynamic RSS feeds

  1. Sunglass Gucci

    I have been browsing online more than three hours today, yet I never found any interesting article like yours. It is pretty worth enough for me. In my view, if all webmasters and bloggers made good content as you did, the web will be much more useful than ever before.

  2. Tory Burch Handbags Sale

    O said, it can Eventually, employers and consumers are going to have become confident on their own, to know that they can survive just fine under the new rules of the game: New tax rates, new health plan, new financial regulations.

  3. search engine

    4: Be aware of free delivery offers – One of the best parts of on the web Black Friday shopping (regardless of not having to cope with the
    chaos to get) gets your items sent at no cost. If
    you should be done shopping and do not qualify for free delivery, check how
    long away you might be from reaching that quantity.
    Idea Return guidelines fundamentally determine how
    much or exactly how small i will be ready to purchase
    something online.: Try not to wager on a table that a minimum wager greater than five % of spending plan. Easy and greatest on the web Blackjack Suggestion Any client can discover an item they desire online
    and then obtain the shop to supply it in the most useful problem and cost.
    This is just what causes it to be enjoyable for the consumer to buy synthetic flowers on line.
    They can therefore find the blossoms they need the celebration they need
    when purchasing on the web.

  4. rubber sheet

    However, witth regard to EPDM Liquid rubber sheet, this will noot be an issue.
    Ithink it’s too early and leaves the hitter in a little
    bit of a “pause” mode and hinders momentum into the pitch.
    Section steel steelmaking basics and classification of a steel, steel and cast iron steel and pig iron are iron-carbon alloys.

Leave a Reply

Your email address will not be published. Required fields are marked *