We all started somewhere! Share your experience on the Get Advice: Let's Get Started Sweepstakes thread and be entered to win a $100 credit on your Constant Contact account.

Newbie Help: Adding a contact, email only

JEFFREYC76
Regular Participant

Newbie Help: Adding a contact, email only

I am using PHP and the class provided and I can get an array of all my lists.  However I get an error when trying to add a new user.  Here is my code:

 

//create the CC objects
$ccListOBJ = new CC_List(); 
$ccContactOBJ = new CC_Contact();	
$postFields = array();
$postFields["email_address"] = $emailaddress; //test123@example.com
$postFields["lists"] = $theid; //http://api.constantcontact.com/ws/customers/[username]/lists/2
$contactXML = $ccContactOBJ->createContactXML(null,$postFields);	
if (!$ccContactOBJ->addSubscriber($contactXML)) 
	{
	echo "<br>NOT ADDED<br>";
	} 
else 
	{
	echo "<br>ADDED<br>";
	}	

 and the error I get back is: Invalid argument supplied for foreach() [filepath] on line 491.

 

Any thoughts?

3 REPLIES 3
Ryan_D
Employee

Hey Jeffrey,

 

I am unsure of which wrapper you are using but I suggest trying out our wrapper that is on GitHub.  Based on what your trying to do this would be the following code to do what you want with the other wrapper.

 

<?php 
	include ('wrapper/ConstantContact.php');
	
	$ConstantContact = new ConstantContact("basic", "apikey", "username", "password");

	//Get potential contact lists
	$lists = $ConstantContact->getLists();
	
	$contactObj = new Contact();
	$contactObj->lists = array($lists['lists'][0]->id, $lists['lists'][1]->id);
	$contactObj->emailAddress = 'test@example.com';
	
	// Create the contact
	$Contact = $ConstantContact->addContact($contactObj);

?>

 Just replace the information at the top with your information in the connection string.  You can find the documentation and the wrapper itself here.

 

If you don't want to use that wrapper please let me know which one you are using so I can trouble shoot this further for you.

 

 

Ryan Davis
Quality Engineer
JEFFREYC76
Regular Participant

I am using the PHP sample files on this page which includes a constant contact class.  I may try your method as it looks simpler but just for completeness and for anyone else searching can advise based on the PHP files based on this page (using the simple contact form): http://community.constantcontact.com/t5/Documentation/Code-Samples/ba-p/25019

 

I am glad that helped, please 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