cancel
Showing results for 
Search instead for 
Did you mean: 
Constant Contact wants to help you succeed! We’re celebrating our professional service programs on the Constant Contact Community this month and you have a chance to try one of the services for free! Learn more.

Adding contact PHP XML - Whats wrong?

Occasional Contributor

Adding contact PHP XML - Whats wrong?

Here is my code:

 

<?php

$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>
  <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>test_100@example.com</EmailAddress>
      <OptInSource>ACTION_BY_CONTACT</OptInSource>
      <ContactLists>
        <ContactList id="http://api.constantcontact.com/ws/customers/medlmobile/lists/50" />
      </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);

?>


Does not work. What am I missing?

 

Thanks in advance!

 

5 REPLIES 5
Occasional Contributor

Re: Adding contact PHP XML - Whats wrong?

any contibutions would be greatly appreciated. thank you.

CTCT Employee

Re: Adding contact PHP XML - Whats wrong?

Hi,

 

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

Occasional Contributor

Re: Adding contact PHP XML - Whats wrong?

Thanks for the reply. 

 

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

 

 

CTCT Employee

Re: Adding contact PHP XML - Whats wrong?

Hi,

 

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

Moderator

Re: Adding contact PHP XML - Whats wrong?

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

curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);

 

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