I'm trying to add pagination to my code after retrieving the contacts using Google's People API.
I've tested it with pagesize => 1
but I struggle to get "nextPageToken" to be able to go to the next record.
I got no idea how to get "nextPageToken"
Reason why I want that to be done is I got over 4000 contacts but can only get the first 1000 contacts, so I need to be able to goto the next 1000 contacts.
session_start();
require __DIR__ . '/vendor/autoload.php';
$client = new Google_Client();
$client->setAuthConfig('MyloginKey.json');
$redirect_uri = 'localhost';
$client->setRedirectUri($redirect_uri);
$client->setApprovalPrompt("force");
$client->addScope('profile');
$client->addScope('email');
$client->addScope('https://www.googleapis.com/auth/contacts.readonly');
if (isset($_GET['oauth'])) {
// Start auth flow by redirecting to Google's auth server
$auth_url = $client->createAuthUrl();
header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
} else if (isset($_GET['code'])) {
// Receive auth code from Google, exchange it for an access token, and redirect to your base URL
$client->authenticate($_GET['code']);
$_SESSION['access_token'] = $client->getAccessToken();
$redirect_uri = 'https://' . $_SERVER['HTTP_HOST'] . '/';
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
} else if ($_SESSION['access_token']) {
// You have an access token; use it to call the People API
$client->setAccessToken($_SESSION['access_token']);
$service = new Google_Service_PeopleService($client);
// $PageToken="GgYKAggFEAI";
$optParams = array(
'pageSize' => 1,
'personFields' => 'names,emailAddresses,phoneNumbers',
'sortOrder' => 'FIRST_NAME_ASCENDING',
// 'pageToken' => 'nextPageToken',
);
$connections = $service->people_connections->listPeopleConnections('people/me', $optParams);
echo "<br>";
echo '<pre>';
var_dump($optParams);
echo '</pre>';
foreach ($connections as $connection) {
if (!empty($connection->getNames()[0])) {
echo $connection->getNames()[0]->getDisplayName() . "\n" . '<br>';
if (!empty($connection->getphoneNumbers()[0])) {
echo $connection->getphoneNumbers()[0]->getcanonicalForm() . "\n" . '<br>';
} else {
echo "0";
echo "<br>";
}
if (!empty($connection->getemailAddresses()[0])) {
echo $connection->getemailAddresses()[0]->getvalue() . "\n" . '<br>';
} else {
echo "0";
echo "<br>";
}
}
}
} else {
$redirect_uri = 'https://' . $_SERVER['HTTP_HOST'] . '/?oauth';
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
i Manage to figure it out, please see correction down here
$optParams = array(
'pageSize' => 1000,
'personFields' => 'names,emailAddresses,phoneNumbers',
'sortOrder' => 'FIRST_NAME_ASCENDING',
'pageToken' => 'PASTE YOUR NEXT TOKEN HERE',
// 'requestSyncToken' => 'true',
);
$connections = $service->people_connections->listPeopleConnections('people/me', $optParams);
echo $token = $connections->getnextPageToken(); echo "<br>"; //GET NEXT TOKEN
echo $contactsTotal = $connections->gettotalItems(); echo "<br>"; //GET TOTAL CONTACTS