Article content

How to write a RSS feed with PHP

Tagged in rss, feed, phphowto

If you have a web site or blog, it's important to spread the word, and what's better than a properly designed and updated RSS feed? Here's how to write a PHP script that fetches the last 10 items from your database and writes it to a RSS-file.

I'm going to use this site as an example, so the data you'll see below will be similar to what you can see here. Permalink for this article http://mirror.magicode.org/content/How_to_write_a_RSS_feed_with_PHP

First off, we'll define the header information:


<?php
$baseurl="http://www.magicode.org/";
$i=0;
$rss="";

Here we've established the site's base URL, a counter and declared the $rss variable which will hold our RSS-data. This text was originally written for http://blog.magicode.org

The header and channel description

Let's start putting stuff into the $rss variable.


$rss.="<?xml version=\"1.0\" encoding=\"UTF-8\" ?>";
$rss.="<rss version=\"0.91\">\n";
$rss.="<channel>\n";


These first lines defines the XML-version and the RSS-version and the third line starts our channel-tag

$rss.="<title>Magicode.org</title>\n";
$rss.="\t<link>$baseurl</link>\n";
$rss.="\t<description>Magicode.org - where
experts come together</description>\n";
$rss.="\ten-EN\n";


Here we set the title, the main link and the channel description. The \t inserts a tabulator, ie. indents the text, and the \n inserts a newline, or carriage return. If you see this notice on any site other than magicode.org, it's probably been lifted without consent


$rss.="<image>\n";
$rss.="<title>Magicode.org</title>\n";
$rss.="\thttp://www.magicode.org/logo.gif\n";
$rss.="\t<link>http://www.magicode.org/</link>\n";
$rss.="\t86\n";
$rss.="\t35\n";
$rss.="</image>\n";


Here we define an image to go with the channel. This part can be skipped if you do not have a readily available logo

The loop


$host="";
$user="";
$password="";
$databasename="";
$databaselink=mysql_connect($host,$user,$password);
$selectdatabase=mysql_select_db($databasename, databaselink);
$query="SELECT id,subject,url,publishdate,content
FROM content ORDER BY id DESC LIMIT 10;";
$result=mysql_query($query,$databaselink);


Here we open a link to MySQL-database. The variable $host,$user,$password and $databasename must be declared, but I'll leave that to you.

NB! The query above must the changed according to your setup. It's unlikely that you have a table called content with the field names in the example above.


while($dataobject=mysql_fetch_object($result)){
$subject=stripslashes($dataobject->subject);
$url=stripslashes($dataobject->url);
$publishdate=stripslashes($dataobject->publishdate);
$content=stripslashes($dataobject->content);
$rss.="<item>\n";
$rss.="\t<title><![CDATA[$subject[[></title>\n";
$rss.="\t<pubDate>$publishdate</pubDate>\n";
$rss.="\t<link>".$url."</link>\n";
$rss.="\t<description><![CDATA[".$content."[[></description>\n";
$rss.="</item>\n";
$i++;
}


This loop will fill the $rss variable with the 10 last items sorted by their id number. You may want to process the variables, especially $link tag as you probably don't have the entire link stored as a single line in your database. The link to this article, for instance, is stored in the database as How_to_write_a_RSS_feed_with_PHP, so my $link is set up as follows: $url=$baseurl."content/".$url.

A note on the pubDate. The date in an rss feed should follow the RFC822 standard, but it's recommended that you use four digits for the year. This is the command you use to get the correct format in php:


$correct_publishdate=date("D, d M Y H:i:s O",$publishdate);
This assumes that the $publishdate is unixtime -- if not, just write strtotime($publishdate).

Let's write the file


if(file_exists("/rss.xml")){
$handle=fopen("/rss.xml","w") or die("cannot open rss.xml");
if(fwrite($handle,$rss,strlen($rss)))
  echo "RSS file written\n";
fclose($handle);
}


We open the local file rss.xml and write the $rss data into it, then close the file. If you run into problems, please ensure that the rss.xml-file actually exists and that PHP can write to it.

The final step is to close the channel and the rss file properly:


$rss.="</channel>\n";
$rss.="</rss>\n";

And there you go. You should now have a working RSS file. Time to spread the word :)

You can download the uncommented PHP script along with a sample rss.xml-file here:

rssfeed.zip (3.49Kb)

Discussion
09.07.2009 07:41
Really, why the hell write this article? There's so many frameworks out there that let you create feeds without having to do it yourself. Why reinvent the wheel?Stefan
09.07.2009 09:29
Some of us like to do stuff from scratch instead of relying on prewritten frameworks, you know :)Sven
09.07.2009 11:43
Sven, only thing I see with this is its not given a valid RSS feed. You might want to make sure the channel details are there.Joseph Montanez
09.07.2009 12:06
Hi Joseph! My posting system cut out the the opening and closing channel tags from the post - they're in the example file! I'm going to edit the article now so that they're included. Thanks for the tip!Sven

Submit your comment

Text:

Your name:

Your email:

Show my mailaddress (spam protected)

Your website:

Show my website

Featured Article

PHP Variables and strings

A variable is a means of storing a value, such as a text string or a number. In PHP you do not have to declare your variable, as it's automatically declared when you set it. Since you do not need to declare the variable, you do not have to specify what kind of data it contains either.

Topics
Magicode's own open source project
From the forum / Latest comments
You may also want to to check out these links: sendanonmail.com, superstrongpassword.com.