1. Go to this page and download the library: Download brickservers/gsuite library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
use BrickServers\GoogleWorkspace\GoogleWorkspace;
// Via the service container
$workspace = app('google-workspace');
// Via the facade (auto-registered as GSuite)
GSuite::users()->get('[email protected]');
GSuite::asUser('[email protected]')->gmail()->usersLabels()->listUsersLabels('me');
// Or using dependency injection
public function __construct(GoogleWorkspace $workspace)
{
$this->workspace = $workspace;
}
// Or inject just the repository you need (handy for mocking in tests)
use BrickServers\GoogleWorkspace\Contracts\UsersRepositoryContract;
public function __construct(UsersRepositoryContract $users) { /* ... */ }
$user = $workspace->users()->get('[email protected]');
// With projection and view type
use BrickServers\GoogleWorkspace\Enums\UserProjection;
use BrickServers\GoogleWorkspace\Enums\UserViewType;
$user = $workspace->users()->get(
'[email protected]',
projection: UserProjection::FULL,
viewType: UserViewType::ADMIN_VIEW,
);
$result = $workspace->users()->list(maxResults: 100);
$users = $result['users']; // Array of UserDTO
$nextPageToken = $result['nextPageToken']; // For pagination
// Or iterate every user; pages are fetched lazily as you go
foreach ($workspace->users()->all() as $user) {
echo $user->email;
}
// Change just the first name
$workspace->users()->update('[email protected]', new UserDTO(
email: '[email protected]',
givenName: 'Johnny',
));
// Pass an empty email to leave the address alone; false values are sent as-is
$workspace->users()->update('[email protected]', new UserDTO(
email: '',
changePasswordAtNextLogin: false,
));
$result = $workspace->groups()->list(maxResults: 100);
$groups = $result['groups']; // Array of GroupDTO
$nextPageToken = $result['nextPageToken']; // For pagination
// Or iterate every group
foreach ($workspace->groups()->all() as $group) {
echo $group->email;
}
// Fields left null are not changed
$updates = new GroupDTO(
email: '[email protected]',
name: 'Dev Team',
);
$updated = $workspace->groups()->update('[email protected]', $updates);
use Google\Service\Gmail\VacationSettings;
$jane = $workspace->asUser('[email protected]');
$jane->gmail()->usersSettings()->updateVacation('me', new VacationSettings([
'enableAutoReply' => true,
'responseSubject' => 'Out of office',
'responseBodyPlainText' => 'Back on Monday.',
]));
$events = $jane->calendar()->events()->listEvents('primary');
$files = $jane->drive()->files()->listFiles(['pageSize' => 50]);
use Google\Service\Gmail;
$jane = $workspace->asUser('[email protected]', [Gmail::GMAIL_SETTINGS_BASIC, Gmail::GMAIL_SETTINGS_SHARING]);
foreach ($workspace->directory()->mobiledevices()->paginate('listMobiledevices', 'my_customer') as $device) {
// every device, across all pages
}
foreach ($jane->drive()->files()->paginate('listFiles', ['q' => "mimeType = 'application/pdf'"]) as $file) {
// ...
}
$batch = $workspace->batch();
$result = $batch->createUsers([$userA, $userB]);
$result = $batch->addGroupMembers('[email protected]', ['[email protected]', '[email protected]']);
$result = $batch->removeGroupMembers('[email protected]', ['[email protected]']);
$result = $batch->suspendUsers(['[email protected]']); // one call per user, so protected users are checked
// ['success' => [...], 'failed' => [['email' => ..., 'error' => ...], ...]]
// createUsers() returns UserDTOs in 'success' and uses a 'user' key in 'failed'
use BrickServers\GoogleWorkspace\Exceptions\GoogleWorkspaceException;
try {
$workspace->users()->create($user);
} catch (GoogleWorkspaceException $e) {
if ($e->getCode() === 5) {
// Resource not found
} elseif ($e->getCode() === 429) {
// Still rate limited after automatic retries
}
// The original Google error is available for details
$original = $e->getPrevious();
logger()->error('Google Workspace API error: ' . $e->getMessage());
}
new UserDTO(
email: string,
givenName: ?string = null,
familyName: ?string = null,
password: ?string = null,
changePasswordAtNextLogin: ?bool = null,
suspended: ?bool = null,
phone: ?string = null, // sent as the primary work phone
title: ?string = null, // sent as the primary organization title
customSchemas: ?array = null,
)
UserProjection::BASIC // Basic information only
UserProjection::FULL // Full user information
UserProjection::CUSTOM // Custom schema information
UserViewType::ADMIN_VIEW // Admin perspective
UserViewType::DOMAIN_PUBLIC // Public domain view
ApiScope::DIRECTORY_USER
ApiScope::DIRECTORY_GROUP
ApiScope::CLASSROOM_COURSES
ApiScope::CALENDAR
ApiScope::GMAIL_COMPOSE
ApiScope::DRIVE
// ... and more