• Posted on March 8, 2013

Creating Dynamic PHP RSS Feed

If you have any type of website that updates content fairly often, such as a blog or a forum having a RSS Feed is a good idea. But if your website is custom coded, or uses a CMS that doesn’t have an RSS Feed you may just forget about it because it seems too “difficult” or not worth the time. I have to tell you it is worth the time and only takes a few minutes to get working correctly.

A lot of sites use RSS feeds to index your site so having them is very important. For instances Blog Directories usually show recent posts and they get this information from RSS Feeds. Not to mention allowing people to subscribe to your RSS Feed to get them coming back when new content is posted.

The first thing we have to do is create our feed file, in this case I named it feed.php. You can name it anything, but if you use an extension other than PHP you will have to change the mime-type so PHP code can run inside of the file.

The basic layout of a XML RSS Feed is as follows.

<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
<channel>
	<title>Website Title / Feed Title</title>
	<description>Website Description / Feed Description</description>
	<link>http:// [ link to your website ]</link>
	<lastBuilddate>Date last Built ex. Fri, 08 Mar 12 10:44:37</lastBuildDate>
	<item>
		<title>Post Title / Content Title</title>
		<description>Part of the content or description for the content.</description>
		<link>[link to the content]</link>
		<guid>[link to the content or unique identifier URL]</guid>
		<pubDate> Date published ex. Fri, 08 Mar 12 10:44:37</pubDate>
	</item>
</channel>
</rss>

As you can see fairly simple. You only need to loop through the posts or content you want and create a new <item> for each one.

In the PHP file, I loop through the most recent 10 posts. This will vary on how you have your database setup and your code so take this as an example.

<?php
	while($row = $sth->fetch(PDO::FETCH_ASSOC)) {
	ob_start();
?>
<item>
	<title><?php echo $row['title']; ?></title>
	<description><![CDATA[<?php echo $row['excerpt']; ?>]]></description>
	<link><?php echo $row['link']; ?></link>
	<guid><?php echo $row['link']; ?></guid>
	<pubDate><?php echo date(DATE_RFC822,$row['posted']); ?></pubDate>
</item>
<?php
	$posts .= ob_get_clean(); // Append Item to Posts Variable
	if($row['posted'] > $lastupdate) // Check if Newer Post
		$lastupdate = $row['posted']; // Update to Last Post Date
	}
?>

As you may have noticed, I wrapped the content of <description> with a CDATA tag. This is because my content sometimes may contain data that will mess with the RSS formatting. If you are using plain text without symbols you shouldn’t have to do this, but it can’t hurt.

Also for each post I looped through I kept track of when it was published and made a variable with the most recent post date. Since I needed the last post date for the RSS feed header I had to first go through the posts and save them as a variable along with the most recent post date.

I now compile the header for the RSS Feed. This is all just plain text I inserted into the file. You could just as easily grab this data from a database if you needed to.

<?php
	ob_start();
?>
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
	<channel>
		<title>My Website Title</title>
		<description>My Website Description</description>
		<link>Link To My Website</link>
		<lastBuildDate><?php echo date(DATE_RFC822,$lastupdate); ?></lastBuildDate>
		<?php echo $posts; ?>
	</channel>
</rss>
<?php
	echo ob_get_clean();
?>

Last I will add content-type on the top of the PHP file so it acts as an XML file.

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

That is how you create a RSS feed, but there is one other thing you should do. You want people to be able to find this feed including search engines and various crawlers and spiders. On your main website in the <head> tags you should add a <link> to the RSS feed.

<link rel="alternate" type="application/rss+xml" title="My RSS Feed" href="http://www.sample.tld/feed.php" />

Now search engines and some browsers will use this and act appropriately.