PHP Requires Chilkat v11.0.0+
PHP
Constant Contact V3: Find a contact list by name
See more Constant Contact Examples
Start with the full list name from the Constant Contact account. Read its list_id and the number of active, mailable contacts. Use the ID for list membership operations or campaign targeting. This lookup requires contact_data.
Authorize the application using the same account and credential store.
Constant Contact developer guide
Chilkat PHP Downloads
<?php
use Chilkat2\Http;
use Chilkat2\HttpRequest;
use Chilkat2\HttpResponse;
use Chilkat2\JsonObject;
use Chilkat2\Secrets;
$success = false;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
$vault = new Secrets();
$vault->Location = 'local_manager';
$vault->AutoSplit = true;
$entry = new JsonObject();
$entry->UpdateString('appName', 'ConstantContactV3');
$entry->UpdateString('service', 'OAuth2');
$entry->UpdateString('username', 'my-account');
$tokens = new JsonObject();
try {
$vault->GetSecretJson($entry, $tokens);
} catch (\Chilkat2\Exception $e) {
echo "Run the desktop authorization example using this account and store.\n";
exit;
}
if ($tokens->JsonTypeOf('access_token') != 1) {
echo "The saved authorization has no access token.\n";
exit;
}
$http = new Http();
$http->RequireSslCertVerify = true;
$http->AuthToken = $tokens->StringOf('access_token');
$resp = new HttpResponse();
// Use the full display name. AddParam encodes spaces and punctuation.
$listName = 'Workshop announcements';
$query = new HttpRequest();
$query->HttpVerb = 'GET';
$query->AddParam('name', $listName);
$query->AddParam('status', 'active');
$query->AddParam('include_membership_count', 'active');
try {
$http->HttpReq('https://api.cc.email/v3/contact_lists', $query, $resp);
$success = true;
} catch (\Chilkat2\Exception $e) {
$success = false;
}
if ((!$success) && ($resp->StatusCode <= 0)) {
echo "Look up list: HTTP request failed.\n";
exit;
}
if ((!$success) || ($resp->StatusCode != 200)) {
echo 'Look up list: HTTP ', $resp->StatusCode, ' ', $resp->BodyStr, "\n";
exit;
}
$matches = new JsonObject();
try {
$matches->Load($resp->BodyStr);
} catch (\Chilkat2\Exception $e) {
echo "Expected a JSON object in the response.\n";
exit;
}
if ($matches->SizeOfArray('lists') == 0) {
echo 'No active email list named ', $listName, "\n";
exit;
}
if (($matches->SizeOfArray('lists') != 1) || (!$matches->StringOfEquals('lists[0].name', $listName, true))) {
echo "Expected one exact list name.\n";
exit;
}
$listId = $matches->StringOf('lists[0].list_id');
if ($listId === '') {
echo "Missing list ID.\n";
exit;
}
echo $listName, ' | List ID: ', $listId, ' | Mailable contacts: ', $matches->IntOf('lists[0].membership_count'), "\n";