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.

More Noobie Help: Adding user to specific list

Highlighted
Occasional Contributor

More Noobie Help: Adding user to specific list

Thanks to help in the community I am using the correct CC wrapper in PHP and can add a user to all my lists, howevewr I'm having a problem adding to a specific list.  I'm comparing a list name to a set cookie and using that to get the name of the desired list to add to.  That much is working. 

 

The problem I am having is trying to add to that specific list.  I have code to add to the first two lists, whatever they may be (you can see that commented out in the code below).  But I need to be able to add to my specifc list.  However when I try that I get warnings that let me know that the list isn't valid. 

 

Here is the code:

$ConstantContact = new ConstantContact("basic", "key", "usr", "pwd");
//Get potential contact lists
$lists = $ConstantContact->getLists();
$x = count($lists);
$listtoadd = "General Interest"; //the default list
//the FOR and IF loops sets $listtoadd to match a set cookie
for ($i=0;$i<$x;$i++)
	{		
	if ($lists['lists'][$i]->name == $_COOKIE['your-selected-location']) {$listtoadd = $lists['lists'][$i]->id;}
	}
$contactObj = new Contact();
//IF I DO THE NEXT LINE INSTEAD, IT ADDS TO BOTH LISTS CORRECTLY
// $contactObj->lists = array($lists['lists'][0]->id, $lists['lists'][1]->id); $contactObj->lists = $listtoadd; $contactObj->emailAddress = $emailaddress; // Create the contact $Contact = $ConstantContact->addContact($contactObj);

 

 

I did check that the $listtoadd var holds the following: http://api.constantcontact.com/ws/customers/[user]/lists/2

 

Any thoughts on what I'm doing wrong here?  I'm thinking it's something simple that I am missing.

 

While I'm at it, is there a graceful way to handle potential errors from: $Contact = $ConstantContact->addContact($contactObj);

I am doing some checking on the front end for proper email formation but dont want to show my end users a potential PHP warning or error.

 

Thanks!

Chris

2 REPLIES 2
Highlighted
Occasional Contributor

Re: More Noobie Help: Adding user to specific list

I finally got it.  I had this:

 

$contactObj->lists = $listtoadd;

I should have had this:

 

$contactObj->lists = array($listtoadd);

Highlighted
Employee

Re: More Noobie Help: Adding user to specific list

Hi Jeff,

 

Take a look at my code as it looks like you were not passing the list as an array.  It has to be an array as you can have multiple lists. 

 

//Get potential contact lists
$lists = $ConstantContact->getLists();
$x = count($lists);
$listtoadd = "General Interest"; //the default list
//the FOR and IF loops sets $listtoadd to match a set cookie

for ($i=0;$i<$x;$i++)
	{		
		
	if ($lists['lists'][$i]->name == $_COOKIE['your-selected-location']) 
		{
			echo $listtoadd = $lists['lists'][$i]->id;
		}
	}
$contactObj = new Contact();
$contactObj->lists = array($listtoadd);
$contactObj->emailAddress = $emailaddress;	

$Contact = $ConstantContact->addContact($contactObj);

 If you want to add multiple lists then you need to add it down in the array.  As far as a cleaner way to handle your errors you can always read up on throwing and catching exceptions in php.  You can read about them on php.net.

 

Let me know if you need anymore help.

Ryan Davis
Quality 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