Skip to content

Instantly share code, notes, and snippets.

@justinribeiro
Created November 16, 2011 01:56
Show Gist options
  • Save justinribeiro/1369019 to your computer and use it in GitHub Desktop.
Save justinribeiro/1369019 to your computer and use it in GitHub Desktop.
Get's a Google Plus stream in JSON and caches locally for display elsewhere
// JDR: who you are, and how many results you want
$myGooglePlusUser = "{YOUR_GOOGLE_PLUS_ID_NUMBER_HERE}";
$myGooglePlusMaxResults = 20;
// JDR: get your Google API key from the APIs Console: https://code.google.com/apis/console#access
$myGoogleAPIkey = "{YOUR_API_KEY_HERE}";
$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;
$myCacheFile = "me.json"; // JDR: name the cache file whatever you like
$myCacheCycle = "600"; // JDR: in seconds
// JDR: technically we don't need this really, but if you ever need other header info, this is the way
$setContextOptions = array(
'http'=> array(
'method'=>"GET"
)
);
$createContext = stream_context_create($setContextOptions);
// JDR: let's get our cache file, returns false if file does not exist
$getCacheFileModTime = @filemtime( $myCacheFile );
if ( !$getCacheFileModTime || ( time() - $getCacheFileModTime >= $myCacheCycle) )
{
// JDR: go get me some json, returns false if error
$getGooglePlusData = file_get_contents($myGooglePlusQuery, false, $createContext);
if ($getGooglePlusData) {
// write the JSON to file, return it to the caller
file_put_contents( $myCacheFile, $getGooglePlusData );
$outputJSON = $getGooglePlusData;
}
else {
// JDR: uh oh, we have a problem, write some simple error json to tell us on the frontend
$errorArray = array('error' => 'There was a problem getting data from the Google API, please try again.');
$outputJSON = json_encode($errorArray);
}
}
else
{
// JDR: get the cache file
$outputJSON = file_get_contents($myCacheFile);
}
// JDR: gzip it if we can
ob_start('ob_gzhandler');
header('Content-type: application/json');
echo $outputJSON;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment