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

Constant Contact V3: Restore a deleted contact to a list

See more Constant Contact Examples

Look up the deleted contact by email, then restore it by adding its existing ID to a chosen list. Constant Contact documents this membership activity as a restoration method. It changes only membership, preserves the saved profile, and makes no email-permission claim. Select the intended destination list explicitly; previous memberships were removed by deletion. Authorize the app first and use the same credential store.

Constant Contact developer guide

Chilkat PHP Downloads

PHP
<?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();

$query = new HttpRequest();
$query->HttpVerb = 'GET';
$query->AddParam('email', 'reader+newsletter@example.com');
$query->AddParam('status', 'deleted');
try {
    $http->HttpReq('https://api.cc.email/v3/contacts', $query, $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;
}

$matches = new JsonObject();
try {
    $matches->Load($resp->BodyStr);
} catch (\Chilkat2\Exception $e) {
    echo "Expected a JSON response.\n";
    exit;
}

if (($matches->SizeOfArray('contacts') != 1) || ($matches->JsonTypeOf('contacts[0].deleted_at') != 1)) {
    echo "Expected one deleted contact for this email. No restoration submitted.\n";
    exit;
}

if (($matches->JsonTypeOf('contacts[0].contact_id') != 1) || ($matches->StringOfEquals('contacts[0].contact_id', '', true))) {
    echo "Missing contacts[0].contact_id in the response.\n";
    exit;
}

$body = new JsonObject();
$body->UpdateString('source.contact_ids[0]', $matches->StringOf('contacts[0].contact_id'));
$body->UpdateString('list_ids[0]', 'DESTINATION_LIST_ID');
try {
    $http->HttpJson('POST', 'https://api.cc.email/v3/activities/add_list_memberships', $body, 'application/json', $resp);
    $success = true;
} catch (\Chilkat2\Exception $e) {
    $success = false;
}
if ((!$success) || (($resp->StatusCode < 200) || ($resp->StatusCode >= 300))) {
    echo 'Could not start the membership activity. HTTP ', $resp->StatusCode, "\n";
    exit;
}

$job = new JsonObject();
try {
    $job->Load($resp->BodyStr);
} catch (\Chilkat2\Exception $e) {
    echo "Expected a JSON response.\n";
    exit;
}

if (($job->JsonTypeOf('activity_id') != 1) || ($job->StringOfEquals('activity_id', '', true))) {
    echo "Missing activity_id in the response.\n";
    exit;
}

$activityId = $job->StringOf('activity_id');
echo 'Submitted activity ID: ', $activityId, "\n";
$http->SetUrlVar('job', $activityId);
// Poll briefly. A longer-running job can be checked later using the same ID.
$attempt = 0;
while ($attempt < 5) {
    try {
        $http->HttpNoBody('GET', 'https://api.cc.email/v3/activities/{$job}', $resp);
        $success = true;
    } catch (\Chilkat2\Exception $e) {
        $success = false;
    }
    if ((!$success) || ($resp->StatusCode != 200)) {
        echo 'Status check returned HTTP ', $resp->StatusCode, ". Keep the activity ID and check again later.\n";
        exit;
    }

    try {
        $job->Load($resp->BodyStr);
        $success = true;
    } catch (\Chilkat2\Exception $e) {
        $success = false;
    }
    if ((!$success) || ($job->JsonTypeOf('state') != 1)) {
        echo "Missing activity state. Keep the activity ID.\n";
        exit;
    }

    $state = $job->StringOf('state');
    if (($state !== 'initialized') && ($state !== 'processing')) {
        echo 'Activity state: ', $state, "\n";
        $i = 0;
        while ($i < $job->SizeOfArray('activity_errors')) {
            $job->I = $i;
            echo $job->StringOf('activity_errors[i]'), "\n";
            $i = $i + 1;
        }

        exit;
    }

    $attempt = $attempt + 1;
    if ($attempt < 5) {
        $http->SleepMs(2000);
    }
}

echo "Still processing. Recheck the activity ID before submitting another request.\n";