How-To Parse RSS Feeds With Google's Feed API

Tools

How-To Parse RSS Feeds With Google’s Feed API

Tired of processing RSS/Atom feeds in your app? Today Google announced their new Google AJAX Feed API that will do the parsing (and caching!) for you. It works great for Javascript developers, but what if you want to use a server side language like PHP? Here’s a quick (and totally unauthorized) hint to let you do so.

Digging through Google’s Javascript code, we find that they make a request to the Google mothership to parse a feed using this url:

http://www.google.com/uds/Gfeeds?callback=google.feeds.Feed.RawCompletion&context=0
&num=10&hl=en&output=xml&q=URL_GOES_HERE&v=1.0&nocache=0

In theory, just insert a link to the feed you want parsed into their url and voila! Sort of.

The feed data is returned inside a Javascript object. Before we can use it we need to extract the real data and convert JSON’s escaped unicode characters into their HTML equivalents.

<?PHP

    $url = urlencode("http://rss.cnn.com/rss/cnn_topstories.rss");

    $data = file_get_contents("http://www.google.com/uds/Gfeeds?callback=
google.feeds.Feed.RawCompletion&context=0&num=10&hl=en&output=xml&q=$url&v=1.0&nocache=0");

    preg_match('/{(.*?)}/', $data, $matches);

    $data = json_decode($matches[0])->xmlString;

?>

And with that, we’re done! You can throw any type of feed at Google’s parser and you’ll get back the data as a well-formed Atom feed. Throw in a little SimpleXML and you’re all set.

Comments are closed on this post