PHP RSS Parsers

Posted on: 04 April 2008

So, the next thing to build in my site is an RSS parser. I'm uploading video files to a hosting provider, who generates their own unique ID for each video. However, when users browse my site they want to play these videos, which means that I need to know the hosting provider's ID for the video the user wants to watch.

My video host provides an RSS feed of my uploaded content, which includes their IDs. So I need a scheduled task which will poll this feed and parse it so the IDs can be written back to my database.

The first try was using Magpie RSS, which took about 10 minutes to download, install, and get working.

require_once '';
$url = '';
$rss = fetch_rss($url);

$rss is now a structured array containing all the data from the provider's RSS feed, and can be accessed like any other array:

print "Channel: ".$rss->channel['title'];

The big drawback is that the array only contains the data from the XML elements in the feed - it doesn't include the attributes. My provider's feed conforms to Yahoo's Media RSS specification, which includes a <media:thumbnail> element where all the thumbnail information is held in attributes:

<media:thumbnail url="" width="75" height="50" time="12:05:01.123" />

So the next candidate was Simple Pie. This proved to be much more flexible and capable, but a bit more complex to get up and running, but got there in the end:

$rss = new SimplePie();

foreach ($rss->get_items() as $item) {
  $user_data = $item->get_item_tags('', 'id');
  $video_id = $user_data[0]['data'];

  $thumbnail = $item->get_item_tags('', 'thumbnail');
  $thumbnail_url = $thumbnail[0]['attribs']['']['url'];