No Stress RSS with the Google Feed API

Share this article

In Craig’s recent series on Ajax data formats, we learned about JSON, a clean and efficient format for dealing with data in your Ajax pages. But if you’re dealing with blogs, news sources, or common content management systems, RSS is still the de facto standard and JSON may be unavailable. You could hack your sources to spit out the data you need in JSON, or you could write a piece of middleware to convert RSS for you, but sometimes that’s impossible or impractical. If that’s the case, why not check out Google’s Feed API? It’s a fast and easy way to grab RSS feeds and use them right away as JSON objects: no mess or fuss or weird parsing effort on your part! You’re retrieving Google’s cached copy of the RSS, so it’s quick and reliable; it’ll also cause less impact on the site serving up the feed, to boot. To show you how easy it is, we’ll build a quick JavaScript headlines widget using the latest news from SitePoint’s RSS feed. First, you’ll need to grab a Google API key. It’s optional, but quick and easy, and Google strongly encourages that you do so. To begin using the Feed API in our pages, we’ll need to include the Google loader in the head of the document. Add that API key as a variable on your script’s src URL:

<script type="text/javascript" src="https://www.google.com/jsapi?key=YOURKEY"></script>
Next, in our own script, we’ll load up the Google Feed API like so:
    google.load("feeds", "1");
This line specifies the module we want (feeds) and the version, which at this stage is still version 1. Does this google.load stuff look familiar? If you’ve seen this before, it’s probably because you’ve spent some time using Google Maps in your application, or perhaps you like to use the Google-hosted JavaScript libraries or Web fonts. There are quite a few different modules available, so if you are using more than one, you could go ahead and load them all up together, like so:
    google.load("feeds", "1");
    google.load("jquery", "1.5.1");
    google.load("maps", "2");
Next, we’ll need to specify a function to execute once the document is completely loaded — we do this using google.setOnLoadCallback. Just for something unusual, I’ll show my feed using a function called showFeed:
google.setOnLoadCallback(showFeed);
showFeed
will be where all the action takes place, and where we can look into a few of the options that the Google Feed API can offer us. Our first point of order is to set up a new object, called feed. It needs just one argument, a URL:
function showFeed() {
  var feed = new google.feeds.Feed("https://www.sitepoint.com/feed/");
}
By default, the API will give us only four items from the feed. Using the setNumEntries method, we’ll ask for some more — let’s say ten:
function showFeed() {
  var feed = new google.feeds.Feed("https://www.sitepoint.com/feed/");
  feed.setNumEntries(10);
}
There’s a setResultFormat method that allows us to specify a format: one of XML, JSON, or a combined result with both. Since the API serves up JSON by default, it’s unnecessary to actually specify it as such; if you’re curious, though, it would look like this:
feed.setResultFormat(google.feeds.Feed.JSON_FORMAT);
If you’d like to dig up older RSS entries, Google can return its stored copies of previous items in the feed. That’s a neat trick! SitePoint’s feed contains enough entries for us to use, so we can skip it in our script, but you’d use it like so:
feed.includeHistoricalEntries();
We’ve set up all we need to grab the feed — now let’s go ahead and load ‘er up. We’d like each RSS entry to appear as a list item in a list with an ID of headlines, showing each item’s title, link, and author. The API can deliver all that, and more
— content, categories, published date, and media. Of note is the ability to automatically create a snippet of each entry’s content for us, which is a nice touch. Let’s throw that in there as well! The .load method asks for the feed and delivers a result. In that result, we’ll receive a nice, fat JSON object, or an error code and message if it fails. The callback acts on that result: if successful, we’ll loop through each item in the JSON object, and insert a list item for each. Otherwise, we’ll fail politely with a link to SitePoint:
feed.load(function(result) {
  if (!result.error) {
    var container = document.getElementById("headlines");
    for (var i = 0; i < result.feed.entries.length; i++) {
      var entry = result.feed.entries[i];
      var li = document.createElement("li");
      li.innerHTML = '<h3><a href="' + entry.link + '">' + entry.title + '</a>' + <cite>by ' + entry.author + '</cite></h3>';
      li.innerHTML += '<p>' + entry.contentSnippet + '</p>';
      container.appendChild(li);
    }
  } else {
    var container = document.getElementById("headlines");
    container.innerHTML = '<li><a href="http://www.sitepoint.com">Get your geek news fix at SitePoint</a></li>';
  }
});
That’s it! You can see a complete example of this script in our demo. All up, it took just a couple of minutes to put together — that’s much shorter than the amount of time it took to explain it in this post! We’ve seen just a part of what the Feed API can deliver, but there’s plenty more to be found in the Feed API documentation. Here’s to stress-free feed parsing!
note:Want more?
If you want to read more from Raena, subscribe to our weekly tech geek newsletter, Tech Times.

Frequently Asked Questions (FAQs) about Google Feed API

What is the Google Feed API and how does it work?

The Google Feed API is a tool that allows developers to download and integrate any public RSS or Atom feed into their web application. It works by fetching a specified feed and returning it in a JSON format, which can then be easily parsed and displayed on your website. This API is particularly useful for displaying news updates, blog posts, or any other type of content that is regularly updated and syndicated via a feed.

Is the Google Feed API still available?

As of December 15, 2016, Google has officially deprecated the Feed API. This means that Google no longer supports the API, and it may not function as expected. However, there are many alternative APIs and methods available for fetching and displaying RSS feeds.

What are some alternatives to the Google Feed API?

There are several alternatives to the Google Feed API. One popular option is the NewsAPI, which provides a simple and easy-to-use interface for fetching news articles from various sources. Another option is the Newscatcher API, which offers a free news API that can be used to fetch news articles from over 300,000 sources.

How can I find the RSS feed of a website?

Most websites that offer an RSS feed will have a link to it somewhere on their page, often in the footer or header. If you can’t find it, you can try appending “/rss” or “/feed” to the end of the website’s URL. If that doesn’t work, you can use a tool like Feedly, which can automatically detect the RSS feed of a website.

How can I integrate an RSS feed into my website?

There are several ways to integrate an RSS feed into your website. One of the simplest methods is to use a JavaScript library like jQuery to fetch the feed and display it on your page. Alternatively, you can use a server-side language like PHP to fetch the feed and display it.

What is the Google Ads API and how is it different from the Google Feed API?

The Google Ads API is a tool that allows developers to interact with the Google Ads platform. It can be used to create, read, update, and delete ads, campaigns, and other entities within Google Ads. Unlike the Google Feed API, which is used to fetch and display RSS feeds, the Google Ads API is used to manage advertising campaigns.

What is the GData API?

The GData API is a protocol developed by Google for querying and updating data on the web. It is based on the Atom publishing protocol and the Atom Syndication format. The GData API can be used to read, write, and modify data on Google services like Google Calendar, Google Docs, and more.

How can I use the NewsAPI?

To use the NewsAPI, you first need to sign up for a free API key. Once you have your key, you can use it to make requests to the NewsAPI. The API returns a JSON object that contains a list of articles, each with a title, description, URL, and other information.

How can I use the Newscatcher API?

The Newscatcher API works similarly to the NewsAPI. You first need to sign up for a free API key. Once you have your key, you can use it to make requests to the Newscatcher API. The API returns a JSON object that contains a list of articles, each with a title, description, URL, and other information.

What is an RSS feed and why is it useful?

An RSS (Really Simple Syndication) feed is a type of web feed that allows users and applications to access updates to online content in a standardized, computer-readable format. These feeds can, for example, allow a user to keep track of many different websites in a single news aggregator. The news aggregator will automatically check the RSS feed for new content, allowing the content to be automatically passed from website to website or from website to user.

Raena Jackson ArmitageRaena Jackson Armitage
View Author

Raena Jackson Armitage is an Australian web developer with a background in content management, public speaking, and training. When she is not thinking about the Web, she loves knitting, gaming, all-day breakfasts, and cycling.

ajaxGoogle Tutorials & ArticlesJavaScriptjsonrss
Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week