Locate Google API Client Service classes
latestgoogle-api-php-client-services. If you need to access specific service classes (e.g., Drive, Sheets, etc.), you must use the external services repository.repository·latest·Indexed 43 days ago
https://github.com/udx/wp-statelessA WordPress plugin for handling media files via Google Cloud Storage. It offers four operating modes: Backup (uploads to GCS but serves local URLs), CDN (copies to GCS and serves from GCS URLs), Ephemeral (stores and serves via GCS with temporary local processing), and Stateless (stores and serves via GCS without local storage). Supports manual Service Account JSON configuration with Storage Admin roles.
google-api-php-client-services. If you need to access specific service classes (e.g., Drive, Sheets, etc.), you must use the external services repository.In version 2.0 and later, authentication data structures have changed from JSON strings to associative arrays.
getAccessToken() now returns an array instead of a JSON string. When saving tokens, you must use json_encode().verifyIdToken() now returns an array directly, rather than a ticket object with a getAttributes() method.// Access Tokens
$accessToken = $client->getAccessToken();
file_put_contents($credentialsPath, json_encode($accessToken));
// ID Tokens
$userData = $client->verifyIdToken($idToken);
$userId = $userData['sub'];Google_Cache_Abstract has been removed. Existing cache classes like Google_Cache_Apc, Google_Cache_File, Google_Cache_Memcache, and Google_Cache_Null have been updated to implement Google\Auth\CacheInterface.The Google_Auth_Abstract class and its methods sign and authenticatedRequest have been removed. They are replaced by Google_Client::authorize. This method accepts a GuzzleHttp\ClientInterface object and follows this authentication priority:
Google_Client::useApplicationDefaultCredentials. It looks for the GOOGLE_APPLICATION_CREDENTIALS environment variable, then ~/.config/gcloud/application_default_credentials.json, and finally falls back to GCECredentials.Google_Client::setDeveloperKey.Google_Client::setAccessToken.Note: The client will automatically refresh access tokens if an expired one is provided.
// Instead of Google_Auth_Abstract::sign, use:
$client->authorize($guzzleClient);WP-Stateless offers four distinct modes for handling media files, depending on your storage and delivery requirements:
The library uses autogenerated classes that map to the APIs Explorer JSON structure. You can build requests in two ways:
Google\Service\Datastore\Query) and their setter methods.It is highly recommended to understand the JSON syntax of the target API before using these services.
// Object-based approach example for Datastore
$datastore = new Google\Service\Datastore($client);
$request = new Google\Service\Datastore_RunQueryRequest();
$query = new Google\Service\Datastore\Query();
$order = new Google\Service\Datastore_PropertyOrder();
$order->setDirection('descending');
$property = new Google\Service\Datastore\PropertyReference();
$property->setName('title');
$order->setProperty($property);
$query->setOrder([$order]);
$kind = new Google\Service\Datastore\KindExpression();
$kind->setName('Book');
$query->setKinds([$kind]);
$query->setLimit(10);
$request->setQuery($query);
$response = $datastore->projects->runQuery('YOUR_DATASET_ID', $request);Execute the test suite using PHPUnit. Note that running all calls may require configuring an API key and token in BaseTest.php via the Google Developer Console.
phpunit tests/Use phpcs to identify coding style violations based on the project's ruleset, and phpcbf to automatically fix fixable violations.
To check violations:
vendor/bin/phpcs src --standard=style/ruleset.xml -npTo automatically fix violations:
vendor/bin/phpcbf src --standard=style/ruleset.xmlThe library now requires Composer for installation and autoloading. Instead of manual file inclusion, use Composer to manage dependencies and generate the autoloader.
$ composer require google/apiclient:~2.0// Include the generated autoloader
require_once 'vendor/autoload.php';Service accounts are used for server-to-server authentication. Note that some APIs (like YouTube Data API) do not support them.
Option 1: Using Environment Variables
Set the GOOGLE_APPLICATION_CREDENTIALS environment variable to the path of your JSON key, then call $client->useApplicationDefaultCredentials().
Option 2: Using a specific JSON key array
Pass the credentials array directly to $client->setAuthConfig($jsonKey).
Impersonation
If you have domain-wide delegation, you can impersonate a user by calling $client->setSubject($user_email).
// Option 1: Environment Variable
putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account.json');
$client = new Google\Client();
$client->useApplicationDefaultCredentials();
$client->addScope(Google\Service\Drive::DRIVE);
// Option 2: Specific JSON key array
$jsonKey = [
'type' => 'service_account',
// ...
];
$client = new Google\Client();
$client->setAuthConfig($jsonKey);
// Impersonation
$client->setSubject($user_to_impersonate);Some Google services return XML or other formats by default. To ensure the library receives a JSON response, add an alt key with the value json to the optional parameters array. This array is typically passed as the last argument to a method call.
$opt_params = array(
'alt' => "json"
);If you do not use Composer, you can download the package directly from the Releases page.
google-api-php-client-[RELEASE_NAME].zip.require_once '/path/to/google-api-php-client/vendor/autoload.php';