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.

Remove All Lists from Contact with PHP ctctWrapper

Highlighted
Occasional Participant

Remove All Lists from Contact with PHP ctctWrapper

I want to remove all the lists from a contact so that contact will show on the 'Removed' list using the PHP ctctWrapper.

 

What do I need to set for the lists value in the contact object to indicate no lists?

 

$Contact->lists = ?

 

Everything I have tried produces an error.

 

Or is there another method I should be using?

 

Steven

9 REPLIES 9
Highlighted
Employee

Re: Remove All Lists from Contact with PHP ctctWrapper

Hey Robbie,

 

You would want to remove the contact lists.   Which wrapper are you using so I can make you an example.

 

Ryan Davis
Quality Engineer
Highlighted
Occasional Participant

Re: Remove All Lists from Contact with PHP ctctWrapper

I am using the wrapper here...  https://github.com/ctctsupport/ctct_php_library.

 

The following is the code is use for my adds.

$contactObj = new Contact();
$contactObj->emailAddress = $row['username'];
$contactObj->firstName = $row['firstName'];
$contactObj->lastName = $row['lastName'];
$contactObj->customField1 = $row['localStore'];
$contactObj->customField2 = $row['verCode'];
$contactObj->customField3 = $row['id'];
$contactObj->lists = array("http://api.constantcontact.com/ws/customers/{username}/lists/17");
$Contact = $ConstantContact->addContact($contactObj);
if($Contact) {
	echo "Success.<br/>";
} else {
	echo "Failed.<br/>";
}

 The deleteContact function sends the user to the Do Not Mail list.  I just want the user removed from all lists.

 

Thanks for any help you can provide.

 

 

Highlighted
Occasional Participant

Re: Remove All Lists from Contact with PHP ctctWrapper

Please provide an example.  Thank you.

Highlighted
Employee

Re: Remove All Lists from Contact with PHP ctctWrapper

Hey Robbie,

 

Here is what I would do to send a contact to a removed status using that wrapper:

 

<?php 
/****************************************************************************************
 * 
 * Author: Ryan Davis
 * This sample form is to search for a contact and set it to removed status.  
 * 		
 ****************************************************************************************/

// this application uses our new wrapper that can be found on github
// https://github.com/ctctsupport/ctct_php_library

include ('ConstantContact.php');
	// Connecting to your account
	$ConstantContact = new ConstantContact("basic", "apikey", "username", "password");

	// Get potential contact lists
	$lists = $ConstantContact->getLists(null, true);
	// Lists are returned in multidimentional arrays 0 being the list, and 1 being the next50
	// null is the page number, while true shows system lists, we need these to send to removed status.
	
	
	// Email address here is used for testing purposes
	$emailAddress = "test@test.com"; // Email address to be set to removed status
	
	// Search for our new Email address
	$search = $ConstantContact->searchContactsByEmail($emailAddress);
	
	// If the search didnt return a contact object
	if($search == false)
	{
		echo "This contact never existed, please enter a new email address.";
		
	} 
	else 
	{
		echo "you are already subscribed";
		// Gather data from our previous search and store it into a data type
		$contactObj = $ConstantContact->getContactDetails($search[0]);
		// We need to remove all the lists and add the removed list.  This will change the status. 
		// this request requires a PUT and will remove the lists
		$contactObj->lists = $lists['lists'][2]->id; // [2] was my list. 0 was active, 1 being DNM, 2 removed.
		// You can get this number by doing a print_r($lists);
		
		// Update the contact and DONE
		 $UpdateContact = $ConstantContact->updateContact($contactObj);
		 echo $contactObj->emailAddress . " Was removed from your account";
	}
		

?>

 Please let me know if this helps.

Ryan Davis
Quality Engineer
Highlighted
Occasional Participant

Re: Remove All Lists from Contact with PHP ctctWrapper

Thank you for the code. It helps me understand the process; however, I am still getting an error.

 

I have verified that the value of $lists['lists'][2]->id is http://api.constantcontact.com/ws/customers/{username}/lists/removed in the printed array.  Even putting in the value directly like

 

$Contact->lists = array("http://api.constantcontact.com/ws/customers/{username}/lists/removed");

 

I continue to get the following error:

 

Constant Contact HTTP Request Exception: Error 400: Contact List http://api.constantcontact.com/ws/customers/{username}/lists/removed does not exist.

 

Please help.  If necessary, I will post the entire script.

 

Thanks.

 

 

Highlighted
Employee

Re: Remove All Lists from Contact with PHP ctctWrapper

Hey Robbie,

 

it is looking for a list object, the string is not going to compile the rest of the data that is inside of that object.  That is why it is necessary to do the code above.

Ryan Davis
Quality Engineer
Highlighted
Occasional Participant

Re: Remove All Lists from Contact with PHP ctctWrapper

Ok, I am getting a successful removal now. :smileyhappy:  However, there are still a couple warnings returned when processing a removal (see below).  Please explain how I can avoid these warnings being returned.  I really appreciate all your help.  Thanks.

 

 

RESULT:

Syncing user profiles to Constant Contact.

scrombie@farmersfurniture.com
Removing...scrombie@farmersfurniture.com

Warning: Invalid argument supplied for foreach() in /var/www/fhfadmin/ConstantContact/Components.php on line 313

Warning: Invalid argument supplied for foreach() in /var/www/fhfadmin/ConstantContact/Components.php on line 313
Success.

 

CODE:

<?php

include_once "../common.php";
include_once $remoteDbLive;
include_once "ConstantContact.php";

//-------------------------------------------------
// CONSTANT CONTACT
echo "Syncing user profiles to Constant Contact.<br/><br/>";

// Users to Update
/*
$qry = sprintf("SELECT 'update' as action, sync, username, firstName, lastName, zipcode, localStore, verCode, id, maillist, deleted, doNotMail FROM `db`.`tbUserProfiles` 
				where sync > 0 
				and active = 1
				and maillist = 1
				and deleted = 0
				and donotMail = 0
				limit 10
				UNION
				SELECT 'remove' as action, sync, username, firstName, lastName, zipcode, localStore, verCode, id, maillist, deleted, doNotMail FROM `db`.`tbUserProfiles` 
				where sync > 0 
				and deleted = 1
				and donotMail = 0
				UNION
				SELECT 'delete' as action, sync, username, firstName, lastName, zipcode, localStore, verCode, id, maillist, deleted, doNotMail FROM `db`.`tbUserProfiles` 
				where sync > 0 
				and donotMail = 1;");
*/
// Test delete query
$qry = sprintf("SELECT 'remove' as action, sync, username, firstName, lastName, zipcode, localStore, verCode, id, maillist, deleted, doNotMail FROM `db`.`tbUserProfiles` 
				where sync > 0 
				and deleted = 1
				and donotMail = 0;");
$result = mysql_query($qry);
if (!$result) {
	$message = "Query failed.";
	echo $message;
}
$userCount = mysql_num_rows($result);


if ($userCount) {
	$ConstantContact = new ConstantContact('basic', $CC_apikey, $CC_username, $CC_password);
	
	while ($row = mysql_fetch_assoc($result)) {
		echo $row['username']."<br/>";
		$action = $row['action'];
		$sync = $row['sync'];
		switch ($action) {
			case "update":
				$search = $ConstantContact->searchContactsByEmail($row['username']);
				echo "Search: ".$search[0]->status."<br/>";
				if(!$search) {
					// Create
					echo "  Adding...";
					$contactObj = new Contact();
					$contactObj->emailAddress = $row['username'];
					$contactObj->firstName = $row['firstName'];
					$contactObj->lastName = $row['lastName'];
					$contactObj->customField1 = $row['localStore'];
					$contactObj->customField2 = $row['verCode'];
					$contactObj->customField3 = $row['id'];
					//$lists = $ConstantContact->getLists();
					//$contactObj->lists = array($lists['lists'][0]->id, $lists['lists'][1]->id);
					$contactObj->lists = array("http://api.constantcontact.com/ws/customers/{username}/lists/17");
					$Contact = $ConstantContact->addContact($contactObj);
					if($Contact) {
						echo "Success.<br/>";
					} else {
						echo "Failed.<br/>";
					}
				} else {
					// Edit
					echo "  Editing...";
					$Contact = $ConstantContact->getContactDetails($search[0]);
					$Contact->emailAddress = $row['username'];
					$Contact->firstName = $row['firstName'];
					$Contact->lastName = $row['lastName'];
					$Contact->customField1 = $row['localStore'];
					$Contact->customField2 = $row['verCode'];
					$Contact->customField3 = $row['id'];
					// All active lists must be included in array
					$Contact->lists = array("http://api.constantcontact.com/ws/customers/{username}/lists/17");
					$Contact = $ConstantContact->updateContact($Contact);
					if($Contact) {
						echo "Success.<br/>";
					} else {
						echo "Failed.<br/>";
					}
				}
				break;
			case "remove":
				// Remove
					echo "  Removing...".$row['username']."<br/>";
					$lists = $ConstantContact->getLists(null, true);
					$search = $ConstantContact->searchContactsByEmail($row['username']);
					if($search == false) {
						echo "Contact does not exist.";
					} else {
						$Contact = $ConstantContact->getContactDetails($search[0]);
						echo "<br/>".$lists['lists'][2]->id."<br/>";
						$Contact->lists = $lists['lists'][2]->id;
						$UpdateContact = $ConstantContact->updateContact($Contact);
						if($UpdateContact) {
							echo "Success.<br/>";
						} else {
							echo "Failed.<br/>";
						}
					}
				break;
			case "delete":
				// DoNotMail  (confirmed deleteContact moves to Do Not Mail list)
				echo "  Deleting...";
				$search = $ConstantContact->searchContactsByEmail($row['username']);
				$delete = $ConstantContact->deleteContact($search[0]);
				if($delete) {
					echo "Success.<br/>";
				} else {
					echo "Failed.<br/>";
				}
				break;
		}
	}
}
?>

 

Highlighted
Employee

Re: Remove All Lists from Contact with PHP ctctWrapper

Hey,

 

The warning is coming from the way the LIST is passed in.  Its not supposed to work in this manner as your supposed to remove the ContactLists node to remove the contacts but by passing this information into our wrapper it will result in a successful removal and those warnings are part of that process. 

 

 foreach($this->lists as $list){
            $listNode = $contactlists_node->addChild("ContactList");
            $listNode->addAttribute("id", $list);                  
        }

 Where $lists are:

$Contact->lists = $lists['lists'][2]->id;

What makes me curious as why you are getting the warning twice.  It makes me believe that it is holding two lists. 

 

Ryan Davis
Quality Engineer
Highlighted
Frequent Visitor

Re: Remove All Lists from Contact with PHP ctctWrapper

All this code example does is put an invalid string value in the lists property so that it passes the wrapper's validation routine.

The better answer is to change the contact object's createXml function to not validate the lists property:

 

        $this->validate(array('emailAddress'));
instead of:

        $this->validate(array('emailAddress','lists'));

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