Webforumz Newsletter - December 2007
Tutorials
Dynamic RSS Feeds
What is an RSS Feed?
Really Simple Syndication Feeds – are an extension of XML. RSS outputs frequently updated data in a readable format for readers. RSS Feeds are great for blogs, podcasts, news, forums, or anything that is frequently updated. RSS feeds can have the extension .xml or .rss although, later in this tutorial I will show you how to rewrite the URL so that you can display it similarly to www.alexgeek.co.uk/feed. There are currently many sites that use RSS feeds, and many readers enjoy the chance to receive up-to-the-minute information. Here is an example of an RSS feed.
Static Feeds
For a rarely updated feed, you may find it easier to manually update the file rather than have a dynamic feed. First, I will show you a full example of an RSS feed then will explain each tag. Here is a complete RSS Feed source code:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<rss version="2.0">
<channel>
<title>My RSS Feed</title>
<link>http://www.example.com</link>
<description>Here will be the description of your feed.</description>
<item><title>News</title>
<link>http://www.example.com/news/News-Story</link>
<description>Recently there has been some news at the site </description>
</item>
<item><title>News</title>
<link>http://www.example.com/news/News-Story-again</link>
<description>Recently there has been some more news</description>
</item>
</channel>
</rss>
RSS feeds are XML documents and therefore must contain an XML declaration:
<?xml version="1.0" encoding="ISO–8859–1" ?>
Then we must also declare the feed as an RSS document. This tutorial will be using RSS version 2.0:
<rss version="2.0">
There are four frequently used tags in RSS: <channel>, <title>, <link> and <description>. These tags are usually contained in an <item> tag however, they are used at the start to describe the feed itself:
<channel>
<title>My RSS Feed</title>
<link>http://www.example.com</link>
<description>Here will be the description of your feed.</description>
*Feed Items here*
</channel>
Now, for the most important part of a feed, the items. Each item must contain a title, link, and description; but you may also add other tags such as a GUID. Here is an example of a feed item:
<item><title>News</title>
<link>http://www.example.com/news/News-Story-again</link>
<description>Recently there has been some more news</description>
</item>
Now all you need to do is close off your RSS tag with </rss>.
There you have your first RSS Feed! Try saving it with the extension ".xml" or ".rss". Upload it to your website or to your local web server and you should see a very basic feed!
Okay, so you have a feed. But maybe you update your sites' news everyday or you post frequently on your blog. It's a lot of hassle to update your feed each and every time! The solution? Dynamic Feeds!
Dynamic Feeds
Dynamic RSS feeds will update themselves as soon as you post that new blog entry, news headline, or whatever your feed is about! For this we use a mixture of MySQL, PHP and RSS. You wonder how a PHP file can display XML? It's very simple to do – we just use PHP Headers:
<?php header('Content-type: text/xml'); ?>
This header will tell the browser that the file is an XML file, regardless of the extension ".php". Next, we must tell the browser that this is an XML/RSS file again:
<?php
echo '<?xml version="1.0" encoding="ISO-8859-1" ?>
<rss version="2.0">';
?>
Obviously, everyone's CMS will have a different method of displaying entries. Here is a basic example that may be similar to your CMS.
<?php
mysql_connect('localhost', 'username', 'password');
mysql_select_db('news');
$query = "select * from entries order by id DESC";
$results = mysql_query ($query) or die (mysql_error());
while ($row = mysql_fetch_assoc($results)) // will loop through all rows
echo '<item><title>', strip_tags($row['title']),
'</title>', "\n",
'<link> ', $row['link'], '</link>', "\n",
'<description>', htmlentities(strip_tags(substr(
$row['entry'], 0, 600))), '..</description>', "\n",
'</item>', "\n\n";
?>
First, we connect to the database that contains the entries and select the database we are using. Next, we perform a query that will grab all the rows that contain each entry, ordered so that the newest entries appear first. Now, we loop through the query and put each row into an <item> tag.
We use strip_tags() to remove any HTML tags that may be in the title or description and substr() to display the first 600 characters of the post. Edit this to suit your CMS.
Now that we've closed the PHP tag, we just close the tags that are left open:
</channel>
</rss>
If you put that all together you will get the following:
<?php header('Content-type: text/xml');
echo '<?xml version="1.0" encoding="ISO-8859-1" ?>
<rss version="2.0">';
mysql_connect('localhost', 'username', 'password');
mysql_select_db('news');
$query = "select * from entries order by id DESC";
$results = mysql_query ($query) or die (mysql_error());
while ($row = mysql_fetch_assoc($results)) // will loop through all rows
echo '<item><title>', strip_tags($row['title']),
'</title>', "\n",
'<link> ', $row['link'], '</link>', "\n",
'<description>', htmlentities(strip_tags(substr(
$row['entry'], 0, 600))), '..</description>', "\n",
'</item>', "\n\n";
?>
</channel>
</rss>
Edit this to your needs then upload it to your server. Pretty cool, yeah? However, if you are like me you will not want your visitors to see this file as having a ".php" extension. We can tackle this with apache's mod_rewrite. Create or edit your .htaccess file and put in the following code:
RewriteEngine on
RewriteRule ^feed$ feed.php [L]
Now, if your site were example.com you can open feed.php via www.example.com/feed, which is a lot cleaner and easier on the eyes. You can change "feed" to anything you want. If you do save your PHP as something other than feed.php remember to change the code in the htaccess file, too.