Sample code for 30+ languages & platforms
PHP Requires Chilkat v11.0.0+

Constant Contact V3: Read every page of list members

See more Constant Contact Examples

Filter the contacts collection with lists and a list ID. To query multiple lists together, supply their comma-separated IDs. Follow the returned cursor links and report the total after the last page. The default contacts query excludes deleted records. A list member can still have unsubscribed email permission. Authorize the app first and use the same credential store.

Constant Contact developer guide

Chilkat PHP Downloads

PHP
<?php

use Chilkat2\Http;
use Chilkat2\HttpResponse;
use Chilkat2\JsonObject;
use Chilkat2\Secrets;
use Chilkat2\StringBuilder;

$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();

$http->SetUrlVar('list', 'LIST_ID');
$url = new StringBuilder();
$url->Append('https://api.cc.email/v3/contacts?lists={$list}&limit=50');
$page = new JsonObject();
$total = 0;
while ($url->Length > 0) {
    try {
        $http->HttpNoBody('GET', $url->GetAsString(), $resp);
        $success = true;
    } catch (\Chilkat2\Exception $e) {
        $success = false;
    }
    if ((!$success) || ($resp->StatusCode != 200)) {
        echo 'Expected HTTP 200. Received ', $resp->StatusCode, "\n";
        if ($resp->StatusCode == 401) {
            echo "Refresh the saved access token or authorize again.\n";
        }

        exit;
    }

    try {
        $page->Load($resp->BodyStr);
        $success = true;
    } catch (\Chilkat2\Exception $e) {
        $success = false;
    }
    $count = $page->SizeOfArray('contacts');
    if ((!$success) || ($count < 0)) {
        echo "Expected a page of contacts.\n";
        exit;
    }

    $i = 0;
    while ($i < $count) {
        $page->I = $i;
        echo $page->StringOf('contacts[i].contact_id'), '  ', $page->StringOf('contacts[i].email_address.address'), "\n";
        $i = $i + 1;
    }

    $total = $total + $count;
    $url->Clear();
    if ($page->JsonTypeOf('_links.next.href') == 1) {
        $url->Append($page->StringOf('_links.next.href'));
        // The documented cursor link is relative to api.cc.email.
        if (!$url->StartsWith('/v3/contacts?', true)) {
            echo "Unexpected pagination link. Stopped before following it.\n";
            exit;
        }

        $url->Prepend('https://api.cc.email');
    }
}

echo 'List members read: ', $total, "\n";