///RSS feeds in PHP: 3 simple steps to PHP RSS generation

RSS feeds in PHP: 3 simple steps to PHP RSS generation

Syndication is well and truly here; just about every major content site has an RSS feed, the leading web browsers have inbuilt RSS readers and there are entire startups dedicated to helping you keep track of the sites that aggregate your feeds – let alone those that actually do the feed reading. Adding an RSS feed to your site is quick and painless, and can be done with pure PHP (and some kind of data source). After the jump, 3 simple steps to building an RSS feed in php.

Step 1: Find your content.
An RSS feed should probably list all the content items in your main section. For example, if you run a blog, you can have an RSS feed of all your latest posts. This blog does. If you run a small download site, consider a feed of your latest releases. Whatever content you choose, make sure it’s interesting, and will be updated regularly – aim for between 3-6 times a week, if you have a choice. Then just write out your basic database logic and get the data into an array. I’ll use $items for this example.

Step 2: Get your basic RSS structure.
RSS is an XML-based format, so all you really need is a set of XML tags as part of the RSS specification. RSS provides a number of optional tags, and being XML, it can be (and often is) extended with any other tag. For example, the Dublin Core metadata is a common inclusion. You can borrow the basic structure from any feed, including ours (load it up and right click > view source), but here’s a sample you can use:

<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
<channel>
  <title>My Blog Name</title>
  <link>http://example.com/blog</link>
  <description>Keeping webmasters up-to-date on technology.</description>
  <pubDate>Tue, 15 Apr 2008 18:00:00 +0000</pubDate>
  <item>
    <title>Story Title</title>
    <pubDate>Tue, 15 Apr 2008 18:00:00 +0000</pubDate>
    <link>http://example.com/my/story</link>
    <description>Today I saw a whale!</description>
  </item>
</channel>
</rss>

The easiest way to build your RSS feed in PHP is to leave all of that XML as plain text and only loop over the sections as needed (the <item> tags). The one gotcha here, as you’ve probably noticed, is the opening <?xml tag. This will probably clash with PHP, so instead echo that line out:

<?php echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; ?>

Then your structure is all in place. The item element is repeated for each story we have in our RSS feed; the date is RFC 2822 formatted (just use date("r")). We’ll use a simple foreach loop to put the content in:

<?php foreach ($items as $item) { ?>
  <item>
    <title><?=$item['title']?></title>
    <pubDate><?=date("r",$item['time'])?></pubDate>
    <link><?=$item['url']?></link>
    <description><?=$item['description']?></description>
  </item>
<?php } ?>

3. Serve it up!
To be recognised as an RSS feed, you’ll need to serve it as the right content type. Technically you should use application/rss+xml, but most browsers won’t recognise this. I find application/xhtml+xml works well, as does text/xml. The choice is yours, although IE7 seems to like text/xml better.

<?php header("Content-type: text/xml"); ?>

And we’re done!

Here’s a sample of the final product:

<?php header("Content-type: text/xml"); ?>
<?php echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; ?>
<rss version="2.0">
<channel>
  <title>My Blog Name</title>
  <link>http://example.com/blog</link>
  <description>Keeping webmasters up-to-date on technology.</description>
  <pubDate>Tue, 15 Apr 2008 18:00:00 +0000</pubDate>
<?php foreach ($items as $item) { ?>
  <item>
    <title><?=$item['title']?></title>
    <pubDate><?=date("r",$item['time'])?></pubDate>
    <link><?=$item['url']?></link>
    <description><?=$item['description']?></description>
  </item>
<?php } ?>
</channel>
</rss>
2008-04-17T05:45:41+00:00 April 17th, 2008|PHP|1 Comment

About the Author:

One Comment

  1. wcoenen April 19, 2008 at 9:20 AM

    Generating plain text is a sure way to end up with something that looks like XML but is not well-formed. For example, what if the title or descriptions contain characters like & that need to be escaped?

    I learned this the hard way: if it’s going to be read by an XML parser, then you should generate it with something like DOMDocument or XML writer. Especially if you don’t control the clients that are going to read your “XML”.

    An even better choice would be to not reinvent the wheel: http://freshmeat.net/projects/rsswriterclass/

Leave A Comment