Existing Constant contact Add contact functionality broken

Regular Participant

Existing Constant contact Add contact functionality broken

Hello Guys,

  We used to have a lightbox based email catch solution that used to work until sometimes ago. I learned that there is a new Authentication Mechanism that CC implemented, which apparently broke our existing solution. I tried to fix it, but I think I'm missing something. I'm posting my entire script here, with credentials removed, I was wondering if I can get some input on where to begin.

I tried to follow the API documentation, but I'm at loss as to where to begin.





include_once ('ctctWrapper1/ctctWrapper.php');

//Get values from the POST request

// $emailAddress = $_POST["emailAddress"];

// $contactListName = $_POST["contactListName"];




define("KE_API", "------------------------");

define("KE_USERNAME", "-------");

define("KE_PASSWORD", "-------");

define("KE_SECRET", "----------------------");

define("KE_NAMESPACE", "http://www.w3.org/2012/Atom");

define("KE_LISTURI", "https://api.constantcontact.com/ws/customers/------/lists");

define("KE_CONTACTLISTNAME", "KCT_customers");

define("KE_POST", "POST");

define("KE_CONTACTURI", "https://api.constantcontact.com/ws/customers/--------/contacts/--------");

define("KE_REQUESTHEADER", "Content-type:application/atom+xml");

define("KE_ACCESSTOKEN", "----------------------");






function createContact($emailAddress, $contactListName) {

    executeWebRequest(KE_POST, createMessageBody($emailAddress,$contactListName), KE_CONTACTURI);



function createMessageBody($emailAddress, $contactListName) {

    $xmlDoc = new DOMDocument();


    //Create the root element

    $rootElement = $xmlDoc->createElement("entry");


    $nsAttribute = $xmlDoc->createAttribute("xmlns");



    // create attribute value node

    $nsValue = $xmlDoc->createTextNode("http://www.w3.org/2005/Atom");



    $title = $xmlDoc->createElement("title");


    $titleValue = $xmlDoc->createTextNode(" ");




    $titleTypeAttribute = $xmlDoc->createAttribute("type");



    $titleTypeAttributeValue = $xmlDoc->createTextNode("text");



//Add when it was updated

    $updated = $xmlDoc->createElement("updated");



    $updateTime = $xmlDoc->createTextNode(getCurrentTimeStamp());



//Add Author

    $author = $xmlDoc->createElement("author");



    $authorName = $xmlDoc->createTextNode(" ");



    //Add id


    $id = $xmlDoc->createElement("id");


    $idValue = $xmlDoc->createTextNode("data:,none");



    //Add Summary


    $summary = $xmlDoc->createElement("summary");

    $summaryVal = $xmlDoc->createTextNode("Contact");



    $summaryAttr = $xmlDoc->createAttribute("type");

    $summaryAttrValue = $xmlDoc->createTextNode("text");






    //Add Content sub xml

    $content = $xmlDoc->createElement("content");

    $contentAttr = $xmlDoc->createAttribute("type");

    $contentAttrValue = $xmlDoc->createTextNode("application/vnd.ctct+xml");




    //Add Contact element

    $contact = $xmlDoc->createElement("Contact");

    $contactNameSpace = $xmlDoc->createAttribute("xmlns");

    $contactNameSpaceValue = $xmlDoc->createTextNode("http://ws.constantcontact.com/ns/1.0/");




    $emailAddressNode = $xmlDoc->createElement("EmailAddress");

    $emailAddressValue = $xmlDoc->createTextNode($emailAddress);




    $OptInresource = $xmlDoc->createElement("OptInSource");

    $OptInresourceValue = $xmlDoc->createTextNode("ACTION_BY_CUSTOMER");





    $contactLists = $xmlDoc->createElement("ContactLists");

    $contactList = $xmlDoc->createElement("ContactList");

    $contactListid = $xmlDoc->createAttribute("id");

    $contactListidValue = $xmlDoc->createTextNode(getContactListUri($contactListName));












    return $xmlDoc->saveXML();



function getCurrentTimeStamp() {

    return date('m/d/Y h:i:s a', time());



function getContactListUri($contactListName) {

    $entries = new SimpleXMLElement(executeWebRequest("GET", "", KE_LISTURI));


    foreach ($entries as $entry) { // lool through the entries

        if ($entry->title == $contactListName) {

            return $entry->id;





function executeWebRequest($verb, $messageBody, $url) {


    $web_request = curl_init();


    curl_setopt($web_request, CURLOPT_URL, $url);


    curl_setopt($web_request, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);

    curl_setopt($web_request, CURLOPT_USERPWD, getUserNamePassword());

    curl_setopt($web_request, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");

    curl_setopt($web_request, CURLOPT_RETURNTRANSFER, 1);


    if ($verb == "GET") {

        curl_setopt($web_request, CURLOPT_HTTPGET, true);

    } else if ($verb == "POST") {

        curl_setopt($web_request, CURLOPT_POST, true);

        curl_setopt($web_request, CURLOPT_HTTPHEADER, array('Content-type:application/atom+xml'));

        ///Set Curl OPT POST



    if ($messageBody != NULL) {

        curl_setopt($web_request, CURLOPT_POSTFIELDS, $messageBody);



    $result = curl_exec($web_request);



    return $result;



function getUserNamePassword() {

    $username = KE_API;

    $username .= '%';

    $username .= KE_USERNAME;

    $username .= ':';

    $username .= KE_PASSWORD;


    return $username;



function setupSession() {

    $utility = new Utility();




    $utility->setRequestLogin($utility->getApiKey() . "%" . $utility->getLogin() . ":" . $utility->getPassword());







You're using a previous wrapper version that we no longer support, so I would suggest updating your application to the new wrapper when you can.  It appears that your  script is using basic authentication, however, which we still have implemented, so I don't think authentication is the issue.


Is an error being returned when your script doesn't  work for a given email address?  Can you direct message me one of the email address for which this script has failed?  It lookslike your script calls only a create contact function. As contacts can be unique  only at the account level in our system, if a contact exists on any status of list in your account already, you can't use a create contact (POST) function to add it to a list, and need to use an update (PUT) method request instead.  Could that explain the issue  you're seeing?

Mark Coleman
Support Engineer
Regular Participant

Thanks for the reply Mark.

Where can I find the new Wrapper library?


Also,  the error message is in the SimpleXML parser, because it cannot parse the return value from the getLists API.


I'm gonna send you the exact error message in a bit. Is there an email address I can email to?

In the current scenario, the contacts are unique no matter what. Thats the assumption we are making, if the contact is not unique, I'm expecting the CTCT api to fail which is acceptable.



The new wrapper library can be downloaded from our repository, along with a wiki that includes implementation guidance. It incorporated  OAuth 2.0 authentication, which we encourage  you to implement, because we will be phasing out basic authentication, and moving increasingly to OAuth 2.0.


You can email me the exact error response here. It would also be helpful if you could dump the raw response from our API and attach that in a text file as well.

Mark Coleman
Support Engineer
Developer Portal

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

Visit Page