Please note that ExpatTech no longer operates regular office hours, meeting is by appointment only. Please call +36306031104 for details.

ExpatTech Techblog

Nagy Richárd 2010.10.05. 14:31

Creating an RSS 2.0 Feed with PHP

As RSS 2.0 is a well-established standard nowadays and a lot of people actually use it, it is quite useful sometimes to create an RSS feed for a blog or a news section. When creating the new ExpatTech TechBlog I really wanted to implement this feature, so I digged into the topic a bit.

The first important thing to know that an RSS is actually an XML document, so you have to comply with the XML standards, and also RSS has a well-defined standard structure that you want to follow. One nice tool is W3C's Feed Validation Service to check your feed.

Of course I wanted to generate the RSS using PHP, because the whole site uses this technology. On websites the recommended charset is UTF-8, so for the examples I will use that.

First thing to start with is to send the correct Content-type headers:

header("Content-Type: application/xml; charset=utf-8");

After that you should start with outputting the XML header:

<?xml version="1.0" encoding="utf-8" ?>

And for the frame the only remaining action is to output:

<rss version="2.0">
<channel>
</channel>
</rss>

From now on, you will write everything inside the <channel> tag. You should start with specifying the feed's general properties. As the specification says, it is required to specify a title, a description and a link. The first two speak for themselves and the link should point to the page which the RSS is there for (in our case the main page for TechBlog). Remember to use an absolute link.

There are also several optional properties, it is wise to provide language, and I also decided to use copyright (which is simple copyright information), generator (your program) and docs (which is a link to the RSS specification).

<title><?=$title?></title>
<link>http://www.expattech.com/tech-blog/200</link>
<description><?=$description?></description>
<language>en</language>
<copyright>Copyright © ExpatTech</copyright>
<generator>ExpatTech Kapa RSS Engine</generator>
<docs>http://blogs.law.harvard.edu/tech/rss</docs>


You can also set an image for your feed, I decided to use the company logo for this. For the image you have to provide url (the image url), title and link (these should be the same that you used previously). Optionally you can specify the width and height of the image.

<image>
    <title><?=$title?></title>
    <url>http://pix.expattech.com/smallet.png</url>
    <link><http://www.expattech.com/tech-blog/200</link>
    <width>59</width>
    <height>88</height>
</image>

And here comes the interesting part, your feed items (blog entries in this case). Each item should be in an <item> tag, and there are no further required tags, but at least a title or a description should be given. The title is the title of the entry, and description in our case will be a preview of the blog entry, or sometimes the whole entry (if no preview is defined). Other tags I decided to use are link (a link to the entry itself), guid (it is a unique identifier for the item, I decide to use the link for this too) and pubDate (date when the entry was published).

<item>
    <guid isPermaLink="true"><?=$link?></guid>
    <pubDate><?=date(DATE_RFC822, $published)?></pubDate>
    <title><?=$title?></title>
    <description><?=htmlentities($preview)?></description>
    <link><?=$link?></link>
</item>

Of course you will output these items with a foreach or for, and won't output all the entries, I decided to set limit at 20. You can see that the date must be in RFC 822 format and I used htmlentities() on the description because it has HTML tags in it (and in XML that would not be a nice idea here). When I implement comments for the TechBlog, the comments tag will be used to provide a link to the comments themselves.

Now we are basically done. All you have to do is to create a nice friendly URL to your RSS (and maybe some caching) and you can start to use it. You might also want to read the full RSS specification for further tags and other information.