First a few notes. You must have PHP 5 for this to work, SimpleXML is not available in versions of PHP below that. Also you must be careful not to put too much strain on the RSS feed you are parsing especially if it isn’t your own. You must also make sure you have, or get permission to use the feed if it isn’t yours. Right now that’s over with let’s get onto the tutorial.

Getting Our Feed Data

Using SimpleXML is actually fairly simple (hence the name) once you get started. First let’s get our RSS feed. I find the best way to do this is via cURL.

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://wallpapers.celeborama.net/feed/');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);

There we are, a simple request using cURL to grab the contents of the feed. I am grabbing the feed for Celeb O Rama’s Wallpaper site here. Why? Well I have permission & I thought it would be more interesting than just grabbing the Return True feed.

The code will be very familiar if you have read the cURL tutorial I did. We first initialize our cURL session & store it’s handle. We then set the URL we want to grab data from. Next we make sure that cURL won’t return the HTTP headers. If it does our XML will become invalid due to the headers being prepended to the result. Then we tell cURL to return our data instead of echoing straight to the browser. Finally we execute the options & store our data in the variable $date and close/destroy the cURL session.

Making Sense Of It All

So now we have our XML data inside a variable, what do we do with it? Well first we have to parse it. This is done by handing it over to SimpleXML like so:

$xml = new SimpleXMLElement($data);

This will parse the data received & return it as a SimpleXML Object. Here is an example of what the object looks like when it is ran through print_r:

SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [version] => 2.0
        )

    [channel] => SimpleXMLElement Object
        (
            [title] => Celeb O Rama Wallpapers
            [link] => http://wallpapers.celeborama.net
            [description] => Stuffing Celebrity Wallpapers Into Your Eyeballs
            [lastBuildDate] => Sat, 17 Apr 2010 21:59:00 +0000
            [generator] => http://wordpress.org/?v=2.9.2
            [language] => en
            [item] => Array
                (
                    [0] => SimpleXMLElement Object
                        (
                            [title] => Anna Friel – 60s Style Trio
                            [link] => http://wallpapers.celeborama.net/2010/04/17/anna-friel-60s-style-trio/
                            [comments] => http://wallpapers.celeborama.net/2010/04/17/anna-friel-60s-style-trio/#comments
                            [pubDate] => Sat, 17 Apr 2010 21:59:00 +0000

If you are familiar with Objects & Arrays then you will understand how simple it is to go from this point, but for those that don’t let’s make a simple example.

Using The Data

Let’s say I want to list the title of each post, here is how I would do it:

foreach($xml->channel->item as $post) {
    echo $post->title;
}

Yes, it really is that simple. Since there is more than one item SimpleXML created an array from it. The simplest way to go through each item is to use a foreach. Once we have each singular item the data inside can be accessed just like any normal PHP object. Simplez. :)

All Done!

That really is all there is to it. Using this you can create some great stuff. Take for example Lisa-Marie’s recently sold widget on her shop. Since she sells through Zazzle she uses a free tumblr account to blog her latest sales then uses the tumblr blog’s RSS feed to place the info in a widget on her WordPress site by parsing it with PHP & SimpleXML. It involved using HTML tag stripping & some date converting, but overall was fairly simple to create.

I hope this tutorial has been helpful & has given you some ideas. If you have any questions, suggestions or requests let me know in the comments. Finally if you have any tutorial requests hit me up on Twitter, there is a link in my sidebar to my account.