cancel
Showing results for 
Search instead for 
Did you mean: 
Due to an issue with YouTube, inserting videos in your emails is showing an error. Until they are able to resolve this issue, we recommend using a different video hosting service. We will continue to monitor this outage and update you as we have more information.

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>
<updated>2011-07-25T20:06:25Z</updated>
<author><name>Garment Graphics Website</name></author>
<id>data:,none</id>
<summary type="text">Contact</summary>
<content type="application/vnd.ctct+xml">
<Contact xmlns="http://ws.constantcontact.com/ns/1.0/">
<EmailAddress>test@gmail.com</EmailAddress>
<OptInSource>ACTION_BY_CONTACT</OptInSource>
<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>
</Contact>
</content>
</entry>

 

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);
            curl_close($ch);

5 REPLIES 5
CTCT Employee

Re: Status code 400 on creating a contact

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.

 

<?php

	// 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>
	  <updated>2008-07-23T14:21:06.407Z</updated>
	  <author></author>
	  <id>data:,none</id>
	  <summary type="text">Contact</summary>
	  <content type="application/vnd.ctct+xml">
		<Contact xmlns="http://ws.constantcontact.com/ns/1.0/">
		  <EmailAddress>anyEmail@example.com</EmailAddress>
		  <OptInSource>ACTION_BY_CONTACT</OptInSource>
		  <ContactLists>
			<ContactList id="http://api.constantcontact.com/ws/customers/{username}/lists/1" />
		  </ContactLists>
		</Contact>
	  </content>
	</entry>';
	
	// 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
	 curl_close($session);
	 
	 
	 // 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.

 

Thanks,

Ryan Davis
Quality Engineer
Occasional Participant

Re: Status code 400 on creating a contact

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

Highlighted
CTCT Employee

Re: Status code 400 on creating a contact

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.

 

https://api.constantcontact.com/ws/customers/{username}/lists

 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. 

 

list-idnumber.JPG

 

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

Re: Status code 400 on creating a contact

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

Solution Provider

Re: Status code 400 on creating a contact

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