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.

Setting the ContactList Element using the PHP wrapper while adding new contact

ClausE
Participant

Setting the ContactList Element using the PHP wrapper while adding new contact

Hello! 

 

Just trying to setup a form to add a new contact  to my email list,  but I can't seem to figure out how to set the id in the ContactList elmenent. 

 

My code so far:

 

 


<?php
include('ccwrapper.php');
$_POST['formdata'] = "ri650@gmail.com";

 

//set up Contacts collection to add new contact

$collection  = new ContactsCollection();

 

//create a ListsCollection to grab the proper list to put the contact into

$lists = new ListsCollection();

//create a listObj to interact with individual lists

$listObj = new ListObj();

//get all the mailing lists

$total = $lists->getLists();

/*need to cycle through all the lists, if we find the right mailing list, extract the id.

 

foreach($total[0] as $key=>$row){

if($row->getName() == "General Interest"){

$id = $row->getId();

}

}

/*-------------
Get the value sent by the form.We're gonna assume JS validation is fine
---------------*/
$formdata = $_POST['formdata'];


//put into array

$formParams = array('email_address'=>$formdata,'id'=>$id);

 

//create a contact object$person = new Contact($formParams);

$person->setLists($id);

 

//add contact. Return code is used to check if request was successful or not

$return_code = $collection->createContact($person);

 

echo $return_code;if($return_code == 400){//spit out error messageecho "400";}else{//spit out successecho "true";}



anyone have some example code to share?


1 REPLY 1
Ryan_D
Employee

Hey Claus,

 

You can set the id manually and pass it through as an array.  Below would be a short example of how to do this.

 

<?php

	include('ctctWrapper.php');

     $contactInfo['email_address'] = "test@test.com";
     
     $contactInfo['lists'][] = "http://api.constantcontact.com/ws/customers/{username}/lists/1";
     
     $contact = new Contact($contactInfo);
     
     $contactCollect = new ContactsCollection();
     
     $addContact = $contactCollect->createContact($contact);
	
	print_r($addContact); // this will also return a 201 or a 400 depending if it was sucessful or if it failed.
	
	?>

 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