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

Constant Contact V3: Find a campaign and its primary email activity

See more Constant Contact Examples

Enter the unique campaign name from the account. Follow the campaign collection’s cursor until the name matches, then fetch that campaign’s details. Select the activity whose role is primary_email. A permalink or resend can appear first.

The example writes the two IDs to newsletter-selection.json, the same file produced by the draft example. Run this lookup when selecting an existing newsletter for review or scheduling.

Authorize the application using the same account and credential store.

Constant Contact developer guide

Chilkat PHP ActiveX Downloads

PHP ActiveX
<?php

$success = 0;

//  Assumes Chilkat is unlocked. See Global Unlock Sample.
$vault = new COM("Chilkat.Secrets");
$vault->Location = 'local_manager';
$vault->AutoSplit = 1;
$entry = new COM("Chilkat.JsonObject");
$entry->UpdateString('appName','ConstantContactV3');
$entry->UpdateString('service','OAuth2');
$entry->UpdateString('username','my-account');
$tokens = new COM("Chilkat.JsonObject");
$loaded = $vault->GetSecretJson($entry,$tokens);
if ($loaded != 1) {
    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 COM("Chilkat.Http");
$http->RequireSslCertVerify = 1;
$http->AuthToken = $tokens->stringOf('access_token');
$resp = new COM("Chilkat.HttpResponse");

$campaignName = 'Workshop newsletter';
$campaignId = '';
$url = new COM("Chilkat.StringBuilder");
$url->Append('https://api.cc.email/v3/emails?limit=50');
$page = new COM("Chilkat.JsonObject");
while ((($url->Length > 0) and ($campaignId == ''))) {
    $success = $http->HttpNoBody('GET',$url->getAsString(),$resp);
    if (($success != 1) and ($resp->StatusCode <= 0)) {
        print 'Read campaigns: HTTP request failed.' . "\n";
        exit;
    }

    if (($success != 1) or ($resp->StatusCode != 200)) {
        print 'Read campaigns: HTTP ' . $resp->StatusCode . ' ' . $resp->BodyStr . "\n";
        exit;
    }

    $success = $page->Load($resp->BodyStr);
    $count = $page->SizeOfArray('campaigns');
    if (($success != 1) or ($count < 0)) {
        print 'Expected a page of campaigns.' . "\n";
        exit;
    }

    $i = 0;
    while ((($i < $count) and ($campaignId == ''))) {
        $page->I = $i;
        if ($page->StringOfEquals('campaigns[i].name',$campaignName,1) == 1) {
            $campaignId = $page->stringOf('campaigns[i].campaign_id');
        }

        $i = $i + 1;
    }

    $url->Clear();
    if (($campaignId == '') and ($page->JsonTypeOf('_links.next.href') == 1)) {
        $url->Append($page->stringOf('_links.next.href'));
        if ($url->StartsWith('/v3/emails?',1) != 1) {
            print 'Unexpected campaign pagination link.' . "\n";
            exit;
        }

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

}

if ($campaignId == '') {
    print 'No campaign named ' . $campaignName . "\n";
    exit;
}

$http->SetUrlVar('campaign',$campaignId);
$success = $http->HttpNoBody('GET','https://api.cc.email/v3/emails/{$campaign}',$resp);
if (($success != 1) and ($resp->StatusCode <= 0)) {
    print 'Read campaign: HTTP request failed.' . "\n";
    exit;
}

if (($success != 1) or ($resp->StatusCode != 200)) {
    print 'Read campaign: HTTP ' . $resp->StatusCode . ' ' . $resp->BodyStr . "\n";
    exit;
}

$campaign = new COM("Chilkat.JsonObject");
$success = $campaign->Load($resp->BodyStr);
if ($success != 1) {
    print 'Expected a JSON object in the response.' . "\n";
    exit;
}

//  The campaign identifies the newsletter. The primary activity identifies the email to test or schedule.
$primary = new COM("Chilkat.JsonObject");
$success = $campaign->FindRecord2('campaign_activities','role','primary_email',1,$primary);
if (($success != 1) or ($primary->JsonTypeOf('campaign_activity_id') != 1)) {
    print 'No primary email activity ID in the response.' . "\n";
    exit;
}

$selectedCampaign = $campaign->stringOf('campaign_id');
$selectedActivity = $primary->stringOf('campaign_activity_id');
if (($selectedCampaign == '') or ($selectedActivity == '')) {
    print 'Missing campaign or activity ID.' . "\n";
    exit;
}

print 'Campaign ID: ' . $selectedCampaign . "\n";
print 'Primary activity ID: ' . $selectedActivity . "\n";
$selection = new COM("Chilkat.JsonObject");
$selection->UpdateString('name',$campaign->stringOf('name'));
$selection->UpdateString('campaign_id',$selectedCampaign);
$selection->UpdateString('campaign_activity_id',$selectedActivity);
$success = $selection->WriteFile('newsletter-selection.json');
if ($success != 1) {
    print 'Could not save newsletter-selection.json. Keep the IDs printed above.' . "\n";
    exit;
}

print 'Saved newsletter-selection.json.' . "\n";

?>