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 'rss_fetch.inc'; $url = 'http://myprovider.com/feed.rss'; $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="http://www.foo.com/keyframe.jpg" 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:
require_once('simplepie.inc'); $rss = new SimplePie(); $rss->set_feed_url('http://myprovider.com/feed.rss'); $rss->enable_cache(false); $rss->init(); foreach ($rss->get_items() as $item) { $user_data = $item->get_item_tags('http://myprovider.com/WebService/userdata/0.1/', 'id'); $video_id = $user_data[0]['data']; $thumbnail = $item->get_item_tags('http://search.yahoo.com/mrss/', 'thumbnail'); $thumbnail_url = $thumbnail[0]['attribs']['']['url']; }