Posted by & filed under article, Google+, JavaScript, jQuery, PHP.

Just want to see the demo and code? See the demo!

As a heavy user of Google Reader, the recent changes to the way links are shared within the app left me high and dry. I had been consuming the RSS feed from the shared stories blog that was generated, but since that was deprecated and soon to be non-existent, I needed some other way of consuming my shared links for display on blogs or status pages.

Enter Google+. At the moment, Google+ doesn’t do RSS feeds of public streams (and why would it really, given the nature of how Google+ works). There have been a couple of tools that call out and create an RSS stream from a Google+ feed (notably Russell Beattie’s very cool PlusFeed), but that wasn’t going to swing it. What to do? Google APIs.

If you’ve never used the Google APIs, I highly suggest you give the Google Developers site a quick look over. They’ve got articles and tutorials and a whole bunch of very good info on what you can do. Want to test drive the APIs in browser? Use the API Explorer.

Getting the JSON from Google+ API

Getting the JSON from the Google+ API for a public, read-only feed is not terribly hard, you simply run a GET against the properly formed Google API URL. In our case, we limit the verbose data that we get back in the JSON as much as possible for this example by limiting the fields returned.

$myGooglePlusQuery = "https://www.googleapis.com/plus/v1/people/" . $myGooglePlusUser . "/activities/public?alt=json&maxResults=" . $myGooglePlusMaxResults . "&fields=items(object(attachments(categories%2Ccontentsource%2CdisplayName%2CobjectType%2Curl)%2Curl))&pp=1&key=" . $myGoogleAPIkey;

You’re notice some PHP variables in there, defining the Google+ user we want to get the data for, the max results, and our API key (which you generate in the Google API console). This is going to return the items which have attachments, which is what we want in this case (our shared links). You can get all kinds of data (shared photos and what not), but I’m keeping this simple.

Caching the JSON with PHP

The Google+ API has a courtesy limit of 1,000 queries per day, so we use a bit of PHP to call our Google API query and than cache it for 10 minutes (see gist of entire PHP script on demo page). It’s not very fancy and could be duplicated in any shape and form in any language you want; the only thing it’s doing is a call out and writing to a file.

Displaying the data with jQuery

For a bit of fun, instead of muxing the JSON on the server side into a slightly slimmer profile, I decided to have fun with jQuery AJAX converters to convert the JSON to a custom datatype called JSONG. I check the 20 results in the converter, verify their type (I only wants articles) and then return this new set of data into $.done(). This is then displayed out to the page using jsRender (overkill for this simple example mind you).

The end result

The end result is a simple way to get, cache and display content from a public Google+ stream and display it where ever or how ever you want. The demo page shows this simple case in action, along with the code that generates it for my Google+ user.

As you can see, it doesn’t take a lot of code to work with the Google+ API, nor does it require loads of heavy lifting in jQuery to display that data out. Hopefully this will inspire you to write your own little connectors to keep information flowing from Google+ to your home pages.

2 Responses to “Pulling data from Google+ stream, caching with PHP and displaying with jQuery”

  1. Joe Porter

    Thanks so much for posting this! I didnt use it for google plus.. but i adjusted for twitter feed by a hashtag… this post got me on the right track!

    Thanks so much.

    Reply

Leave a Reply

  • (will not be published)