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.

New to API, PHP check existingcontact

SOLVED
Go to solution
Highlighted
Occasional Contributor

New to API, PHP check existingcontact

I am using the PHP wrapper and have it successfully working with the code below.  However I am getting the "dreaded" 409 error back to the user if they already exist.

 

I need to add a check to this function to see if the user exists anywhere on constant contact, and I'm a bit stumped at how to do that.  Please advise?  Thanks!

 

//CONSTANT CONTACT: add user
function ccadduser($emailaddress)
{
	$ConstantContact = new ConstantContact("basic", "***key***", "***usr***", "***pwd***");
	$listtoadd = "General Interest"; //the default list
	$contactObj = new Contact();
	$contactObj->lists = array($listtoadd);		
	$contactObj->emailAddress = $emailaddress;	
	// Create the contact
	$Contact = $ConstantContact->addContact($contactObj);
	if($NewContact)	
		{ $result = "true"; }
	else
		{ $result = "false"; }
		
	return ($result);		
}

 

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Employee

Re: New to API, PHP check existingcontact

Hey Jeffrey,

 

Here is some code that I have wrote to help with preventing the 409 error. 

 

<?php 
/****************************************************************************************
 * 
 * 		Please note that this is just an example, please change the credentials to
 * 		your own constant contact login credentials.  The following code is an example of:
 * 
 * 		Search for contact by email address
 * 		Creating a new contact if it doesen't already exist
 * 		If Contact exists, then update the contact by adding it to a new list
 * 		
 ****************************************************************************************/

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

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

	// Get potential contact lists
	$lists = $ConstantContact->getLists();
	// Lists are returned in multidimentional arrays 0 being the list, and 1 being the next50
	
	// Email address here is used for testing purposes
	// NOTE:  This email address will be added to your account if you execute this script,
	//		  you should send this to do-not-mail after your testing
	$emailAddress = "rdavis@roving.com";
	
	// Search for our new Email address
	$search = $ConstantContact->searchContactsByEmail($emailAddress);
	
	// If the search didnt return a contact object
	if($search == false)
	{
		echo "Created new contact";
		// Create a new Contact Object to store data into
		$contactObj = new Contact();
		// Adding multiple lists to this new Contact Object
		$contactObj->lists = array($lists['lists'][0]->id, $lists['lists'][1]->id);
		// Set the email address
		$contactObj->emailAddress = $emailAddress;
		// Create the Contact and DONE
		$Contact = $ConstantContact->addContact($contactObj);
		
		echo $contactObj->emailaddress;
		
	} // Otherwise we update our existing
	else 
	{
		echo $contactObj->emailaddress . " was added to your new list";
		// Gather data from our previous search and store it into a data type
		$contactObj = $ConstantContact->getContactDetails($search[0]);
		// We need to get the old list and add a new list to it as
		// this request requires a PUT and will remove the lists
		// as they are stored in an array
	     array_push($contactObj->lists, $lists['lists'][3]->id );
		
		// Update the contact and DONE
		$UpdateContact = $ConstantContact->updateContact($contactObj);
	}
		

?>

 Of course you would want to change some of the information as far which list or how data is pushed out.  But the process is all there.

Ryan Davis
Quality Engineer

View solution in original post

4 REPLIES 4
Highlighted
Employee

Re: New to API, PHP check existingcontact

Hey Jeffrey,

 

Here is some code that I have wrote to help with preventing the 409 error. 

 

<?php 
/****************************************************************************************
 * 
 * 		Please note that this is just an example, please change the credentials to
 * 		your own constant contact login credentials.  The following code is an example of:
 * 
 * 		Search for contact by email address
 * 		Creating a new contact if it doesen't already exist
 * 		If Contact exists, then update the contact by adding it to a new list
 * 		
 ****************************************************************************************/

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

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

	// Get potential contact lists
	$lists = $ConstantContact->getLists();
	// Lists are returned in multidimentional arrays 0 being the list, and 1 being the next50
	
	// Email address here is used for testing purposes
	// NOTE:  This email address will be added to your account if you execute this script,
	//		  you should send this to do-not-mail after your testing
	$emailAddress = "rdavis@roving.com";
	
	// Search for our new Email address
	$search = $ConstantContact->searchContactsByEmail($emailAddress);
	
	// If the search didnt return a contact object
	if($search == false)
	{
		echo "Created new contact";
		// Create a new Contact Object to store data into
		$contactObj = new Contact();
		// Adding multiple lists to this new Contact Object
		$contactObj->lists = array($lists['lists'][0]->id, $lists['lists'][1]->id);
		// Set the email address
		$contactObj->emailAddress = $emailAddress;
		// Create the Contact and DONE
		$Contact = $ConstantContact->addContact($contactObj);
		
		echo $contactObj->emailaddress;
		
	} // Otherwise we update our existing
	else 
	{
		echo $contactObj->emailaddress . " was added to your new list";
		// Gather data from our previous search and store it into a data type
		$contactObj = $ConstantContact->getContactDetails($search[0]);
		// We need to get the old list and add a new list to it as
		// this request requires a PUT and will remove the lists
		// as they are stored in an array
	     array_push($contactObj->lists, $lists['lists'][3]->id );
		
		// Update the contact and DONE
		$UpdateContact = $ConstantContact->updateContact($contactObj);
	}
		

?>

 Of course you would want to change some of the information as far which list or how data is pushed out.  But the process is all there.

Ryan Davis
Quality Engineer

View solution in original post

Highlighted
Occasional Contributor

Re: New to API, PHP check existingcontact

This line:

 

$search = $ConstantContact->searchContactsByEmail($emailAddress);

 

 

was EXACTLY what I needed, thanks!

Highlighted
Established Member

Re: New to API, PHP check existingcontact

Hello, running stuck with the 'updateContact()' function not working.  I'm assuming you are simply pushing the list's id in the format;  'http://api.constantcontact.com/ws/customers/custname%40customerdomain.com/lists/3' into the $contactObj object's lists array correct?

 

EDIT!

 

I had:  

array_push($mfmin );
			

 and left out the array from the array_push method:  

array_push($contactObj->lists,$mfmin );
			

 woops!

Highlighted
Honored Contributor

Re: New to API, PHP check existingcontact

Hello Nate,

 

Great to see that you figured it out! If you do run into any other issues with updating contacts you can reply here. If you have issues with something else you can open a new forum topic or if you would prefer a non-public discussion you can email us at webservices@constantcontact.com.

 

Sincerely,

Elijah G.

Tier 2 Support

Elijah G.
API Support Engineer
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