Categories

Tags Cloud

RSS Feed

Subscribe to blog RSS Feed Subscribe to Blog's RSS Feed
AUG
2011
13
1 Comment
871 Hits
Easy Parsing Twitter's RSS Feed With PHP
Posted under: PHP, Tutorial

Twitter LogoFrom my previous post about fetching Tweets from Twitter by using PHP we're able to search tweet posts with Twitter API help in RSS format (XML). The specification of RSS itself described here. In order to be able to get information needed from the feed, we need to parse it into other form of data which is easy to process with PHP. In this post I will explain how to parse every items on the feed into array.

Parsing an RSS feed is not such an easy task if you don't understand how does it formed and where the information needed is located inside the XML. After some googling around for XML parser, I found Binny at http://www.bin-co.com had made a great PHP function to convert XML into array.

The PHP code for XML to array conversion made by Binny can be seen here.

How to Use It?

It's simple. In my previous post, we had our RSS feed XML data in PHP $rss variable. Let's see the following PHP code to parse the XML into array by using Binny's xml2array function:

<?php

// $rss contains XML string of RSS feed fetched from Twitter API.
$arr = xml2array( $rss ); // parse it into array!
$items = $arr['rss']['channel']['item']; // getting all item into $items

?>

Yes, with those simple two lines of code we're able to get items collection into array ($items). To get every item (which is also an array). We need to iterate it with the following PHP code:

<?php

foreach( $items as $item ) {
    // each item is contained in $item
    // for example: $item['author'] contains the author data
    // inside item's <author> tag.
    echo $item['author'];
}

?>

Now, we have every items from the feed. Here's an example what is the $item variable contains by dumping the $item variable:

array(10) {
  ["title"]=>
  string(60) "See what Google+ can do now? It's games! http://t.co/bR2aqW6"
  ["link"]=>
  string(53) "http://twitter.com/aryoxp/statuses/101914833439555584"
  ["description"]=>
  string(94) "See what Google+ can do now? It's games! <a href="http://t.co/bR2aqW6">http://t.co/bR2aqW6</a>"
  ["pubDate"]=>
  string(31) "Fri, 12 Aug 2011 07:16:04 +0000"
  ["guid"]=>
  string(53) "http://twitter.com/aryoxp/statuses/101914833439555584"
  ["author"]=>
  string(25) "aryoxp@twitter.com (Aryo)"
  ["media:content"]=>
  array(0) {
  }
  ["media:content_attr"]=>
  array(4) {
    ["type"]=>
    string(9) "image/jpg"
    ["height"]=>
    string(2) "48"
    ["width"]=>
    string(2) "48"
    ["url"]=>
    string(61) "http://a3.twimg.com/profile_images/1325498499/foto_normal.jpg"
  }
  ["google:image_link"]=>
  string(61) "http://a3.twimg.com/profile_images/1325498499/foto_normal.jpg"
  ["twitter:metadata"]=>
  array(1) {
    ["twitter:result_type"]=>
    string(6) "recent"
  }
}

That's  it, now we have every item data. So now we're able to manipulate the item data into whatever we need and whatever we want. Saving into database is one of the options. Tell me if you found a good use of it or you have something to tell me about this.

Next
Prev
24 Apr 2012
02:47 PM
1
Aneeq

There are several ways to read RSS feed in PHP, but this one is surely one of the easiest.

channel->item as $entry) { echo “

link’ title=’$entry->title’>” . $entry->title . “

”; } ?>

Source: http://phphelp.co/2012/04/23/how-to-read-rss-feed-in-php/ OR http://addr.pk/a0401

Write Your Comments

Comments are parsed with Markdown.
 
Notes
* Your email is required to submit this form, and it will not be published or shared without your consent. We use your email address to show your avatar picture profile from Gravatar. Don't have one? Then sign up to gravatar and create your own here.
We also filters your comment against SPAM because we hate SPAM as much as you do. If your comment is recognized as SPAM then it will be moderated, otherwise it will shows up immediately.
Form Key: #fe3733b4068f04a27489909fd1775b26
Loading...