the latest

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.

posted by /
Tyler Hall

posted on /
04/18/2007

comments /
5


when i learned about that api i was thinkin exactly the same. :) was only too lazy to dig up the goods, heh. thank you!

Kjell
06/25/07
06:01 pm


I tried changing the file_get_contents to CuRl It returned GwebSearch.RawCompletion('0',null, 400, 'invalid version', 200) Do You Know Why? thanks but file_get_contents work. athi

athi
11/04/07
04:43 pm


[...] Nashville SEO (Search Engine Optimization) and Web Design: “By Tyler HallTired 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.” [...]


More modern way: http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&output=xml&q=ESCAPED_FEED_URL The q= is the escaped URL of the feed. The result is json data. PHP Code to do it: $url = urlencode("http://digg.com/rss/index.xml"); $data = file_get_contents("http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&output=xml&q=$url"); $data = json_decode($matches[0])->xmlString; Easy. You can also find out feed addresses for websites and do searches this way: http://code.google.com/apis/ajaxfeeds/documentation/reference.html#_intro_fonje

Otto
07/08/08
11:42 am


Whoops, make that last line this: $data = json_decode($data)->xmlString;

Otto
07/08/08
11:43 am


comments


is your robot hungry? Subscribe to the feed


Categories

Archives