Creating an RSS feed using PHP

Doesn’t the title of this post just fill you with thoughts of fun and excitement? Of things you love to do on warm, sunny days? Of course it does.

On Friday evening, I thought (as you do), “I know! I’m going to create my own RSS feed in PHP!”. And promptly did. Hurrah! Thing is, it turned out to be a lot harder than I expected, due to the lack of full and/or correct information on the internet. Here then, is what I did.

To start with, I needed something to put in my feed. As you may be aware, I keep a record of all the games I buy in the museum section of this very site (here, in fact), so I decided to have this provide a feed, showing a bit of info on each game as I add it to the database. The actual data being put into the feed is actually trivial and uncomplicated PHP/MySQL, so I’ll not explain that bit.

My first problem was actually getting my PHP page to output as an XML document rather than text or HTML or something. I found that I needed these headers:

<?php header(“Content-type: text/xml”);?>
<?php echo “<?xml version=\”1.0\” encoding=\”UTF-8\”?>\n” ?>
<rdf:RDF xmlns:rdf=”http://www.w3.org/1999/02/22-rdf-syntax-ns#” xmlns=”http://purl.org/rss/1.0/”>

You’ll see that a couple are actually PHP calls to put these in. The “xml version” one needs to be output in this way, as otherwise the PHP interpreter sees “<?” as a reference to the start of a PHP code block and things break. Next up, the title info for the feed:

<channel rdf:about=”lofi-gaming.org.uk/museum/games/gamerss.php”>
<title>deKay’s Lofi-Gaming New Game List</title>
<description>
All the latest games I’ve bought and added to my site!
</description>
<link>http://lofi-gaming.org.uk/museum/games/</link>
</channel>

This is the “channel” enclosure. Part of my problem with getting this whole thing to work was conflicting reports about how to arrange the data in this set of tags. You can also add an <image> tag too, as a sort of banner for your feed, but it needs to be under a certain size and didn’t bother fiddling with that. The “rdf:about” attribute is a link to the actual RSS feed page itself, and the link tag points to the page the RSS link appears on (so, in my case, the Games page of my museum).

After the channel, you start adding items. Each item tag pair contains a title tag, a description and a link. There’s also an “rdf:about” attribute for each item, and both this and the link point at the page associated with the item – in this case, the individual game page on my site. Crucially, and annoyingly, there’s no image tag, so you can’t include a picture with your post. Here’s an example item for my feed:

<item rdf:about=”http://lofi-gaming.org.uk/museum/games/game.php?id=1533″>
<title>Yoshi’s Island: Super Mario Advance 3 (Game Boy Advance)</title>
<description>
Year: 2002 / Region: Japan / Condition: Mint / Media: Cartridge
Port of SMW2 for the SNES.
</description>
<link>http://lofi-gaming.org.uk/museum/games/game.php?id=1533</link>
</item>

Then you just keep adding items until the cows come home. My code cycles through the most recent ten games and serves them up, so the feed never gets too big. Finally, you close the open RDF tag:

</rdf:RDF>

And you’re done!

Sadly, though, that wasn’t good enough for me. I wanted pictures in my feed – the box art for each game would be nice, yes? Now, I said that you can’t have an image tag in the item tags. This is true. You also can’t “cheat” and use HTML in the item tags. This is also true. However, you can (and this information was only figured out by me because I dissected numerous feeds from other places) cheat the cheat and “force translate” HTML code to wedge it into the feed.

What I’m saying is this: HTML tags are ignored if you put them in the description tags. They just seem to get stripped out. However, if you convert the < and > symbols to actual entities (such as &lt; and &>), and do the same for quotes, then they’re not stripped from the feed and are encoded back to the actual symbols when the feed is displayed – magic HTML!

For example, this:

&lt;img src=&quot;http://lofi-gaming.org.uk/museum/games/scans/1533.jpg&quot; width=”250″ height=”162″ alt=”Yoshi’s Island: Super Mario Advance 3&quot; /&gt;

Gets converted to this:

<img src=”http://lofi-gaming.org.uk/museum/games/scans/1533.jpg” width=”250″ height=”162″ alt=”Yoshi’s Island: Super Mario Advance 3″ />

Which displays this:

Yoshi's Island: Super Mario Advance 3

And there you have it – a fully working RSS feed written in PHP, picking data up from a MySQL database, and including pictures.

Leave a Reply