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.

Adding Contact: HTTP Response Code '400'

Highlighted
Occasional Participant

Adding Contact: HTTP Response Code '400'

Constantly getting error code 400 when using this script based loosely off others that I've seen. Anyone see anything wrong? (All post variables are coming across fine)


    public function create_contact($post) {

        

        // DIGEST AUTHENTICATION: API KEY, USERNAME, PASSWORD

        $userNamePassword = $this->api_key . '%' . $this->user . ':' . $this->pass ;

        

        // BUILD UPDATED CONSTANT CONTACT LIST FOR USER

        $subscription_list = '';

        foreach($post as $id) {

            $subscription_list .= "<ContactList id=\"http://api.constantcontact.com/ws/customers/{$this->user}/lists/{$id}\"></ContactList>\r";

        }

        

        // USER CONTACT INFORMATION

        $entry = <<<EOF

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

              <title type="text">Contact: {$post}</title>

              <updated>{$this->today}</updated>

              <author>Carl C.</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>{$post}</EmailAddress>

                  <FirstName>{$post}</FirstName>

                  <LastName>{$post}</LastName>

                  <OptInSource>ACTION_BY_CONTACT</OptInSource>

                  <ContactLists>

                    {$subscription_list}

                  </ContactLists>

                </Contact>

              </content>

            </entry>

EOF;



        

        // INITIALIZE THE CURL SESSION

        $session = curl_init();

        

        // CURL OPTIONS

        $options = array(

                    CURLOPT_URL => str_replace('http://', 'https://', $post),

                    CURLOPT_HTTPAUTH => CURLAUTH_BASIC,

                    CURLOPT_USERPWD => $userNamePassword,

                    CURLOPT_FOLLOWLOCATION => 1,

                    CURLOPT_POST => 1,

                    CURLOPT_POSTFIELDS => $post,

                    CURLOPT_HTTPHEADER => Array("Content-Type:application/atom+xml"),

                    CURLOPT_HEADER => 0,

                    CURLOPT_RETURNTRANSFER => 1

        );

        curl_setopt_array($session, $options);

        

        // EXECUTE CURL

        $response = curl_exec($session);

        

        // DEBUG OUTPUT

        $this->debug_print("------------------ UPDATE CONTACT ------------------");

        $this->debug_print("Connecting to '".str_replace('http://', 'https://', $post)."'");

        $this->debug_print("And Posting:<pre>\n\n---START---\n". htmlspecialchars($entry) ."\n---END---\n\n</pre>");

        $this->debug_print("HTTP Response Code '".curl_getinfo($session, CURLINFO_HTTP_CODE)."'");

        $this->debug_print("Received:<pre>\n\n---START---\n". htmlspecialchars($response) ."\n---END---\n\n</pre><br><br>");

    

        // CLOSE CONNECTION AND TEMP FILE

        curl_close($session);

        fclose($tmpfile);

    }


The debug output I receive is:


ConstantContact Debug: ------------------ UPDATE CONTACT ------------------

ConstantContact Debug: Connecting to 'https://api.constantcontact.com/ws/customers/hibbett/contacts'

ConstantContact Debug: And Posting:



---START---
<entry xmlns="http://www.w3.org/2005/Atom">
<title type="text">Contact: carllcombs@gmail.com</title>
<updated>2009-09-15T09:47:48+01:00</updated>
<author>Carl C.</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>carllcombs@gmail.com</EmailAddress>
<FirstName>Conan</FirstName>
<LastName>Barbarian</LastName>
<OptInSource>ACTION_BY_CONTACT</OptInSource>
<ContactLists>
<ContactList id="http://api.constantcontact.com/ws/customers/hibbett/lists/90"></ContactList>
<ContactList id="http://api.constantcontact.com/ws/customers/hibbett/lists/131"></ContactList>

</ContactLists>
</Contact>
</content>
</entry>
---END---


 


ConstantContact Debug: HTTP Response Code '400'


 


ConstantContact Debug: Received:



---START---
Error 400:

---END---

Thanks for any help!

 


 

6 REPLIES 6
Highlighted
Employee

RE: Adding Contact: HTTP Response Code '400'

Hello there,


 


A 400 error is typically due to an error in the XML request. After testing your XML in the RESTClient, modifying only the username and the list id number, it appears as though everything if formatted correctly. One possible of this error is that the XML that you output contains hidden characters such as new lines or carriage returns that the cURL function is not able to handle. Another possible cause of this error is that you are referencing a list id that does not exist however the response would give you a detailed message.


 


If these possibilities are not the cause of the error, please try adding CURLOPT_SSL_VERIFYPEER => 0 to your setopt array. This fixes an issue with the verification of the SSL certificate and may also be a factor in this issue.


 


If you are still unsuccessful in resolving this, please attach your complete code in an email to us.

Dan H

Support Engineer, Constant Contact

Highlighted
Occasional Participant

Still no progress

What you see above is pretty much all the code minus the sql query that gathers all the $post values.


I also tried your suggestions and they're not working for me.


I did receive an email yesterday around 3pm central that welcomed me as a new user...I assumed this was you or someone there testing my script.


Since you say it worked for you, can you send me exactly what you used? I've spent a dozen hours going through your docs and looking at my code line by line...I can't see anything wrong.


I am able to update users without problem btw.


Thanks,

-Carl

Highlighted
Employee

I was able to successfully

I was able to successfully add a contact to my account using your XML and the RESTClient. The only change made to your XML was modifying the list to one that matched my list id. I would recommend that you try the same approach to see if your XML is correct. If you are able to send a successful request then we can isolate the error to your code. Please feel free to email me the code that you are using to webservices support so that i can run it on my local machine and debug further.

Dan H

Support Engineer, Constant Contact

Highlighted
Frequent Participant

heredocs are funny...

If you copied straight from the online manual into the heredoc statement it could just be the newline characters are doing some funny stuff:


http://php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc


I would personally not use a heredoc, even though they are easier to read in some situations.


 


 


 

James Benson, PHP Developer.


Constant Contact Services: http://integrationservic.es/

Highlighted
Occasional Participant

RESTClient Works...script still does not

I tried the RESTClient and it works fine there. I was able to create a contact without changing anything in the xml. I've also converted the heredoc (I hate em too. The first developer loved em) to just a string and even went as far as to remove all carriage returns so it outputs as one long string...still getting 400 on response.


Below is the code I'm using after a few modifications over the last few days:


public function create_contact($post) {

       

        // BASIC AUTHENTICATION: API KEY, USERNAME, PASSWORD

        $userNamePassword = $this->api_key . '%' . $this->user . ':' . $this->pass ;

       

        // BUILD UPDATED CONSTANT CONTACT LIST FOR USER

        $subscription_list = '';

        foreach($post as $id) {

            $subscription_list .= '<ContactList id="http://api.constantcontact.com/ws/customers/'.$this->user.'/lists/'.$id.'"></ContactList>';

        }

       

        // USER CONTACT INFORMATION

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

            $xml .= '<title type="text">Contact: '.$post.'</title>';

            $xml .= '<updated>'.$this->today.'</updated>';

            $xml .= '<author>Carl C.</author>';

            $xml .= '<id>data:,none</id>';

            $xml .= '<summary type="text">Contact</summary>';

            $xml .= '<content type="application/vnd.ctct+xml">';

                $xml .= '<Contact xmlns="http://ws.constantcontact.com/ns/1.0/">';

                    $xml .= '<EmailAddress>'.$post.'</EmailAddress>';

                    $xml .= '<FirstName>'.$post.'</FirstName>';

                    $xml .= '<LastName>'.$post.'</LastName>';

                    $xml .= '<OptInSource>ACTION_BY_CONTACT</OptInSource>';

                    $xml .= '<ContactLists>';

                        $xml .= $subscription_list;

                     $xml .= '</ContactLists>';

                $xml .= '</Contact>';

              $xml .= '</content>';

        $xml .= '</entry>';



       

        // INITIALIZE THE CURL SESSION

        $session = curl_init();

       

        // CURL OPTIONS

        $options = array(

                    CURLOPT_URL => str_replace('http://', 'https://', $post),

                    CURLOPT_HTTPAUTH => CURLAUTH_BASIC,

                    CURLOPT_USERPWD => $userNamePassword,

                    CURLOPT_FOLLOWLOCATION => 1,

                    CURLOPT_POST => 1,

                    CURLOPT_POSTFIELDS => $post,

                    CURLOPT_HTTPHEADER => Array("Content-Type:application/atom+xml"),

                    CURLOPT_HEADER => 0,

                    CURLOPT_RETURNTRANSFER => 1,

                    CURLOPT_SSL_VERIFYPEER => 0

        );

        curl_setopt_array($session, $options);

       

        // EXECUTE CURL

        $response = curl_exec($session);

       

        // DEBUG OUTPUT

        $this->debug_print("------------------ UPDATE CONTACT ------------------");

        $this->debug_print("Connecting to '".str_replace('http://', 'https://', $post)."'");

        $this->debug_print("And Posting:<pre>\n\n---START---\n". htmlspecialchars($xml) ."\n---END---\n\n</pre>");

        $this->debug_print("HTTP Response Code '".curl_getinfo($session, CURLINFO_HTTP_CODE)."'");

        $this->debug_print("Received:<pre>\n\n---START---\n". htmlspecialchars($response) ."\n---END---\n\n</pre><br><br>");

   

        // CLOSE CONNECTION AND TEMP FILE

        curl_close($session);

    }


I'm suspecting now that the problem must be in the CURL settings somewhere. I pulled all settings from the sample files provided on this site, but honestly, I'm not very familiar with CURL

Highlighted
Occasional Participant

Okay...figured it out

This line:



  • CURLOPT_POSTFIELDS => $post,


...was posting my $post array of values from a SQL query.

I changed it to post the xml string:



  • CURLOPT_POSTFIELDS => $xml,


...and it works fine.


Thanks for helping narrow this down guys.

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