How to create rss using php – some easy steps
Posted on 23. Apr, 2009 by admin 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. 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. You can borrow the basic structure from any feed, (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>

Leave a reply