4

I want to use API of the constant contact and want to insert user email using PHP while user register to the site.

please reply if any help.

Thanks in advance.

Avinash
  • 6,064
  • 15
  • 62
  • 95
  • Please clarify your question. I didn't get what do you want to do ;) – Vladislav Rastrusny Sep 23 '09 at 07:03
  • Constant Contact is providing service for newsletter management. I want to add the user to constant contact database when they register for the site. Means they will automatically add to the newsletter group when they register to the site. Thanks for taking interest in my question. – Avinash Sep 29 '09 at 06:39

3 Answers3

10
// fill in your Constant Contact login and API key
$ccuser = 'USERNAME_HERE';
$ccpass = 'PASS_HERE';
$cckey  = 'APIKEY_HERE';

// fill in these values 
$firstName  = "";
$lastName   = "";
$emailAddr  = "";
$zip        = "";

// represents the contact list identification number(s)
$contactListId = INTEGER_OR_ARRAY_OF_INTEGERS_HERE;
$contactListId = (!is_array($contactListId)) ? array($contactListId) : $contactListId;

$post = new SimpleXMLElement('<entry></entry>');
$post->addAttribute('xmlns', 'http://www.w3.org/2005/Atom');

$title = $post->addChild('title', "");
$title->addAttribute('type', 'text');

$post->addChild('updated', date('c'));
$post->addChild('author', "");
$post->addChild('id', 'data:,none');

$summary = $post->addChild('summary', 'Contact');
$summary->addAttribute('type', 'text');

$content = $post->addChild('content');
$content->addAttribute('type', 'application/vnd.ctct+xml');

$contact = $content->addChild('Contact');
$contact->addAttribute('xmlns', 'http://ws.constantcontact.com/ns/1.0/');

$contact->addChild('EmailAddress', $emailAddr);
$contact->addChild('FirstName', $firstName);
$contact->addChild('LastName', $lastName);
$contact->addChild('PostalCode', $zip);
$contact->addChild('OptInSource', 'ACTION_BY_CUSTOMER');

$contactlists = $contact->addChild('ContactLists');

// loop through each of the defined contact lists
foreach($contactListId AS $listId) {
    $contactlist = $contactlists->addChild('ContactList');
    $contactlist->addAttribute('id', 'http://api.constantcontact.com/ws/customers/' . $ccuser . '/lists/' . $listId);
}

$posturl = "https://api.constantcontact.com/ws/customers/{$ccuser}/contacts";
$authstr = $cckey . '%' . $ccuser . ':' . $ccpass;

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $posturl);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, $authstr);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post->asXML());
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type:application/atom+xml"));
curl_setopt($ch, CURLOPT_HEADER, false); // Do not return headers
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0); // If you set this to 0, it will take you to a page with the http response

$response = curl_exec($ch);

curl_close($ch);

// returns true on success, false on error
return (!is_numeric($response));
Corey Ballou
  • 42,389
  • 8
  • 62
  • 75
2

The developers of ConstantContact have launched PHP library to handle such kind of tasks.

Following version is for older PHP versions (5.2 or lesser). Download code from here: https://github.com/constantcontact/ctct_php_library

Following is an example to add a subscriber's email in your constant contact lists.

After downloading the above library, use following code to add an email in Constant Contact list(s).

session_start();
include_once('ConstantContact.php'); // Set path accordingly
$username = 'YOUR_CONSTANT_CONTACT_USER_NAME';
$apiKey = 'YOUR_API_KEY';
$password = 'YOUR_CONSTANT_CONTACT_PASSWORD';

$ConstantContact = new Constantcontact("basic", $apiKey, $username, $password);

$emailAddress = "new_email@test.com";

// Search for our new Email address
$search = $ConstantContact->searchContactsByEmail($emailAddress);

// If the search did not return a contact object
if($search == false){

    $Contact = new Contact();
    $Contact->emailAddress = $emailAddress;
    //$Contact->firstName = $firstName;
    //$Contact->lastName = $lastName;
    // represents the contact list identification link(s)
    $contactList = LINK_OR_ARRAY_OF_LINKS_HERE;
    // For example, 
    // "http://api.constantcontact.com/ws/customers/USER_NAME/lists/14"; 
    $Contact->lists = (!is_array($contactList)) ? array($contactList) : $contactList;


    $NewContact = $ConstantContact->addContact($Contact);
    if($NewContact){
        echo "Contact Added. This is your newly created contact's information<br /><pre>";
        print_r($NewContact);
        echo "</pre>";

    }
} else {
    echo "Contact already exist.";
}

Note: The latest version can be found on the GitHub links given in following URL: http://developer.constantcontact.com/libraries/sample-code.html But I am not sure if the above example code works with the newer library or not.

Waqas
  • 714
  • 5
  • 13
1

If you can't get this to work, you might have to add this curl_setopt:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
musefan
  • 47,875
  • 21
  • 135
  • 185
Andy Van Fossen
  • 136
  • 1
  • 3