RSS Feed: Building an RSS Data Feed in PHP with SimpleXML
RSS feeds are a must-have for modern websites. It’s easy enough to make an RSS feed of recent articles in PHP. But did you know an RSS feed can simply be information - not links to articles?
This article will look at how to build a basic feed of data - for our purposes, we’ll consider random quotes. It will also illustrate how to use SimpleXML to build the feed for us.
The RSS 2.0 Standard
RSS feeds, like XML in general, have specific standards. In order to be use-able across platforms, it is imperative that standard syntax and naming patterns are used. If you’re unfamiliar with the RSS 2.0 standard, you should look through a copy here.
Before we get to building the feed, let’s identify what tags we’re going to use.
The entire feed is wrapped in an <rss> tag. Within that element is a <channel> element.
The <channel> element has a number of optional child elements. The basic ones we’ll want to include are - <title>, <description>, <link>, and <pubDate>.
The actual data (our random quotes) is wrapped in <item> elements. These also have a bunch of optional parameters. We’ll be using the basics - <title;>, <description>, and <pubDate>. Notice that there’s no link - because we’re feeding simple data, not articles!
Creating the SimpleXML Object
In order to create the SimpleXML object, you need to feed it some kind of XML string. Normally this would be a completed file or string - like an existing feed.
Since we’re building the feed with SimpleXML, we don’t have a lot to start with. We can give it a very basic string to create the object - an opening and closing rss element.
$xml = new SimpleXMLElement('<rss version="2.0"></rss>');
Adding Child Elements
The addChild() method of SimpleXML allows us to add child elements to existing elements. In addition to adding a child element, the method returns a reference to the new child - allowing us to manipulate it in turn.
The first child we’ll need to create is the channel element.
$channel = $xml->addChild('channel');
That creates the channel element and stores a reference to it in $channel. We can then use the $channel variable to add more children - the information defining the channel element itself.
$channel->addChild('title', 'Test Feed'); $channel->addChild('link', 'http://www.earn-web-cash.com'); $channel->addChild('description', 'This is a feed testing how to build an RSS feed in SimpleXML'); $channel->addChild('pubDate', date("D, d M Y H:i:s T"));
You may want to include other optional parameters that are included in the specification. Just add another $channel->addChild() call with the parameter’s name and its value.
Creating the Random Quote Items
At this point, we just need to add our item elements and we’re done. To do that, we’ll loop through an array of data, create an item element, and add the appropriate children to it.
Let’s assume that I retrieved information from a database table with three columns - title, description, and pubDate.
while ($row = mysql_fetch_array($result)) { $newItem = $channel->addChild('item'); $newItem->addChild('title', $row['title']); $newItem->addChild('description', $row['description']); $newItem->addChild('pubDate', date("D, d M Y H:i:s T", $row['pubDate'])); }
Again, you can add extra data to your feed by adding extra child elements.
Outputting the Feed
Now that the entire feed is stored in the $xml variable, we just need to output it.
In doing this, we’ll do two things. We need to define the content-type as xhtml/xml. Then we use the asXML() method to print the information in an xml string.
header('Content-Type: application/xhtml+xml'); echo $xml->asXML();
You could alternatively save this to an xml file. There are two ways to do this - either file_put_contents the output from asXML() or pass a filename as the parameter for asXML().
file_put_contents($filename, $xml->asXML()); // or ... $xml->asXML($filename);
Start Feeding
With this basic script, you can serve up any kind of information as an RSS feed. This makes it easy for people to read it in their RSS readers or even access the information on the cell phones.
There are plenty of types of data you could use this for - quotes, jokes, question of the day, trivia, tip of the day, etc. By publishing tiny bits of data on a daily basis, you keep your users coming back for more - but you don’t need to publish full size articles every day.
In case you ran into trouble putting the script together, here’s the entire source code with an array of data built into it.
$items = array(); $items[] = array('title' => 'Mark Twain on Profanity', 'description' => 'Under certain circumstances, profanity provides a relief denied even to prayer.'); $items[] = array('title' => 'Woody Allen on People', 'description' => 'It seemed the world was divided into good and bad people. The good ones slept better... while the bad ones seemed to enjoy the waking hours much more.'); $items[] = array('title' => 'Will Rogers on Vets', 'description' => 'The best doctor in the world is the veterinarian. He can\'t ask his patients what is the matter - he\'s got to just know.'); // Create the root element - RSS $xml = new SimpleXMLElement('<rss version="2.0"></rss>'); // Create the channel element, a child of RSS $channel = $xml->addChild('channel'); // Add random channel information $channel->addChild('title', 'Random Quotes'); $channel->addChild('link', 'http://www.earn-web-cash.com'); $channel->addChild('description', 'This is a feed of random, interesting quotes.'); $channel->addChild('pubDate', date("D, d M Y H:i:s T")); // Loop through and create item elements foreach ($items as $item) { $newItem = $channel->addChild('item'); $newItem->addChild('title', $item['title']); $newItem->addChild('description', $item['description']); } // Output header('Content-Type: application/xhtml+xml'); echo $xml->asXML();







Tips to Ensure Your XML RSS Feed is Valid | Web Cash said this on March 19th, 2008 at 5:20 am
[…] feed, it can be easy enough to keep tags balanced. However, it’s a good idea to look into using an XML parser like SimpleXML to build the feed. This ensures that the tags are balanced and that the XML is […]