|
Project Information
Featured
Links
|
Google APIs Client Library for PHP The Google API Client Library enables you to work with Google APIs such as Analytics, Adsense, Google+, Calendar, Drive or Games on your server, in the language of your choice ( as long as it's PHP :P ) Status:An alpha release of the next major revision (1.0.0-alpha) of the library is available, please consider migrating. The newer version is published on GitHub. As soon as we reach 1.0.0-beta we will officially deprecate the 0.6 branch, and move all issue tracking to GitHub. Getting up and running in 60 seconds1) Download the latest release file curl "http://google-api-php-client.googlecode.com/files/google-api-php-client-0.6.7.tar.gz" -O tar -xvf google-api-php-client-0.6.7.tar.gz cd google-api-php-client/examples/ 2) Extract the library from the archive and copy the google-api-php-client directory to your project root. 3) Include and use the library in your project or take a look at an example under google-api-php-client/examples/plus. <?php
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_PlusService.php';
// Set your cached access token. Remember to replace $_SESSION with a
// real database or memcached.
session_start();
$client = new Google_Client();
$client->setApplicationName('Google+ PHP Starter Application');
// Visit https://code.google.com/apis/console?api=plus to generate your
// client id, client secret, and to register your redirect uri.
$client->setClientId('insert_your_oauth2_client_id');
$client->setClientSecret('insert_your_oauth2_client_secret');
$client->setRedirectUri('insert_your_oauth2_redirect_uri');
$client->setDeveloperKey('insert_your_simple_api_key');
$plus = new Google_PlusService($client);
if (isset($_GET['code'])) {
$client->authenticate();
$_SESSION['token'] = $client->getAccessToken();
$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}
if (isset($_SESSION['token'])) {
$client->setAccessToken($_SESSION['token']);
}
if ($client->getAccessToken()) {
$activities = $plus->activities->listActivities('me', 'public');
print 'Your Activities: <pre>' . print_r($activities, true) . '</pre>';
// We're not done yet. Remember to update the cached access token.
// Remember to replace $_SESSION with a real database or memcached.
$_SESSION['token'] = $client->getAccessToken();
} else {
$authUrl = $client->createAuthUrl();
print "<a href='$authUrl'>Connect Me!</a>";
}The next steps
|