Web Design, Development & Marketing

Web Development Articles

XML / PHP: Create an RSS Feed Using PHP

Here's a simple way to create an XML file using PHP. This can be a useful way of automatically updating a basic RSS feed you may have set up.

Of course, you can adapt the code to suit your requirements. One hurdle you may have to overcome is permissions. Your script actually needs to be able to write to the location you wish to create your XML file in!

Right, so here's some PHP code for you:

$xml_content = "<?xml version=\"1.0\" ?>\r\n";
$xml_content .= "<rss version=\"2.0\">\r\n";
$xml_content .= "<channel>\r\n";
$xml_content .= "<title>Channel Title Goes Here</title>\r\n";
$xml_content .= "<description>Channel Description Here</description>\r\n";
$xml_content .= "<language>en-gb</language>\r\n";
$xml_content .= "<link>http://www.your-domain-name.co.uk/yourFolder/yourPage.html</link>\r\n";
$xml_content .= "<item>\r\n";
$xml_content .= "<title>Your First Item's Title</title>\r\n";
$xml_content .= "<description><![CDATA[ All your fancy content goes here... ]]></description>\r\n";
$xml_content .= "<link>http://www.your-domain-name.co.uk/folder/yourPage.html</link>\r\n";
$xml_content .= "<guid isPermaLink=\"true\">http://www.your-domain-name.co.uk/folder/yourPage.html</guid>\r\n";
$xml_content .= "</item>\r\n";
$xml_content .= "</channel>\r\n";
$xml_content .= "</rss>\r\n";
// open xml feed file and truncate to zero length
$xml_file = fopen("../feedLocation/feed.xml", "w");
// write xml content to xml file
fwrite($xml_file, $xml_content);
// close xml file
fclose($xml_file);

You can make this code more useful by looping over a set of items you wish to put into the XML file. The basic principle for RSS XML feed creation is pretty straightforward. It's up to your own brilliance as to how you use it!

The few lines of code at the end assume that you are clearing the exisiting XML file's content and reloading it all. This is perfectly fine for relatively small file sizes and it's a good method if you wish to keep an RSS file a certain length i.e. you just want your 10 most recent articles in the feed. Instead of mucking around deleting one item off the end and adding the new one to the beginning just wipe it clean and load the 10 most recent items from your database (or wherever it is you keep your wonderfully crafted content).

Another point worthy of noting is the use of the <![CDATA[ ... ]]> tag in the description (content) field of the XML items. This enables you to put anything you like between those tags and not worry about quotes or html tags screwing up your XML file syntax.

Well I hope that has been moderately informative. Comments and questions are always welcome.

Subscribe to RSS Feed Bookmark and Share

Related Links

Related Articles / Posts

Barclaycard ePDQ CPI Integrations (27/05/2009)

HSBC XML API / CPI E-commerce Integrations (02/04/2009)

Recommended Open Source Invoicing System (25/03/2009)

Recent Recommended Links - October 2008 (03/10/2008)

Controlling PHP Register Globals Using .htaccess File (04/03/2008)