google/apiclient-services
Auto-generated PHP classes for Google API services, updated daily and tagged weekly. Installed automatically with the Google API PHP Client, it provides typed service classes and resources for calling Google APIs directly from PHP.
Installation:
composer require google/apiclient-services
Ensure google/apiclient (v2.0+) is also installed as a dependency.
First Use Case: Authenticate and List Drive Files
use Google\Service\Drive;
use Google\Client;
// Configure OAuth 2.0 client
$client = new Client();
$client->setAuthConfig('path/to/service-account.json'); // Or use user credentials
$client->addScope(Drive::DRIVE);
// Create Drive service
$driveService = new Drive($client);
// List files in root folder
$files = $driveService->files->listFiles([
'pageSize' => 10,
'fields' => 'files(id, name, mimeType)'
])->getFiles();
return $files;
Laravel Integration:
AppServiceProvider:
public function register()
{
$this->app->singleton(Drive::class, function ($app) {
$client = new Client();
$client->setAuthConfig(config('services.google.drive'));
$client->addScope(Drive::DRIVE);
return new Drive($client);
});
}
public function index(Drive $driveService)
{
$files = $driveService->files->listFiles(['pageSize' => 10])->getFiles();
return response()->json($files);
}
google/apiclient: Core library for OAuth, HTTP clients, and shared utilities.Google_Service_Exception and Google_Auth_Exception for debugging.Service Provider Pattern
AppServiceProvider or a dedicated GoogleServiceProvider.$this->app->bind('google.drive', function ($app) {
$client = new Client();
$client->setAuthConfig(config('services.google.drive'));
$client->addScope(Drive::DRIVE);
return new Drive($client);
});
use Illuminate\Support\Facades\GoogleDrive;
public function upload()
{
$file = GoogleDrive::files()->create([
'name' => 'test.txt',
'mimeType' => 'text/plain'
], upload('test.txt'));
}
Facade Pattern
GoogleDrive, GoogleGmail).class GoogleDrive extends Facade
{
protected static function getFacadeAccessor() { return 'google.drive'; }
}
Job-Based Batch Processing
class SyncDriveFilesJob implements ShouldQueue
{
public function handle(Drive $driveService)
{
$results = $driveService->files->listFiles([
'pageSize' => 100,
'fields' => 'nextPageToken, files(id, name)'
]);
foreach ($results->getFiles() as $file) {
// Process file (e.g., store metadata in DB)
}
if ($results->getNextPageToken()) {
SyncDriveFilesJob::dispatch($driveService, $results->getNextPageToken());
}
}
}
Event-Driven Workflows
DriveFileUploaded).event(new DriveFileUploaded($fileId, $userId));
// In an event listener:
public function handle(DriveFileUploaded $event)
{
// Notify user or update UI
}
Filesystem Adapter
Illuminate\Filesystem\FilesystemAdapter to wrap Drive API calls.class DriveAdapter extends FilesystemAdapter
{
public function put($path, $contents, $options = [])
{
$file = $this->driveService->files->create([
'name' => basename($path),
'parents' => [$this->rootFolderId]
], $contents);
return $file->getId();
}
}
config/filesystems.php:
'disks' => [
'drive' => [
'driver' => 'google-drive',
'root_folder_id' => env('GOOGLE_DRIVE_ROOT_FOLDER'),
],
],
OAuth Flow
Sanctum or Passport for token management.$client = new Client();
$client->setAuthConfig(config('services.google'));
$client->setAccessToken($user->google_token); // From Sanctum/Passport
Retry Logic
use Google\ApiCore\ApiException;
try {
$response = $driveService->files->get($fileId);
} catch (ApiException $e) {
if ($e->getStatusCode() === 429) {
sleep(2 ** $e->getRetryInfo()->getExponent());
return $this->handle($request);
}
throw $e;
}
Pagination
pageToken for large datasets.$optParams = ['pageSize' => 100, 'fields' => 'nextPageToken, files(id)'];
$results = $driveService->files->listFiles($optParams);
do {
$files = $results->getFiles();
// Process files
$optParams['pageToken'] = $results->getNextPageToken();
$results = $driveService->files->listFiles($optParams);
} while ($results->getNextPageToken());
Async Operations
SyncGmailLabelsJob::dispatch($gmailService, $labelId)->onQueue('google');
.env and config/services.php:
'google' => [
'drive' => [
'key' => env('GOOGLE_DRIVE_KEY'),
'scopes' => [Drive::DRIVE],
],
],
config('services.google.drive') in different environments (dev/staging/prod).Mockery or Vcr:
$mock = Mockery::mock(Drive::class);
$mock->shouldReceive('files')->andReturnSelf();
$mock->shouldReceive('listFiles')->andReturn(new Google_Service_Drive_DriveFileList());
try {
$response = $driveService->files->get($fileId);
} catch (Exception $e) {
Log::error('Google Drive API error', ['exception' => $e]);
throw $e;
}
API Deprecation
composer why-not google/apiclient-services to check for updates.OAuth Scopes
Drive::DRIVE vs. Drive::DRIVE_READONLY).Rate Limits
Token Expiry
Sanctum or Passport for token persistence and refresh logic.Large File Uploads
Google\Service\Drive\DriveFile with mediaUpload:How can I help you explore Laravel packages today?