Generating RSS Feeds on the Fly With PHP

on Tue Dec 02 20:25:48 GMT 2008 in PHP and viewed 30295 times

This article serves to augment the previous article. Instead of writing to a file to make an RSS feed, php code is included that makes the output of RSS without writing to a separate file.


Introduction

If you read the previous article on generating an RSS Feed by writing to a file, then you know the premise of this article. If you didn’t, well the short version is that we have some data, and we need to make an RSS Feed out of it. The RSS Feed is basically an XML file with specific elements to include in it.

The difference between the last article and this is that while the last article dealt with taking the information from the database and writing it to a separate file to be used as the feed. Here, you will learn how to instead take the information from the database and dynamically output it to the screen in the same file, cutting out the extra file.

To add some items to the feed. Same as the other article, “but has the link to the dynamically generated RSS Feed as well”http://www.shadow-fox.net/examples/dynrss/rss.php.

This will be copied from the other article to provide uniformity in the database, and in case you haven’t read the other one. Plus it’s good for reference.

To Start – The Database Outline

To start you need your overall view of the database you’re going to be extracting the information for the feed from. This will be a simple little news table. So connect to MySQL however you want to, and run this table creation query:


CREATE TABLE news (id int PRIMARY KEY AUTO_INCREMENT,
title varchar(255),
content text);

After this you need to fill it with some data to make sure you see the result, so…


INSERT INTO news VALUES ('', 'test', 'test content');
INSERT INTO news VALUES ('', 'test2', 'more content');

Now you have your database set up! You’re about 1/3 of the way there. Ish.

The Basics

Now you come to the part where you need to set up the basics of your file. This includes database connections, database selections, and the outline of the file.

So to start…


<?php

$connection = mysql_connect("localhost",
"username",
"password");
mysql_select_db("yourdb", $connection);

$select = "SELECT * FROM news ORDER BY id DESC LIMIT 5";
$query = mysql_query($select) or die(mysql_error());

?>

That’s the basics. You have your connection and your selection. Next you have to get into the more difficult stuff.

The XML

Well, you know the basic XML structure of the whole document.


<?xml version="1.0"?>
<rss version="2.0">
<channel>

<title>The Shadow Fox Feed</title>
<link>http://www.shadow-fox.net</link>
<description>Feed Description</description>
<language>en-us</language>

<item>
<title>Title 1</title>
<link>http://www.yoururlhere.com/1</link>
<description>Your Content</description>
</item>

<item>
<title>Title 2</title>
<link>http://www.yoururlhere.com/2</link>
<description>More of your content</description>
</item>

</channel>
</rss>

So now you have to translate that into basically a bunch of printing in PHP.

You can definately get the first and second chunks of XML down without too much trouble. The parts without the items.


<?php

$connection = mysql_connect("localhost",
"username",
"password");
mysql_select_db("yourdb", $connection);

$select = "SELECT * FROM news ORDER BY id DESC LIMIT 5";
$query = mysql_query($select) or die(mysql_error());

echo "<?xml version="1.0"?>
<rss version="2.0">
<channel>
<title>The Shadow Fox Feed</title>
<link>http://www.shadow-fox.net</link>
<description>Feed Description</description>
<language>en-us</language>";

// items will go here

echo "</channel></rss>";

?>

So now you have the start and the end of the XML/RSS file. So the next logical step would be to input the data.

All you are going to do here is to just use a simple while loop with the mysql_fetch_array function. This is nearly identical to the other method, except instead of writing it to the with fwrite, you are just using an echo.


while($array = mysql_fetch_array($query)){
extract($array);
$content = str_replace("<br />", "n", $content);
$content = htmlentities($content);
$content = str_replace("&", "&amp;", $content);
echo "<item>
<title>$title</title>
<link>http://www.shadow-fox.net/index.php?blog=post&amp;id=$id</link>
<description>$content</description>
</item>";
}

Put that in the file where the comment is, upload it and it should work. If it doesn’t, come back here.

It didn’t work? That’s odd. Most likely because it’s still a php file. It isn’t being sent to the browser as XML. So to change that, we give it a different header.

header(“Content-type: text/xml”);

Put that line right before the first echo of the XML. The one that starts


echo "<?xml version="1.0"?>
<rss version="2.0">

header("Content-type: text/xml") basically tells the browser (well, I guess the server actually because this is server side code) to accept the output as XML. Otherwise you just get a bunch of text with some urls and stuff.

Conclusion

Well, you now have your full code:


<?php

$connection = mysql_connect("localhost",
"username",
"password");
mysql_select_db("yourdb", $connection);

$select = "SELECT * FROM news ORDER BY id DESC LIMIT 5";
$query = mysql_query($select) or die(mysql_error());

header("Content-type: text/xml");

echo "<?xml version="1.0"?>
<rss version="2.0">
<channel>
<title>The Shadow Fox Feed</title>
<link>http://www.shadow-fox.net</link>
<description>Feed Description</description>
<language>en-us</language>";

while($array = mysql_fetch_array($query)){
extract($array);
$content = str_replace("<br />", "n", $content);
$content = htmlentities($content);
$content = str_replace("&", "&amp;", $content);
echo "<item>
<title>$title</title>
<link>http://www.shadow-fox.net/index.php?blog=post&amp;id=$id</link>
<description>$content</description>
</item>";
}

echo "</channel></rss>";

?>

Instead of the other method, which involves making another file, you get the data straight from the database. The only downside to this could be more processing time, especially if many people are accessing the code at once. But in most cases it’ll be a bit easier to modify because you don’t have to rebuild the rss file, just modify the PHP file and it’ll all automatically happen.

Either method will suit your purpose fine.

Now that you have two different methods of making RSS feeds for your users, go out and join some more web trends!