Skip to content

Sending HTTP requests to read, write or delete contact data

Stefan Kientzler edited this page Apr 25, 2023 · 2 revisions

After a successful login and saved access token, the several API resources can be accessed.

Each access to a API resource requires a instance of the GClient class initialized with a valid access token. Furthermore the expiration of the used access token have to be checked. If the token has expired, first a new token have to be requested befor continueing.

$oSecrets = new GSecrets();
$oClient = new GClient();
$oClient->setAccessToken($oSecrets->getAccessToken());
if ($oClient->isAccessTokenExpired()) {
    // try to refresh the accesstoken
    $strRefreshToken = $oSecrets->getRefreshToken();
    if (empty($strRefreshToken)) {
        // no refresh token available - redirect to google login
        header('Location: ./GoogleLogin.php');
        exit;
    }
    $oClient->setOAuthClient($oSecrets->getClientSecrets());
    $oSecrets->saveAccessToken($oClient->refreshAccessToken($strRefreshToken));
}

Use the created GClient instance to access the API resource.

$oContacts = new GContacts($oClient);
$oContacts->addPersonFields(GContacts::DEF_LIST_PERSON_FIELDS);
$oContacts->setPageSize(50);
$aContactList = $oContacts->search($strSearch);
...

If one of the methods that calls the google API fails (if it returns false), the last responsecode and furter informations can be retrieved through following methods of the GClient instance

  • $oClient->getLastResponseCode()
  • $oClient->getLastError()
  • $oClient->getLastStatus()

Clone this wiki locally