adding new users via 3rd party api (OAuth) with php

Regular Participant

adding new users via 3rd party api (OAuth) with php

I'm trying to add new leads from a database pull.  This code is based on the ctctWrapper.php code.  

I run a check for existing contacts using the email then if there is no conflict I add the contact.  

It works until I add the code to add the contact.  Then the connection spins and doesn't resolve the remainder of the page (waiting on server...)



//let's sync that list!


            $leads = $aero79->GetLeads($_SESSION->Business()->GetProperty('ID'), FALSE, 6);

            if (count($leads) > 0)


                for ($i = 0; $i < count($leads) && $i < 6; $i++)


                    //Create API Request to check if contact exists

                    $email_api_request = OAuthRequest::from_consumer_and_token($oauth_consumer, $authorization_token, 'GET', $webServiceUrl.'contacts?email='.urlencode($leads));


                    // Sign request

                    $email_api_request->sign_request($sig_method, $oauth_consumer, $authorization_token);



                    // Execute HTTP Request 

                    $email_request_url  = $email_api_request->to_url();

                    $email_session = curl_init($email_request_url);

                    curl_setopt($email_session, CURLOPT_RETURNTRANSFER, 1);

                    curl_setopt($email_session, CURLOPT_SSL_VERIFYPEER, 0);


                    //Execute cURL request and get return.  Returns blank xml or error code 500 if no contact exists

           $aero79_check_contact = curl_exec($email_session);

           $aero79_check_contact = curl_getinfo($email_session);

           $aero79_check_contact = curl_error($email_session);



         $email_exists = simplexml_load_string($aero79_check_contact);

                    if(!$email_exists->entry->content) {

                    //if 'content' is blank, should return false - means there is no existing contact

                        $name = explode(" ", $leads);


                        $first_name = $name;

                        $last_name = $name;

                        if(isset($name)) { $last_name .= " ".$name; }


                        $id = "urn:uuid:E8553C09F4xcvxCCC53F481214230867087";

           //Build XML Request

           $update_date = date("Y-m-d").'T'.date("H:i:s").'+01:00';

           $xml_string = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><entry xmlns=''></entry>";

           $xml_object = simplexml_load_string($xml_string);

           $title_node = $xml_object->addChild("title", htmlspecialchars(("TitleNode"), ENT_QUOTES, 'UTF-8'));

           $updated_node = $xml_object->addChild("updated", htmlspecialchars(($update_date), ENT_QUOTES, 'UTF-8'));

           $author_node = $xml_object->addChild("author");

           $author_name = $author_node->addChild("name", (""));

           $id_node = $xml_object->addChild("id", htmlspecialchars(((string) $id),ENT_QUOTES, 'UTF-8'));

           $summary_node = $xml_object->addChild("summary", htmlspecialchars(("Customer document"),ENT_QUOTES, 'UTF-8'));

           $summary_node->addAttribute("type", "text");

           $content_node = $xml_object->addChild("content");

           $content_node->addAttribute("type", "application/vnd.ctct+xml");

           $contact_node = $content_node->addChild("Contact", htmlspecialchars(("Customer document"), ENT_QUOTES, 'UTF-8'));

           $contact_node->addAttribute("xmlns", "");

           $email_node = $contact_node->addChild("EmailAddress", htmlspecialchars(($leads), ENT_QUOTES, 'UTF-8'));

           $fname_node = $contact_node->addChild("FirstName", urldecode(htmlspecialchars(($first_name), ENT_QUOTES, 'UTF-8')));

           $lname_node = $contact_node->addChild("LastName", urldecode(htmlspecialchars(($last_name), ENT_QUOTES, 'UTF-8')));

           $optin_node = $contact_node->addChild("OptInSource", htmlspecialchars(("ACTION_BY_CUSTOMER"), ENT_QUOTES, 'UTF-8'));

           $hn_node = $contact_node->addChild("HomePhone", htmlspecialchars($leads, ENT_QUOTES, 'UTF-8'));

                        $contactlists_node = $contact_node->addChild("ContactLists");

                 $contactlist_node = $contactlists_node->addChild("ContactList");

           $contactlist_node->addAttribute("id", $aero79_list);

           $contactXML = $xml_object->asXML();


                        //Auth request with stored OAuth Creds

                        $contact_api_request = OAuthRequest::from_consumer_and_token($oauth_consumer, $authorization_token, 'POST', $webServiceUrl.'contacts');

                        $contact_api_request->sign_request($sig_method, $oauth_consumer, $authorization_token);



                        $request_url  = $contact_api_request ->to_url();

                        $contact_request = curl_init($request_url);


                        //Send POST with XML to the lists REST API url

                       curl_setopt($contact_request, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");

           curl_setopt($contact_request, CURLOPT_HEADER, 0);

           curl_setopt($contact_request, CURLOPT_RETURNTRANSFER, 1);

           curl_setopt($contact_request, CURLOPT_HTTPHEADER, Array("Content-Type:application/atom+xml", "accept:application/atom+xml", 'Content-Length: ' . strlen($contactXML)));

               curl_setopt($contact_request, CURLOPT_FAILONERROR, 1);

           curl_setopt($contact_request, CURLOPT_SSL_VERIFYPEER, 0);

           curl_setopt($contact_request, CURLOPT_CUSTOMREQUEST, 'POST');

           curl_setopt($contact_request, CURLOPT_POSTFIELDS, $listXML);

               //Execute cURL request and get return.

           $aero79_new_contact = curl_exec($contact_request);

           $aero79_new_contact = curl_getinfo($contact_request);

           $aero79_new_contact = curl_error($contact_request);



                        if(!$aero79_new_contact) {

                            echo "<h2>YAY!</h2>";

                        } else {

                            echo "<h2>DOH!</h2>";

                            echo "<pre>";


                            echo "</pre>";







                //No leads to sync!    




Hi There,

I believe the issue that you are running into is actually something very small. Towards the end of the code you posted, you are using the following line to post the XML that you created for your new contact.

curl_setopt($contact_request, CURLOPT_POSTFIELDS, $listXML);


However, the variable you are saving your XML to is actually $contactXML. I believe if you change this to the following, the script you wrote will work just fine.

curl_setopt($contact_request, CURLOPT_POSTFIELDS, $contactXML);


Please let me know if this works for you or if you have any other questions or concerns regarding this. Thanks.

David J

Regular Participant

Actually - that was most of the issue.  I can't believe I missed that.  Thanks again!



Developer Portal

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

Visit Page