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.

Adding Contact - php curl xml - help

Occasional Contributor

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.

 

<?php

$url = "https://api.constantcontact.com/ws/customers/myaccount/contacts";

$post_string = '<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>djswan@email.com</EmailAddress>
<OptInSource>ACTION_BY_CONTACT</OptInSource>
<ContactLists>
<ContactList id="http://api.constantcontact.com/ws/customers/myaccount/lists/25" />
</ContactLists>
</Contact>
</content>
</entry>';


$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);

if(curl_errno($ch))
print curl_error($ch);
else
curl_close($ch);


?>

 

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

 

 

2 REPLIES 2
Occasional Contributor

Re: Adding Contact - php curl xml - help

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

Moderator

Re: Adding Contact - php curl xml - help

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: http://www.github.com/constantcontact.  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