While our SDK doesn't have a service directly for adding bulk contacts, this can be done. Please take a look at the following code to see how to create an array of contacts, assign them column names, and use the addCreateContactsActivity. <!DOCTYPE HTML>
<html>
<head>
<title>Constant Contact API v2 Bulk Add Contacts Sample Code</title>
</head>
<body>
<pre>
<?php
// --- Setup ---
// require the autoloader
require_once 'php-sdk-master/src/Ctct/autoload.php';
// Set up our use statements for the components we are using
use Ctct\ConstantContact;
use Ctct\Components\Contacts\CustomField;
use Ctct\Components\Contacts\Contact;
use Ctct\Components\Contacts\Address;
use Ctct\Exceptions\CtctException;
use Ctct\Components\Activities\AddContacts;
use Ctct\Components\Activities\AddContactsImportData;
// Enter your Constant Contact APIKEY and ACCESS_TOKEN
define("APIKEY", "API_KEY");
define("ACCESS_TOKEN", "ACCESS_TOKEN");
// Create an instant of the Constant Contact Class
$cc = new ConstantContact(APIKEY);
// --NEW--
// Define an array of contacts to import
$contacts = Array();
try {
// --- Defining a contact to import ---
// Create an instance of the AddContactsImportData class
$contact = new AddContactsImportData();
// Next we will populate this with data. For this sample I am defining a value for each of the 15 custom fields and an email address.
// For more information about what fields you can set on this class, you can review src/Ctct/Components/Activities/AddContactsImportData.php
// This example only loads a single contact, but you could loop over the code to define multiple contacts.
$contact->addEmail("email_testing1@example.com");
$contact->addCustomField(CustomField::create(array("name"=>"Custom Field 1","value"=>"CF1")));
// --NEW--
// Add the contact we just created to the array of contacts.
$contacts[] = $contact;
// Once we have our contact data, we need to construct an array of column names. These can be found in the documentation for
// bulk uploads here: http://developer.constantcontact.com/docs/bulk_activities_api/bulks-activities-import-contacts-api.html
$columns = Array(
"EMAIL",
"CUSTOM FIELD 1"
);
// Next you need to define an array of strings that contains the IDs of each list you wish to add these contacts to.
$lists = Array(
"LIST_ID"
);
// The last step before we send it to Constant Contact is to create an instance of the AddContacts wrapper class to contain all of the information about this import.
$contactImport = new AddContacts($contacts,$lists);
$contactImport->column_names = $columns;
print_r(json_encode($contactImport));
// Finally, you can send the import to Constant Contact.
$cc->addCreateContactsActivity(ACCESS_TOKEN,$contactImport);
echo "Activity Created!";
} catch (CtctException $e) {
echo "An Error was encoutered: " . print_r($e->getErrors(),true);
}
?>
</pre>
</body>
</html>
... View more