need help setting up API to add a contact in PHP

Regular Participant

need help setting up API to add a contact in PHP

Hi, I'm not a PHP developer by trade (ColdFusion actually) but my client needs this done in PHP.  Here's what I've done so far:


1.  I added the oAuth library from the google code website.

2. I have an index.html page (which has an e-mail address form for the user to enter their e-mail address form.

3. I have the oAuth code from David J's post in this forum on using oAuth to authenticate the site.

4.  When I tried the first time to submit an e-mail address, I got redirected to the constant contact authentication page (which is what I expected).  I entered my constant contact usernamd and password and submitted.  Afterwards, I got a page that simply said "an error occurred, please contact us.".


Does anyone have an implementation already done of PHP and oAuth?  I need to get this working today and without any error information at all on the screen I can't even begin to figure it out.  I'm going crazy!






Hi Dave,


I apologize for the delayed response over the weekend, but I am more than happy to assist you with this. Without seeing the PHP code that you have, it makes it difficult for me to begin troubleshooting what may be happening, but if the user is getting redirected to Constant Contact to authenticate, you must be pretty close to having the successfully working.


With that being said, I just saw the email you sent to our Webservices Team. I am taking a look at that now and will respond as soon as I have anymore information.

David J

Regular Participant

Hi David,


Any luck on this?  My client is getting antsy. :)  I'm stumped.





Dave J is out of the office today, but I did see that he sent you a response yesterday to the email you sent in.  Below is the response he sent over:


I think the issue here is that there are a few pieces of conflicting information in your actual request to create a contact. Fortunately, since this is the very last step in your process, a minimal amount of changes need to be made.

1. This line is setting up a request for a 'GET' (as seen by the 3rd argument passed to the from_consumer_and_token function:
$api_request = OAuthRequest::from_consumer_and_token($oauth_consumer, $authorization_token, 'GET', $webServiceUrl);

2. You are then passing in XML that would be used to create a new contact, and using the URL to create a new contact ({username}/contacts).

3. Finally, it looks as though you are using curl options to perform a 'PUT' (update):
    curl_setopt($session, CURLOPT_PUT, 1);
    curl_setopt($session, CURLOPT_CUSTOMREQUEST, "PUT");

In order to get this to work, you would want to use:
1) $api_request = OAuthRequest::from_consumer_and_token($oauth_consumer, $authorization_token, 'POST', $webServiceUrl);
2. Use the following curl_setopt: curl_setopt($session, CURLOPT_CUSTOMREQUEST, "POST");
3. Add the following curl option to specify the the content-type/content-length:
curl_setopt($session, CURLOPT_HTTPHEADER, Array("Content-Type:application/atom+xml", 'Content-Length: ' . strlen($parameter)));

Alternatively, I am in the final stages of testing a full PHP Wrapper library that uses oauth authentication. While it is still in beta and always subject to change before we release it, this would save you a lot of the processing you are doing, and having these actions like "createContact" already built in as functions. If you're interested in trying this out, please let me know and I'd be happy to send that over.

If you have any questions or concerns regarding this, please let me know and I will be happy to help. Thanks.

Dave Berard
Senior Product Manager, Constant Contact
Regular Participant

Thanks, for some reason I did NOT get that e-mail.  Please pass my thanks along to David and also yes, I'm interested in beta testing the wrapper for oAuth.





Regular Participant

Okay, I made the changes you recommended.  I still get the same error:


HTTP Status 401 - Invalid signature for signature method HMAC-SHA1


I appreciate your help!



Sorry for the confusion earlier. I will try sending an email outside of our ticketing system to see is that helps the email arrive in your inbox. There were a couple of other hiccup in the XML that were causing an issue with your contact being created, but this should prevent that.


If you have any questions please respond to this post or to the email that I'll be sending you and I'll be happy to help. Thanks.





$consumer_key = 'KEY'; // Constant Contact API key
$consumer_secret = 'SECRET'; // Constant Contact consumer secret

$access_token = 'ACCESS TOKEN'; //  Access Token
$access_token_secret = 'ACCESS TOKEN SECRET'; // Access Token Secret

$username = 'USERNAME'; // Constant Contact username - The account's resources are you attempting to access

$webServiceUrl = ''.$username.'/contacts';
$sig_method = new OAuthSignatureMethod_HMAC_SHA1();

    //Create API Request
    $oauth_consumer = new OAuthConsumer($consumer_key, $consumer_secret);
    $authorization_token = new OAuthToken($access_token, $access_token_secret);
    $api_request = OAuthRequest::from_consumer_and_token($oauth_consumer, $authorization_token, 'POST', $webServiceUrl);

    // Sign request
    $api_request->sign_request($sig_method, $oauth_consumer, $authorization_token);

    // Execute HTTP Request
    $request_url  = $api_request->to_url();
    $xml = '<entry xmlns="">
  <title type="text">Contact:</title>
  <author> </author>
  <content type="application/vnd.ctct+xml">
    <Contact xmlns="">
        <ContactList id="'

    $session = curl_init($request_url);
    curl_setopt($session, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($session, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($session, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($session, CURLOPT_HTTPHEADER, Array("Content-Type:application/atom+xml", 'Content-Length: ' . strlen($xml)));
    curl_setopt($session, CURLOPT_POSTFIELDS, $xml);

    $response = curl_exec($session);
    if (!$response) {
        $response = curl_error($session);
        echo $response;

    $error = curl_error($session);


David J

Developer Portal

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

Visit Page