PHP API wrapper help

Occasional Participant

PHP API wrapper help

So basically, im trying to write code using a php wrapper for constant contact to have a user, who is already subscribed or not, change the mailing list he is in via radio buttons. A simple choice A or choice B. Then an update button that switches the user from list A to B or vice versa. On the same page the user has the choice of unsubscribing.



my code is this so far. please ignore the geoTags as it is an inhouse html library.


include "includes/top_new.php";
include "ConstantContact/ConstantContact.php";

$option1 = $_POST['unsubEmail']; //unsubscribe
$option2 = $_POST['submitEmail']; //update the subscription
$list = $_POST['group1']; //1=default 2=less frequent

$username = "correctINFO";
$password = "correctINFO";
$apiKey = "correctINFO";
$consumerSecret = "correctINFO";
$Datastore = new CTCTDataStore();
$DatastoreUser = true;
$name = "normal"; //default list to be added to
$name2 = "lessFrequent"; //less frequent list


$ConstantContact = new ConstantContact('basic', $apiKey, $username, $password);
$ContactLists = $ConstantContact->getLists();
$Contact -> emailAddress = $_SESSION['cust_email'];

$email = $_SESSION['cust_email'];

//TODO -- error if the user has opted out

//check to see if it exists
$exists = $ConstantContact->searchContactsByEmail($email);

//if they want to unsubscribe
if (strcmp($option1,"Unsubscribe") == 0){

//and the email exists
if ($exists){
//delete them
$text = '<p>Your email '.$email.' has been successfully removed from our contact list.</p>';
//or if the email doesnt exist
$text = '<p>Your email '.$email.' doesn\'t appear to be on our list</p>';
//otherwise figure out what list and add them to it
elseif(strcmp($option2,"Update") == 0){

//the user wants to be in the default mailing
if($list == 1){
//echo 'in default';
$Contact = new Contact();
$Contact -> emailAddress = $_SESSION['cust_email'];

foreach($ContactLists['lists'] as $list) {
if ($list->name == $name)
$Contact->lists = array($list->id);

$text = '<p>Your email '.$email.' has been successfully added to our Normal emailing list.</p>';

//the user wants to be in the less frequent mailing list
elseif($list == 2){
//echo 'in less frequent';
$Contact = new Contact();
$Contact -> emailAddress = $_SESSION['cust_email'];

foreach($ContactLists['lists'] as $list) {
if ($list->name == $name2)
$Contact->lists = array($list->id);


$text = '<p>Your email '.$email.' has been successfully added to our Less Frequent emailing list.</p>';
$text= '<p>A bad list was selected. Please go back and try again</p>';
$head -> setTitle("Changes Successful");


//set up the window size the way we want it
$text = div($text,NULL,NULL,'width:1050px; margin:auto; clear:both; overflow:auto;');
$page = div($top.div($text,'main').$footer); //put the header on top, the text in the body, and the footer below

//set the title and echo the page on screen

echo geoHtml(geoBody($page),$head);



In general, your flow appears reasonable, but your method for creating a contact object to use to update list membership attributes of a given contact won't work, because when you create a new contact object, the object will not contain the contact id url.  Because to udpate a contact, you must include not  only the email address, but the contact id in the contact object, and you will need to make sure all the requires elements are indcluded in your request, the recommended flow goes like this:


1) Check if contact exists using $ConstantContact->searchContactsByEmail($email)

2) If exists, get details for the contact using getContactDetails($Contact) using the limited contact object returned in step 1 to request full detials for the contact, which will include all required elements for the update request.

3) Modfy the lists in the contact object and submit your update request using updateContact($Contact)


I hope this helps you out. If you modify your code and run into difficulties with it on execution, please add breakpoints to your code to identify where the issue occurs, if you don't get an informative error message, and echo or var_dump any variables bieng used at that point in the code. If a status code is echoed to browser that indicates the API request was unsuccessful for some reason other than authentication problems,  you can also add some code to the makeRequest function in Authentication.php to dump the XML being submitted. Comparing the XML to our API requirements specified in the Constant Contact API Documentation for Contacts (see Contacts Collection and Resource documentation and Updating Contact Information, in particular).



Mark Coleman
Support Engineer
Occasional Participant

I'll have to check this out (I'm currently doing other projects too).


I REALLY appreciate your response, if I run into another bug I'll post to here.


In the meantime please keep this post as "unsolved" or however you categorize it.



Oh, also.


Why is it the case that deleteContact($Contact) moves the subscriber to the do not mail list?


And what is the accepted way of checking for such an event as try{}catch{} blocks don't seem to work the same with the PHP wrapper API?

In our system, contacts are associated with the 'account' they are added to rather than a spcific list belonging to an account. List membership is specified as an attribute of associated with the contact, rather than vice versa.  Strictly speaking, Do Not Mail, Removed, and Active are not lists attributes, but are "Status" attributes of a contact. If a contact opts out, they go on the Do Not Mail list, so we can enforce the contact's wishes on whether to recieve or not recieve your mailings. By convention, we use a DELETE method HTTP request to our API to "Unsubscribe" contacts. Simply "Removing" contacts from your active list, while retaining the ability to add them to lists again at some point in the future for the purpose of managing contacts and lists, is done instead by updating the contact and simply including no lists in the contact object.  These statuses are not directly set by editing them in the contact object. Our system assigns the status. Active status is assigned when a contacts is added to an active maling list in an account.


I don't understand your second question. Unsubscribing is a request by the contact that you no longer email them, and our system is designed to enforce that request until the contact initiates an active relationship with you. If you want to have a different option to allow people to remove themselves from your regular mailings, but remain available for you to send only occassional emails to, then you should have a separate "active" list in your account for only those who want occassional emails, but don't want the regular (frequenty or infrequent) emails, or you should update the contact to remove them from all active lists, but don't delete them.


Make sure if you do this, however, that you are clear on your form that the contact will not recieve the regular emails, but may be occassionally contacted for other purposes, so it is not confused with a complete unusubscribe from all your mailings. If there is confusion, this may cause SPAM complaints.

Mark Coleman
Support Engineer
Developer Portal

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

Visit Page