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.


Thanks

 

 

<?php

include_once ('ctctWrapper1/ctctWrapper.php');

//Get values from the POST request

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

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

$emailAddress="a@a.com";

$contactListName="KCT_customers";

 

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", "----------------------");

 

 

createContact($emailAddress,$contactListName);

 

 

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");

    $rootElement->appendChild($nsAttribute);

 

    // create attribute value node

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

    $nsAttribute->appendChild($nsValue);

 

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

    $rootElement->appendChild($title);

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

 

    $title->appendChild($titleValue);

 

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

 

    $title->appendChild($titleTypeAttribute);

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

    $titleTypeAttribute->appendChild($titleTypeAttributeValue);

 

//Add when it was updated

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

    $rootElement->appendChild($updated);

 

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

    $updated->appendChild($updateTime);

 

//Add Author

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

    $rootElement->appendChild($author);

 

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

    $author->appendChild($authorName);

 

    //Add id

 

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

    $rootElement->appendChild($id);

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

    $id->appendChild($idValue);

 

    //Add Summary

 

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

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

    $summary->appendChild($summaryVal);

 

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

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

    $summaryAttr->appendChild($summaryAttrValue);

    $summary->appendChild($summaryAttr);

    $rootElement->appendChild($summary);

 

 

    //Add Content sub xml

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

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

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

    $contentAttr->appendChild($contentAttrValue);

    $content->appendChild($contentAttr);

 

    //Add Contact element

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

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

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

    $contactNameSpace->appendChild($contactNameSpaceValue);

    $contact->appendChild($contactNameSpace);

 

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

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

    $emailAddressNode->appendChild($emailAddressValue);

    $contact->appendChild($emailAddressNode);

 

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

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

    $OptInresource->appendChild($OptInresourceValue);

    

    $contact->appendChild($OptInresource);

    

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

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

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

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

    $contactListid->appendChild($contactListidValue);

    $contactList->appendChild($contactListid);

    $contactLists->appendChild($contactList);

    $contact->appendChild($contactLists);

 

 

    $content->appendChild($contact);

 

    $rootElement->appendChild($content);

    $xmlDoc->appendChild($rootElement);

 

    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);

    curl_close($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->setApiKey(KE_API);

    $utility->setLogin(KE_USERNAME);

    $utility->setPassword(KE_PASSWORD);

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

}

 

?>

3 REPLIES 3
Member

Hello,

 

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