I'm currently trying to get a list of all users inside the domain and the logic I'm using is the following:
$service = new Google_Service_Directory($client);
$optParams = array(
'customer' => 'my_customer',
'maxResults' => 500,
'orderBy' => 'email',
);
$results = $service->users->listUsers($optParams);
$users = $results->getUsers();
foreach($users as $user) {
$usersemails = $user->getPrimaryEmail();
echo $usersemails.'<br>';
}
The problem is I only get a max of 500 users. I figure out that I have to use the next page token so I tried this:
$service = new Google_Service_Directory($client);
$optParams = array(
'customer' => 'my_customer',
'maxResults' => 500,
'orderBy' => 'email',
'pageToken' => NULL,
);
$results = $service->users->listUsers($optParams);
$pageToken = $results->getNextPageToken();
$users = $results->getUsers();
while($pageToken);
foreach($users as $user) {
$usersemails = $user->getPrimaryEmail();
echo $usersemails.'<br>';
}
But I'm getting the following message:
504 Gateway Time-out. The server didn't respond in time.
Is there a problem with the code I'm using or is this a problem with the server?
After checking many times I was able to find out that the problem was that I was not properly writing the code. I have modified my code and now it works fine. In case anybody else goes through the same problem perhaps this might help. This is the final code:
$service = new Google_Service_Directory($client);
$pageToken = NULL;
$optParams = array(
'customer' => 'my_customer'
);
try {
do {
if ($pageToken){
$optParams['pageToken'] = $pageToken;
}
$results = $service->users->listUsers($optParams);
$pageToken = $results->getNextPageToken();
$users = $results->getUsers();
foreach($users as $user) {
$usersemails = $user->getPrimaryEmail();
echo $usersemails.'<br>';
}
} while($pageToken);
} catch (Exception $e) {
print 'An error occurred: ' . $e->getMessage();
}