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.

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

Highlighted
Member

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
Highlighted
Employee

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

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

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