The Community is hosting an End of Summer sweepstakes! Participants must complete tasks to earn tickets that will enter them with a chance to win a free year of Constant Contact and other great prizes!*
*No Purchase Necessary. For Official Rules, visit here. Constant Contact’s End of Summer 2020 Sweepstakes ends on October, 20, 2020 at 11:50 PM EST.

Error 401 Using Contact.php - tried http 2 https in request - No Difference

Highlighted
Occasional Participant

Error 401 Using Contact.php - tried http 2 https in request - No Difference

I created my own form based on my clients needs with just First Name, Last Name & Email Address and want this going to CC acct, I have checked php examples here on the boards and have used the following based on examples and still getting a 401 Authentication Error


I have XXX'd out the obvious user, pass, key.  Although I know the user, pass, and key are accurate, could it be that one of them is really not authenticating?? 


 


I am no expert by any means but I have tried every variation I have seen in forums with this error:


 


$request = "http://api.constantcontact.com/ws/customers/" . $UN . "/contacts";


$request = "https://api.constantcontact.com/ws/customers/" . $UN . "/contacts";


curl_setopt($session, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);


curl_setopt($session, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);


None work!!!!!Same 401 Error


 


 


Here is current code:


 


<?php




$first_name = $_POST;

$last_name = $_POST;

$email = $_POST;

$UN = "dpxxxxx";

$PW = "d2xxxxx";

$Key = "4fxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";


$date = date("Y-m-d\TH:i:s");


$entry = '<entry xmlns="http://www.w3.org/2005/Atom">

<title type="text"> </title>

<updated>' . $date . '</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>' . $email . '</EmailAddress>

<FirstName>' . $first_name . '</FirstName>

<LastName>' . $last_name . '</LastName>

<OptInSource>ACTION_BY_CUSTOMER</OptInSource>

<ContactLists>

<ContactList id="http://api.constantcontact.com/ws/customers/' . $UN . '/lists/1"><ContactList>

</ContactLists>

</Contact>

</content>

</entry>';


$request = "https://api.constantcontact.com/ws/customers/" . $UN . "/contacts";

$session = curl_init($request);


$userNamePassword = $Key . '%' . $UN . ':' . $PW ;


curl_setopt($session, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);

curl_setopt($session, CURLOPT_USERPWD, $userNamePassword);

curl_setopt($session, CURLOPT_POST, 1);

curl_setopt($session, CURLOPT_POSTFIELDS , $entry);

curl_setopt($session, CURLOPT_HTTPHEADER, Array("Content-Type:application/atom+xml"));

curl_setopt($session, CURLOPT_HEADER, false);

curl_setopt($session, CURLOPT_RETURNTRANSFER, 0);


$response = curl_exec($session);

curl_close($session);


?>


 


What Am I Doing Wrong, PLEASE HELP!

4 REPLIES 4
Highlighted
Employee

Error 401 Using Contact.php

I am sorry that you are receiving a 401 error but perhaps I can help you out with this. I noticed that you had said that you tried switching the curl_setopt($session, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST); to curl_setopt($session, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); however the code that you provided still has DIGEST specified. In order to work with our authentication you MUST have BASIC set as well as using the 'https://' URI.


 

There is one other CURLOPT setting that you may need to specify due to your cURL configuration and that is:

curl_setopt($session, CURLOPT_SSL_VERIFYPEER, 0);


 

This setting circumvents an error that arises in attempting to verify our SSL certificate with certain versions of the cURL library. You may have received an error when using the combination of BASIC and 'https://' and assumed that it was the same 401 that you had received previously.


 

Looking at the rest of the code, everything seems to be in order. Please verify that you have tried with that combination and let us know how it works for you.

Dan H

Support Engineer, Constant Contact

Highlighted
Occasional Participant

Now I Get A Error 400

Making progress I guess....now I get a Error 400.  Made all changes you had suggested, thank you by the way!!!!


Here is code:


<?php




$first_name = $_POST;

$last_name = $_POST;

$email = $_POST;

$UN = "dpxxxxx";

$PW = "d2xxxxx";

$Key = "4fxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";


$date = date("Y-m-d\TH:i:s");


$entry = '<entry xmlns="http://www.w3.org/2005/Atom">

<title type="text"> </title>

<updated>' . $date . '</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>' . $email . '</EmailAddress>

<FirstName>' . $first_name . '</FirstName>

<LastName>' . $last_name . '</LastName>

<OptInSource>ACTION_BY_CUSTOMER</OptInSource>

<ContactLists>

<ContactList id="http://api.constantcontact.com/ws/customers/' . $UN . '/lists/1"><ContactList>

</ContactLists>

</Contact>

</content>

</entry>';


$request = "https://api.constantcontact.com/ws/customers/" . $UN . "/contacts";

$session = curl_init($request);


$userNamePassword = $Key . '%' . $UN . ':' . $PW ;


curl_setopt($session, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);

curl_setopt($session, CURLOPT_USERPWD, $userNamePassword);

curl_setopt($session, CURLOPT_POST, 1);

curl_setopt($session, CURLOPT_POSTFIELDS , $entry);

curl_setopt($session, CURLOPT_HTTPHEADER, Array("Content-Type:application/atom+xml"));

curl_setopt($session, CURLOPT_HEADER, false);

curl_setopt($session, CURLOPT_RETURNTRANSFER, 0);

curl_setopt($session, CURLOPT_SSL_VERIFYPEER, 0);


$response = curl_exec($session);

curl_close($session);


?>


 


 


Any more suggestions?


 

Highlighted
Occasional Participant

Error 400: The new contact must contain at least on valid Contac

actually found the cause on that but STILL HAVING ISSUES!!!


It was I had bad xml and no close tag


<ContactLists>

<ContactList id="http://api.constantcontact.com/ws/customers/' . $UN . '/lists/1"><ContactList>    <------needed to be </ContactList>

</ContactLists>

</Contact>

 


Now I get this message:


Error 400: The new contact must contain at least on valid ContactList


There is only one contact list on this account and it set that way in xml request, wtf? Am I missing something?


 

Highlighted
Occasional Participant

Working NOW!!!

I got it...it was my xml and list#...all good


list number was not what I thought it was.

Developer Portal

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

Visit Page

Constant Contact 2020 End of Summer Community Sweepstakes!

The Constant Contact User Community is hosting a sweepstakes. The more you participate, the more chances you have to win! Read on to learn more...

Read More
Featured