CkPhp Requires Chilkat v11.0.0+
CkPhp
Constant Contact V3: Add existing contacts to a list
See more Constant Contact Examples
Submit two existing contact IDs and one destination list ID. Constant Contact queues the change as a bulk activity. Save its ID, check the activity state and item errors, and resume checking that ID if the first few polls are inconclusive. Authorize the app first and use the same credential store.
Constant Contact developer guide
Chilkat CkPhp Downloads
<?php
include("chilkat.php");
$success = false;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
$vault = new CkSecrets();
$vault->put_Location('local_manager');
$vault->put_AutoSplit(true);
$entry = new CkJsonObject();
$entry->UpdateString('appName','ConstantContactV3');
$entry->UpdateString('service','OAuth2');
$entry->UpdateString('username','my-account');
$tokens = new CkJsonObject();
$loaded = $vault->GetSecretJson($entry,$tokens);
if ($loaded != true) {
print 'Run the desktop authorization example using this account and store.' . "\n";
exit;
}
if ($tokens->JsonTypeOf('access_token') != 1) {
print 'The saved authorization has no access token.' . "\n";
exit;
}
$http = new CkHttp();
$http->put_RequireSslCertVerify(true);
$http->put_AuthToken($tokens->stringOf('access_token'));
$resp = new CkHttpResponse();
// Use contact IDs from email lookups and the list ID from the selected destination.
$body = new CkJsonObject();
$body->UpdateString('source.contact_ids[0]','FIRST_CONTACT_ID');
$body->UpdateString('source.contact_ids[1]','SECOND_CONTACT_ID');
$body->UpdateString('list_ids[0]','DESTINATION_LIST_ID');
$success = $http->HttpJson('POST','https://api.cc.email/v3/activities/add_list_memberships',$body,'application/json',$resp);
if (($success != true) or ($resp->get_StatusCode() < 200) or ($resp->get_StatusCode() >= 300)) {
print 'Could not start the membership activity. HTTP ' . $resp->get_StatusCode() . "\n";
exit;
}
$job = new CkJsonObject();
$success = $job->Load($resp->bodyStr());
if ($success != true) {
print 'Expected a JSON response.' . "\n";
exit;
}
if (($job->JsonTypeOf('activity_id') != 1) or ($job->StringOfEquals('activity_id','',true) == true)) {
print 'Missing activity_id in the response.' . "\n";
exit;
}
$activityId = $job->stringOf('activity_id');
print '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)) {
$success = $http->HttpNoBody('GET','https://api.cc.email/v3/activities/{$job}',$resp);
if (($success != true) or ($resp->get_StatusCode() != 200)) {
print 'Status check returned HTTP ' . $resp->get_StatusCode() . '. Keep the activity ID and check again later.' . "\n";
exit;
}
$success = $job->Load($resp->bodyStr());
if (($success != true) or ($job->JsonTypeOf('state') != 1)) {
print 'Missing activity state. Keep the activity ID.' . "\n";
exit;
}
$state = $job->stringOf('state');
if (($state != 'initialized') and ($state != 'processing')) {
print 'Activity state: ' . $state . "\n";
$i = 0;
while (($i < $job->SizeOfArray('activity_errors'))) {
$job->put_I($i);
print $job->stringOf('activity_errors[i]') . "\n";
$i = $i + 1;
}
exit;
}
$attempt = $attempt + 1;
if ($attempt < 5) {
$http->SleepMs(2000);
}
}
print 'Still processing. Recheck the activity ID before submitting another request.' . "\n";
?>