For a while I’ve been using PHP’s extension cURL and SimpleXML to obtain API XML info from sites like Twitter and Bizzare Creations. Since the format for RSS feeds and XML are the same, I don’t see why I can’t use the same method so I’ve decided to try it out.
Normally I would make a class and all but for display purposes I’m going to keep it simple. Remember, you need to have cURL and SimpleXML extension enabled in your php.ini
$rss_feed = "http://www.bungie.net/News/NewsRss.ashx"; // halo 3 news eh? $ch = curl_init($rss_feed); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 4); $data = curl_exec($ch); // put data from rss url into variable curl_close($ch);
All thats happening on top is simply going to the URL provided and getting the rss feed source code (aka the info).
// handle the xml data parsing $xml = new SimpleXmlElement($data, LIBXML_NOCDATA); // Do some testing with the data using print_r(); // We gotta see what info we need print_r($xml); // it'll show all the data from the RSS feed but we // only need the news so lets shrink our test print_r($xml->channel); // item is an array so we can put a loop to process it // NOTE: channel and item are the variables used in the source feed // Other RSS Feeds may not have all this, though it should be standard foreach($xml->channel->item) { $item->description; // will show each description for each item // other options that can be included (by doing tests, // you can find out what options are available) // title, link, pubDate, guid, description $item->link; // would give you the url to the news article $item->title; // title of the article, obviously // AGAIN: variables names may be different, use print_r($xml) // to initially find out your variables }
Thats it! The Step Process is as simple as 1, 2, and 3.
1) Get info via cURL
2) Parse info via SimpleXML
3) Select what you need by viewing the whole variable, and pick vars out
When the loop was occurring, you notice that I didn’t start the url using $xml. That’s because the [ITEM] tag was already converted into a SimpleXML format (all the variables were) so using $item->description is definitely valid. $xml->channel->item[0]->description will get the same result if you want to see a different perspective in whats going on.
If you want to play around with the data, instead of echoing the variables, just have other variables equal them for organization.
Full working source below
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <?php $ch = curl_init('http://www.bungie.net/News/NewsRss.ashx'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 4); $data = curl_exec($ch); curl_close($ch); $xml = new SimpleXmlElement($data, LIBXML_NOCDATA); foreach($xml->channel->item as $item) { foreach($item as $key=>$details) { // XML is all the way down to // the core array, no need // to do $xml->... or // $item->... // the loop did the work for us echo '<strong>'.ucwords($key). ':</strong> '.$details.'<br />'; } echo '<br /><br />'; } ?> |
June 20th, 2010 at 4:47 pm
nice tutorial.
July 12th, 2010 at 4:55 am
Thanks very much, nice simple but informative tutorial