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.

Status code 400 on creating a contact

Occasional Participant

Status code 400 on creating a contact

I am trying to create a contact using the following XML using php.  Is there something wrong with my XML or PHP?


<entry xmlns="http://www.w3.org/2005/Atom">
<title type="text"> </title>
<author><name>Garment Graphics Website</name></author>
<summary type="text">Contact</summary>
<content type="application/vnd.ctct+xml">
<Contact xmlns="http://ws.constantcontact.com/ns/1.0/">
<ContactLists><ContactList id="http://api.constantcontact.com/ws/customers/Garmentgraphics/lists/43"/><ContactList id="http://api.constantcontact.com/ws/customers/Garmentgraphics/lists/45"/><ContactList id="http://api.constantcontact.com/ws/customers/Garmentgraphics/lists/46"/><ContactList id="http://api.constantcontact.com/ws/customers/Garmentgraphics/lists/50"/><ContactList id="http://api.constantcontact.com/ws/customers/Garmentgraphics/lists/48"/><ContactList id="http://api.constantcontact.com/ws/customers/Garmentgraphics/lists/7"/><ContactList id="http://api.constantcontact.com/ws/customers/Garmentgraphics/lists/47"/></ContactLists>


the following is my php code:


            $ch = curl_init($url);
            curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
            curl_setopt($ch, CURLOPT_USERPWD, PARAM_API_KEY . '%' . PARAM_USER_NAME . ':' . PARAM_PASSWORD);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/atom+xml'));
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            $xml = curl_exec($ch);
            $result = curl_getinfo($ch, CURLINFO_HTTP_CODE);


Hey Holly,


I took a look at your code and your XML seems ok but I do not know how the rest of your php looks.  Please take a look at my example and compare it to yours.



	// This is an example used to do a post to your
	// Constant Contact account
	// Declare Credentials
	$username = 'username';
	$password = 'password';
	$apiKey = "api-key";
	$apiPath = "https://api.constantcontact.com/ws/customers/";
	// This is where your will declare your URI
	$url = $apiPath . $username . "/contacts";
	// Store XML as a variable
	$xml ='<entry xmlns="http://www.w3.org/2005/Atom">
	  <title type="text"> </title>
	  <summary type="text">Contact</summary>
	  <content type="application/vnd.ctct+xml">
		<Contact xmlns="http://ws.constantcontact.com/ns/1.0/">
			<ContactList id="http://api.constantcontact.com/ws/customers/{username}/lists/1" />
	// Ths is where we connect to the server and tell it what we need to do	
	 $session = curl_init($url);
		 $usrpsw = $apiKey. '%' . $username .':'. $password;
		 curl_setopt($session, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
		 curl_setopt($session, CURLOPT_USERPWD, $usrpsw);
		 curl_setopt($session, CURLOPT_FOLLOWLOCATION ,1);
		 curl_setopt($session, CURLOPT_POST, 1);
		 curl_setopt($session, CURLOPT_POSTFIELDS , $xml);
		 curl_setopt($session, CURLOPT_HTTPHEADER, Array("Content-Type:application/atom+xml"));
		 curl_setopt($session, CURLOPT_HEADER, 0);
		 curl_setopt($session, CURLOPT_RETURNTRANSFER, 1);
	// this is now our response back from the server
	 $response = curl_exec($session);
	 // our curl
	 // This echo is just to show you the results of your curl in case there is an error just uncomment
	 // echo $response;


 Please let me know if you need any more help.



Ryan Davis
Quality Engineer
Occasional Participant

I replaced my curl code with yours, and although I didn't see a difference there, it now gets past that part anyway.  Althought, now the curl response is:


Error 400: Contact List id: 'http://api.constantcontact.com/ws/customers/Garmentgraphics/lists/34' does not exist


I have a list with that I set up in my control panel.


Thanks for your help

Hey Holly,


What appears to be happening is that the list does not exist on the server.  Use our rest client to do a get to your lists to verify that your list number is correct.



 Which will output the names and list number id's of all your lists.  Another way to do this is to hover over the link from inside of your account. 




My mouse is hovered over the test list which will give me the ?listID=11 in my URL.  That tells me that test has a listID of 11. 


Please let me know if this is the issue,

Ryan Davis
Quality Engineer
Occasional Participant

Ah thank you, I mistook the order column to be the list id

Occasional Participant

Is there a new method for obtaining the List ID now that the interface has changed?

Developer Portal

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

Visit Page