New To CC, 400 error on php code

Highlighted
Occasional Participant

New To CC, 400 error on php code

Hi all, I'm new to constant contact and I got a client that wants a subscribe form on his site for his newsletter. He only wants something simple and easy to use for the visitors so we decided on a form that just asks for their name and email. I downloaded the php sample and i found a simple_form.php file that I used as a reference, i was able to use that form to add a new contact, i added my account info (email, password and API key) to the cc_class.php Now i was getting the 400 error on the simple_form.php sample but then i realized that i wasn't checking the list that i was being added to and once i checked the list "General Interests" I stopped getting the error. I did my own little form just to test and i'm getting the same error, my little test page code is below

 

 

<?php
header('Content-Type: text/html; charset=UTF-8');
include_once('cc_class.php');
$ccContactOBJ = new CC_Contact();
$ccListOBJ = new CC_List(); 
$ContactLists = $ccListOBJ->getLists();
//$ContactLists = $ConstantContact->getLists();
	if ($_POST['submit']){
		if ($_POST['email_address']!='' && $_POST['first_name']!=''){
			$postFields = array("email_address"=>$_POST["email_address"],
								"first_name"=>$_POST["first_name"],
								"mail_type"=>$_POST["mail_type"],
								"list"=>"General Interests"
								);
			$contactXML = $ccContactOBJ->createContactXML(null,$postFields);
			if (!$ccContactOBJ->addSubscriber($contactXML)) {
				$error = true;
			} else {
				$error = false;
				//$_POST = array();
			}
			if ($error){
				echo ' ERROR 1';
			} else {
				echo 'subscriber added';
			}
			
		} else {
			echo ' ERROR 2';
		}
	}
?>

 

a var_dump of $ccContactOBJ

 

 

object(CC_Contact)#1 (12) { 
["login"]=> string(19) "MYEMAIL" 
["password"]=> string(9) "MYPASSWORD" 
["apikey"]=> string(36) "MYAPIKEY" 
["contact_lists"]=> array(1) { 
[0]=> string(16) "General Interest" } 
["force_lists"]=> bool(false) 
["show_contact_lists"]=> bool(true) 
["actionBy"]=> string(18) "ACTION_BY_CUSTOMER" 
["curl_debug"]=> bool(true) 
["requestLogin"]=> string(66) "MYAPIANDEMAIL" 
["lastError"]=> string(126) "This contact already exists. Click here to edit the contact details." 
["apiPath"]=> string(64) "https://api.constantcontact.com/ws/customers/MYEMAIL" 
["doNotIncludeLists"]=> array(3) { 
[0]=> string(7) "Removed" 
[1]=> string(11) "Do Not Mail" 
[2]=> string(6) "Active" } }

 

 

Thank you in advance!

6 REPLIES 6
Highlighted
CTCT Employee

Re: New To CC, 400 error on php code

Hi,

 

The error that I see in your var_dump "This contact already exists. Click here to edit the contact details." indicates that you are trying to add an email address to the account that already existed at some point.  You would need to handle updating existing contacts differently than adding a new contact, as below.

 

<?php

session_start ();
require_once 'ConstantContact.php';
require_once 'config.php';

$ConstantContact = new ConstantContact("oauth2", $apiKey, $username, $accessToken);

// 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
$emailAddress = "testemail10312012@test.com";

// Search for our new Email address
$search = $ConstantContact->searchContactsByEmail($emailAddress);

// If the search didnt return a contact object
if($search == false)
{
	// 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 ("Contact added.");

} // Otherwise we update our existing
else 
{
	// 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'][1]->id );
	
	// Update the contact and DONE
	$UpdateContact = $ConstantContact->updateContact($contactObj);
	echo ("Contact updated.");
}	

?>

 

 

Best Regards,

Shannon W.

API Support Specialist

Highlighted
Occasional Participant

Hi Shannon, thank you for the reply! i get the same error...

Hi Shannon, thank you for the reply! i get the same error  even when adding a different email that was never used on that list before

 

 ["lastError"]=> string(17) "An Error Occurred"

Also i'm curious, do i have to specify a list and mail type? i just need a simple "Subscribe to news letter" that only needs the email address.

 

Thanks

Highlighted
CTCT Employee

Re: Hi Shannon, thank you for the reply! i get the same error...

Hi,

 

If the contact was ever in the Constant Contact account you are trying to add it to (on a different list, etc), you will get that duplicate error.  You don't get that error for a brand new email address, (e.g. testemail_11262012@test.com), do you?

 

You would need to provide a minimum of email address and the list it should belong to for our system to know how to add an email address (and where in the account to add it), but you shouldn't need to add a mail type.

 

Best Regards,

Shannon W.

API Support Specialist

Highlighted
Occasional Participant

HI Shannon, thank you again for the response. I do get...

HI Shannon, thank you again for the response.

 

I do get the same error when adding a brand new email address to the list.

 

Going back to this code

<?php
header('Content-Type: text/html; charset=UTF-8');
include_once('cc_class.php');
$ccContactOBJ = new CC_Contact();
$ccListOBJ = new CC_List(); 
$ContactLists = $ccListOBJ->getLists();
//$ContactLists = $ConstantContact->getLists();
	if ($_POST['submit']){
		if ($_POST['email_address']!=''){
			$postFields = array("email_address"=>$_POST["email_address"],
								"mail_type"=>$_POST["mail_type"],
								"list"=>"General Interests"
								);
			$contactXML = $ccContactOBJ->createContactXML(null,$postFields);
			if (!$ccContactOBJ->addSubscriber($contactXML)) {
				$error = true;
			} else {
				$error = false;
				//$_POST = array();
			}
			if ($error){
				echo ' ERROR 1';
			} else {
				echo 'subscriber added';
			}
			
		} else {
			echo ' ERROR 2';
		}
		
	}
?>

 And I forgot to clarify, i'm not doing any error checking yet i just want to be able to add a contact to understand what i'm doing here first.

 

If you see my $postFields array, for the "list" i used the name of the only list i have as the value, in this case "General Insterests".

I will only use 1 list for all my contacts, when i create the array with the values, should the "list" value be the name of the list or an unique ID for the list that comes when using getLists()?

 

In other words, what should that $postFields array look like if all i will be collecting is the email address?

 

thank you again for the help!

Highlighted
CTCT Employee

Re: HI Shannon, thank you again for the response. I do get...

Hi,

 

It took a while to test, so thanks for your patience.  This is what works for that wrapper library (the one that uses cc_class.php). You don't need a mail type, but you have to provide the lists parameter with a partial URL for the list you want to add contacts to, beginning with /ws and ending with the list ID.

 

<?php
header('Content-Type: text/html; charset=UTF-8');
include_once('cc_class.php');
$ccContactOBJ = new CC_Contact();
$ccListOBJ = new CC_List(); 
$ContactLists = $ccListOBJ->getLists();
//$ContactLists = $ConstantContact->getLists();
echo $_POST["email_address"];

if ($_POST['Submit']){
	if ($_POST['email_address']!=''){
		$postFields = array("email_address"=>$_POST["email_address"], 	
		"lists"=>array("/ws/customers/username/lists/1")  //replace username with your own
	);
																							
	$contactXML = $ccContactOBJ->createContactXML(null,$postFields);
	
	if (!$ccContactOBJ->addSubscriber($contactXML)) {
		$error = true;
	} else {
		$error = false;
		//$_POST = array();
	}
	
	if ($error){
		echo ' ERROR 1';
	} else {
		echo 'subscriber added';
	}
	
} else {
	echo ' ERROR 2';
}
	
}
?>

 

 

Best Regards,

Shannon W.

API Support Specialist

Highlighted
Occasional Participant

Re: HI Shannon, thank you again for the response. I do get...

Shannon you are my hero! i knew it had to do with the list not being present

 

Thank you so much!

Developer Portal

View API documentation, code samples, get your API key.

Visit Page

Constant Contact Would Like To Hear From You!

We want to hear from customers like you about your favorite features and how they have helped your business or organization. Tell us by answering a few questions in...

Read More
Featured