The Community is hosting an End of Summer sweepstakes! Participants must complete tasks to earn tickets that will enter them with a chance to win a free year of Constant Contact and other great prizes!*
*No Purchase Necessary. For Official Rules, visit here. Constant Contact’s End of Summer 2020 Sweepstakes ends on October, 20, 2020 at 11:50 PM EST.

Problem Adding New Contact to List

Highlighted
New Member

Problem Adding New Contact to List

 Hi All,


We are having a problem adding a new contact to one of our lists using the API. We are attempting to add the contact using PHP. The return value indicates that the addition of the new contact has been successful, however when we check our lists, the new contact does not show in our contact list. The PHP code that we are using is as follows:


 


<?php


$ccuser = 'xxx';


$ccpass = 'xxx';


$cckey  = 'xxx';


// these values are POSTED from the previous page


$firstName  = htmlspecialchars(trim($_REQUEST));


$lastName   = htmlspecialchars(trim($_REQUEST));  


$emailAddr  = htmlspecialchars(trim($_REQUEST));


$phone = htmlspecialchars(trim($_REQUEST));


$zip        = htmlspecialchars(trim($_REQUEST));


 


// represents the contact list identification number(s)


$contactListId = 1;


$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('HomePhone', $phone);


$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, 1); // 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: We are receiving a "1"


return(!is_numeric($response));


?>


 


Any help resolving this issue is greatly appreciated.

1 REPLY 1
Highlighted
Employee

I needed to make a few slight

I needed to make a few slight modifications to your code, since I do not have the actual form that submits to this page. After manually putting in a few test values, I was able to successfuly get your PHP code to work and create a new contact by adding the following line to your curl option:


 


curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);


 


This should allow you to successfully create your contact using your PHP code above. I hope this helps, please let me know if this does not resolve your issue.

David J

Developer Portal

View API documentation, code samples, get your API key.

Visit Page

Constant Contact 2020 End of Summer Community Sweepstakes!

The Constant Contact User Community is hosting a sweepstakes. The more you participate, the more chances you have to win! Read on to learn more...

Read More
Featured