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.

Adding contact PHP XML - Whats wrong?

Regular Participant

Adding contact PHP XML - Whats wrong?

Here is my code:



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

$post_string = '<?xml version="1.0" encoding="UTF-8"?>
<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/medlmobile/lists/50" />

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


Does not work. What am I missing?


Thanks in advance!


Regular Participant

any contibutions would be greatly appreciated. thank you.



Sorry for the delay in responding.  Two problems I see are that you need to authenticate to us somehow (if you use basic authentication, you would provide your credentials as a base64 encoded string consisting of APIKey%username:password).  For content type, you want to be providing "application/atom+xml".


I hope that helps!


Best Regards,

Shannon W.

API Support Specialist

Regular Participant

Thanks for the reply. 


Could you please give me an example of how to implement authentication using base64 encoded string?





It might be easier to use our PHP wrapper, since it handles the curl and authentication code (see authentication.php).  The wrapper is found here. This is much of the code it uses for basic authentication.  You would need to find a website that does base64 encoding on a string or use a library to base64 encode your APIKey%username:password string if you don't want to use the wrapper.


private function basic_construct($apiKey, $username, $password){
	$this->requestLogin = $apiKey.'%'.$username.':'.$password;
	$this->authType = 'basic';

public function makeRequest($url, $method, $body='', $contentType="application/atom+xml"){
	$curl = curl_init();

	if ($this->authType == 'basic'){
	   curl_setopt($curl, CURLOPT_USERPWD, $this->requestLogin);
	   curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
	} else if($this->authType == 'oAuth2') {
		//curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
	curl_setopt($curl, CURLOPT_URL, $url);
	//curl_setopt($curl, CURLOPT_FAILONERROR, 0);
	curl_setopt($curl, CURLOPT_HEADER, 0);
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);



Best Regards,

Shannon W.

API Support Specialist

The important lines for you in Shannon's example, if you are not planning on using the PHP library, are the following:


curl_setopt($curl, CURLOPT_USERPWD, $apiKey.'%'.$username.':'.$password);



Using this, it will automatically create a Basic authentication header for you that takes care of the Base64 encoding.  If you are lookign to do the Base64 encoding manually, we are unable to provide examples of doing that, but you can find much more information on this Wiki page: http://en.wikipedia.org/wiki/Basic_access_authentication

Dave Berard
Senior Product Manager, Constant Contact
Developer Portal

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

Visit Page