HTTP Status 401 - Unable to authenticate user 'myusername'

Occasional Participant

HTTP Status 401 - Unable to authenticate user 'myusername'


I'm trying to communcate with ConstantConact API using PHP  below:

$ConstantContact = new Constantcontact("basic", myapi, myusername, mypass);


if i put wrong credentail, I got 401 error. This is OK, i understand this error but this gets diplay on my whole page. I want to catch the error and show some user friendly error message to user on page.


try {
        // Create ConstantContact object and call to ConstantContact api
       $ConstantContact = new Constantcontact("basic", myapi, myusername, mypass);
        // Get ConstantContact's account lists
        return $ConstantContact->getLists();
    } catch(Exception $ex) {
    // Not able to catch and log the error


I'm not able to catch the error and i always get the ConstantConact error list on whole page like below:


Can you please help me that if my user enter invalid credential and error is returned back then how can i catch this error and show my user friendly errro message to user on page? Thanks


This is due to how the wrapper currently handles exceptions (something we are working on enhancing and changing going forward).  To change this and catch the exception, you'll just need to modify a couple of lines of code.  In the Components.php file, you'll find all the exception handling is done by one method:



class CTCTException extends Exception{

    public function __construct($message, $code = 0, Exception $previous = null){

        parent::__construct($message, $code);



    public function generateError($msgPrefix=null){


        echo $msgPrefix.' '.$this->getMessage().'<br />';



    private function logError($errorText, $file="error.log"){


        $message = "Constant Contact Exception -- ".date("F j, Y, g:i:sa")."\n".$errorText."\n";

        $message .= "Stack Trace: ".$this->getTraceAsString()."\n";

        error_log($message."\n", 3, $file);




To stop the reporting of the error to your screen, you'll just need to remove the echo line in the function generateError().  You can instead throw the exception higher up to catch back in your main code path and handle there. 

Dave Berard
Senior Product Manager, Constant Contact
Occasional Participant

Hi Dave Berard,


Thanks for your quick response.


The solution you porovided didn't work for me. I just removed the echo line in the function generateError() and now i am getting an other error on screen.


I'm at very close dealine to implement your API at very simple level for my client, So please do help me to figure out that how i can prevent all the errors being displayed  on screen?


I even got an other email from your  Colleague Mark. He is mentioning to replace echo with return, i think he is referring the same echo in the function generateError(). Here is Mark's words:


"In the Components.php function near the bottom of the file, you can change the echo to a return, to return the error message to the makeRequest function in the Authentication.php file, but you may also need to modify code near the bottom of that function so that the the returned error message is added the $return array, and can be read out of the response by the function that is initiating your API request."



I would be very gald if you could help and figure out this issue in day. Thanks again for you great suppot and looking forward to hear your repsonse. Thanks




Thanks for the screenshot.  What you're seeing are warnings being sent by PHP, these are not the same as errors or exceptions being echo'd to the screen.  They also don't mean that the library is not working, it actually is.  What this is showing is an error trying to use an XML parser on the response body of an error message for the 401 Unauthorized.  You would need to surpress warnings in PHP to disable these from showing up, there is nothing in the library that does this by default. 


The only reason you are seeing this error is because you are getting a 401 unauthorized message.  If you update the username/password combination to be a valid username/password, this error will go away and you won't see those warning messages anymore. 

Dave Berard
Senior Product Manager, Constant Contact
Developer Portal

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

Visit Page