Quick And Easy RSS Parsing With PHP

When I wrote about creating a Twitter bot with PHP, etc. last week I had in mind to continue developing the idea. To this end I’ve created a bot that replaces the formulaic type of URL for an ‘of the day’ type page (for example http://www.dilbert.com/strips/”.date(“Y”).”-“.date(“m”).”-“.date(“d”).”/”) with an RSS parser so the bot will retweet the links of a given RSS feed.

It turned out to be much easier than I thought. The main tool here is SimpleXML. Here’s how to do it:

  1. Set a variable to represent the RSS feed you want to use. For example:
    $feedUrl = 'http://feeds.nytimes.com/nyt/rss/Baseball';
  2. Retrive the current version of the feed as follows:
    $rawFeed = file_get_contents($feedUrl);
  3. Create a SimpleXML object containing the contents of the RSS file. This will make reading the file simple (no pun intended).
    $xml = new SimpleXmlElement($rawFeed);
  4. The next step is to read the RSS file and pull out the data you want. To do this set up a ‘for’ loop to process each item in the RSS until we’ve reached the end. In my case I want to read the XML in reverse order so if there are multiple articles I want to tweet I can send them in chronological order. (If I read the RSS forward they’d be I’d be reading in reverse chronological order.) Here’s the ‘for’ statement I used:
    for ($counter=count($xml->channel->item); $counter > -1; $counter--) {
    code to process the current item
    }

    The ‘code to process the current item’ is described below. Everything you want to do with this data (assign to variables, send to Twitter, whatever) should be included here because once you move on to the next item the data for the current item will no longer be accessible.
  5. Each trip through the loop will allow us to process another item in the RSS until we get to the end. All that’s needed to grab our data is an array followed by a couple of lines to assign data from the RSS item the loop is currently pointing to to a space in the array. For example, the following lines create an array and stores the link and title of the current item to it:
    $article = array();
    $article['link'] = $xml->channel->item[$counter]->link;
    $article['title'] = $xml->channel->item[$counter]->title;

At this point you’d have the data you want and can do whatever you want with it.

Tags: , ,

If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.