Create an RSS feed with PHP

Having an RSS feed on your website is a great way of sharing your content with the rest of the Internet. It’s not a new technology and it’s probably something that you use on a daily basis. If you have a blog or use any form of CMS, that software will most likely handle the creation of your RSS feed for you.

Sometimes, however, it might be necessary for you to create a RSS feed yourself. Perhaps you have just won a new client who’s current site has a old bespoke CMS which they like and want to keep, but you want to be able to publish their updated content via RSS. Hopefully this tutorial will help you to achieve this.

What is RSS?

RSS, in its current form, stands for Really Simple Syndication and is a family of web formats to publish frequently updated content. The RSS Feed (as it is commonly known) can then be read by the users feed reading software or by another website which wishes to ‘syndicate’ the content of that feed.

The RSS format is based on XML that is built using standardised tags. Here is an example of a basic RSS document, we will be generating something similar using PHP later in this tutorial:

[xml]
<?xml version="1.0" encoding="ISO-8859-1"?>
<rss version="2.0">
<channel>
<title>My RSS feed</title>
<link>http://www.openshell.in/</link>
<description>This is an example RSS feed</description>
<language>en-us</language>
<copyright>Copyright (C) 2011 openshell.in</copyright>
<item>
<title>Topic 3</title>
<description>This is an example of topic desc</description>
<link>http://www.openshell.in/topic3.html</link>
<pubDate>Mon, 14 Feb 2011 09:27:16 +0000</pubDate>
</item>
<item>
<title>Topic 2</title>
<description>This is an example of topic desc</description>
<link>http://www.openshell.in/topic2.html</link>
<pubDate>Wed, 12 Jan 2011 12:00:00 +0000</pubDate>
</item>
<item>
<title>Topic 1</title>
<description>This is an example of topic desc</description>
<link>http://www.openshell.in/topic1.html</link>
<pubDate>Wed, 05 Jan 2011 15:57:20 +0000</pubDate>
</item>
</channel>
</rss>
[/xml]

Creating the feed

Step 1: Create the .htaccess file

As we know, RSS is made up of standardised XML tags which you would normally find in a flat XML file. What we are going to do is create this standard XML data dynamically using PHP. We’re going to tell our server to treat files with an .xml extension as PHP files.

To do that, simply create a file named .htaccess. Add AddType application/x-httpd-php .xml and save it to your server. If you have access to your server’s configuration (httpd.conf) file, you can put the AddType declaration there instead.

Keep in mind that this will cause all files with an .xml extension in that directory (or on your server) to be parsed as PHP.

Step 2: Add the RSS ‘Header’ Info[Set the header]

By default, PHP sends text with an HTML content type. As a result, your aggregator may not recognize it as a valid RSS / XML file.

Enter the header() function. We’ll put the following header near the top of our script, before any text gets sent to the client.

[html]
<? header(‘Content-type: application/rss+xml; charset=ISO-8859-1’); ?>
[/html]

Step 3: A basic-begin the feed

A basic RSS document consists of a channel. Within each channel are items. At minimum, each item requires a title, description and link. In this example, we’re also going to add a publication date.

Now we are going to define our database connection details and create the header XML tags for our RSS feed.

RSS feed should start off like this:

[php]
DEFINE (‘DB_USER’, ‘my_username’);
DEFINE (‘DB_PASSWORD’, ‘my_password’);
DEFINE (‘DB_HOST’, ‘localhost’);
DEFINE (‘DB_NAME’, ‘my_database’);

$rssfeed = ‘<?xml version="1.0" encoding="ISO-8859-1"?>’;
$rssfeed .= ‘<rss version="2.0">’;
$rssfeed .= ‘<channel>’;
$rssfeed .= ‘<title>My RSS feed</title>’;
$rssfeed .= ‘<link>http://www.openshell.in</link>’;
$rssfeed .= ‘<description>This is an example RSS feed</description>’;
$rssfeed .= ‘<language>en-us</language>’;
$rssfeed .= ‘<copyright>Copyright (C) 2011 openshell.in</copyright>’;
[/php]

Step 4: Retrieve articles from the database

We need to extract our data by looping through our MySQL database. We’ll need a title, description and publication date for each item.

[php]
$connection = @mysql_connect(DB_HOST, DB_USER, DB_PASSWORD)
or die(‘Could not connect to database’);
mysql_select_db(DB_NAME)
or die (‘Could not select database’);

$query = "SELECT * FROM mytable ORDER BY date DESC";
$result = mysql_query($query) or die ("Could not execute query");

while($row = mysql_fetch_array($result)) {
extract($row);

$rssfeed .= ‘<item>’;
$rssfeed .= ‘<title>’ . htmlentities(strip_tags($title)) . ‘</title>’;
$rssfeed .= ‘<description>’ . htmlentities(strip_tags($description)) . ‘</description>’;
$rssfeed .= ‘<link>’ . $link . ‘</link>’;
$rssfeed .= ‘<pubDate>’ . date("D, d M Y H:i:s O", strtotime($date)) . ‘</pubDate>’;
$rssfeed .= ‘</item>’;
}

$rssfeed .= ‘</channel>’;
$rssfeed .= ‘</rss>’;

echo $rssfeed;
?>
[/php]

Using a while loop, we’re making the script output an RSS item for each row returned from the database. We’re also using strip_tags() and htmlentities() to strip HTML from the title and body, and to escape ampersands and quotes.

From beginning-to-end, your script should look a bit like this:

[php]
<?php
header("Content-Type: application/rss+xml; charset=ISO-8859-1");

DEFINE (‘DB_USER’, ‘my_username’);
DEFINE (‘DB_PASSWORD’, ‘my_password’);
DEFINE (‘DB_HOST’, ‘localhost’);
DEFINE (‘DB_NAME’, ‘my_database’);

$rssfeed = ‘<?xml version="1.0" encoding="ISO-8859-1"?>’;
$rssfeed .= ‘<rss version="2.0">’;
$rssfeed .= ‘<channel>’;
$rssfeed .= ‘<title>My RSS feed</title>’;
$rssfeed .= ‘<link>http://www.openshell.in</link>’;
$rssfeed .= ‘<description>This is an example RSS feed</description>’;
$rssfeed .= ‘<language>en-us</language>’;
$rssfeed .= ‘<copyright>Copyright (C) 2009 openshell.in</copyright>’;

$connection = @mysql_connect(DB_HOST, DB_USER, DB_PASSWORD)
or die(‘Could not connect to database’);
mysql_select_db(DB_NAME)
or die (‘Could not select database’);

$query = "SELECT * FROM mytable ORDER BY date DESC";
$result = mysql_query($query) or die ("Could not execute query");

while($row = mysql_fetch_array($result)) {
extract($row);

$rssfeed .= ‘<item>’;
$rssfeed .= ‘<title>’ . htmlentities(strip_tags($title)) . ‘</title>’;
$rssfeed .= ‘<description>’ . htmlentities(strip_tags($description)) . ‘</description>’;
$rssfeed .= ‘<link>’ . $link . ‘</link>’;
$rssfeed .= ‘<pubDate>’ . date("D, d M Y H:i:s O", strtotime($date)) . ‘</pubDate>’;
$rssfeed .= ‘</item>’;
}

$rssfeed .= ‘</channel>’;
$rssfeed .= ‘</rss>’;

echo $rssfeed;
?>
[/php]

Step 5: Include RSS feed on your site

Now that you have created your feed, you can link to it from your site or publish it. But there is an extra step which will allow your visitor’s browsers to automatically detect the RSS feed on your site. In the section of you pages, include the following tag:

[html]<link rel="alternate" href="rssfeed.xml" title="My RSS feed" type="application/rss+xml" />[/html]

Permanent link to this article: https://blog.openshell.in/2011/06/create-an-rss-feed-with-php/

Leave a Reply

Your email address will not be published.