Parsing A RSS Feed With PHP Using SimpleXML

/ PHP / by Paul Robinson / 7 Comments
This post was published back on April 27, 2010 and may be outdated. Please use caution when following older tutorials or using older code. After reading be sure to check for newer procedures or updates to code.

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.

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:

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:

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:

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.

7 Comments

Author’s gravatar

I had this working until I checked the page a minute ago and all I saw was a blank white page. Checking the error log, I saw that the line “$xml = new SimpleXMLElement( $data ); is causing a fatal error “Uncaught exception ‘Exception’ with mes sage ‘String could not be parsed as XML'”.

How do I prevent this error, please?

Reply
Author’s gravatar author

Hi Jonathan,

normally that error is caused when the XML data returned by cURL is malformed. If you visit the URL you are grabbing the feed from does it have any XML errors in it?

Author’s gravatar

Is there a way to limit the number of feed items to something lets say 10 or 5. I am new to simplexml.

Reply
Author’s gravatar author

Hi Maurice,

If you are unable to limit the source then the easiest way would be to introduce a counter into your loop. So for example:

You must make sure that the if is the first piece of code in your loop & the $count++ is the last piece of code in your loop.

Basically all you are doing is telling the loop to break when the counter reaches 4, which is actually 5 because it started counting from 0. So if you wanted to break at 10 you would have to say 9 instead.

Hopefully that makes sense. Let me know if you have any questions. 🙂

Author’s gravatar

I’m only getting one result from your example. The RSS feed has several items under channel. What am I doing wrong?

Reply
Author’s gravatar author

Hi Corey,

I’m at a complete loss… I’ve just ran the exact code you pasted & it only pulls back one item, despite there being more than one. Even if you add in a:

there is only one item. However if you replace the url with http://return-true.com/feed/ it works fine. So I can only assume there is something wrong with the feed, although I can’t see anything & considering it’s a tumblr feed there shouldn’t be. 🙁

Sorry, I don’t think I’ve been much help there.

Reply
Older Comments
Newer Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

I'll keep your WordPress site up-to-date and working to its best.

Find out more