Parsing A RSS Feed With PHP Using SimpleXML
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.
1 2 3 4 5 6 |
$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:
1 |
$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
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
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:
1 2 3 |
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.
7 Comments
Jonathan Gravois
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?
Paul Robinson
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?
Maurice Oduor
Is there a way to limit the number of feed items to something lets say 10 or 5. I am new to simplexml.
Paul Robinson
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. 🙂
Corey Romero
I’m only getting one result from your example. The RSS feed has several items under channel. What am I doing wrong?
Paul Robinson
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.