Adding Contact - php curl xml - help

Regular Participant

Adding Contact - php curl xml - help

I am trying to add a user to my constant contact list but I am not having any luck. Below is my code.



$url = "";

$post_string = '<entry xmlns="">
<title type="text"> </title>
<summary type="text">Contact</summary>
<content type="application/vnd.ctct+xml">
<Contact xmlns="">
<ContactList id="" />

$header = "POST HTTP/1.0 \r\n";
$header .= "Content-type: text/xml \r\n";
$header .= "Content-length: ".strlen($post_string)." \r\n";
$header .= "Content-transfer-encoding: text \r\n";
$header .= "Connection: close \r\n\r\n";
$header .= $post_string;

$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 4);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $header);

$data = curl_exec($ch);

print curl_error($ch);



No error mesages or anything, page is just blank and user is not added.



Regular Participant

Any help would be greatly appreciated. Thank you in advance.

I'm not sure why you aren't receiving an error message but that request should fail.  The Content-Type header must be set to application/atom+xml or you will receive a 415 Unsupported Media Type error.  Also, I've never seen a POST request done with a custom header, so I assume that you'd have to add the Authorization header in manually, but it appears you are not adding the header your Authentication.  Depending on if you are using OAuth 2.0 or Basic, it will look very different, but without that you would receive a 401 Unauthorized.  Again, I'm not sure why you wouldn't receive an error message if this was the case but the code snippet you have is clearly missing the Authorization header.  Your username for Basic would be "{api_key}%{username}" and the password component would be just your Constant Contact password.


If you're looking to simply create a Contact through PHP, it may be faster to use our PHP wrapper library that handles all of the XML parsing, Authentication and Curl management for you.  You can find this on our github page here:  We usually recommend using the library as it generally will save you from many of the headaches of creating your own Curl request and parsing XML.

Dave Berard
Senior Product Manager, Constant Contact
Developer Portal

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

Visit Page