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 16th, 2020 at 11:59 PM EST.

PHP-SDK: Problems getting addOrUpdateContact.php to work

Highlighted
Visitor

PHP-SDK: Problems getting addOrUpdateContact.php to work

Hi,

 

I'm trying to use addOrUpdateContact.php to add a contact to my list when they register on my website.

I have an API Key and token.

 

I have downloaded the PHP-SDK wrapper and uploaded it to wp-content/theme-name/Ctct

 

and used the addOrUpdateContact.php in my registration page, as below.

 

I don't get any errors, just a completely blank page, as you'll see on my test page here: http://www.melbournemeditationcentre.com/meditation-courses/course-registration-3/

The registration process begins with this page: http://www.melbournemeditationcentre.com/meditation-courses/course-registration/

 


I'm developing this in Dreamweaver, which says that there is a syntax error (which relates to the following lines)a;

 

use Ctct\ConstantContact;
use Ctct\Components\Contacts\Contact;
use Ctct\Components\Contacts\ContactList;
use Ctct\Components\Contacts\EmailAddress;
use Ctct\Exceptions\CtctException;

 

 

THE FULL CODE:

 

<?php
// require the autoloader
require_once 'Ctct/autoload.php';

use Ctct\ConstantContact;
use Ctct\Components\Contacts\Contact;
use Ctct\Components\Contacts\ContactList;
use Ctct\Components\Contacts\EmailAddress;
use Ctct\Exceptions\CtctException;

// Enter your Constant Contact APIKEY and ACCESS_TOKEN
define("APIKEY", "myAPIKEY");
define("ACCESS_TOKEN", "myACCESS-TOKEN");

$cc = new ConstantContact(APIKEY);

// attempt to fetch lists in the account, catching any exceptions and printing the errors to screen
try {
    $lists = $cc->getLists(ACCESS_TOKEN);
} catch (CtctException $ex) {
    foreach ($ex->getErrors() as $error) {
        print_r($error);
    }
}

// check if the form was submitted
if (isset($_POST['email']) && strlen($_POST['email']) > 1) {
    $action = "Getting Contact By Email Address";
    try {
        // check to see if a contact with the email addess already exists in the account
        $response = $cc->getContactByEmail(ACCESS_TOKEN, $_POST['email']);

        // create a new contact if one does not exist
        if (empty($response->results)) {
            $action = "Creating Contact";

            $contact = new Contact();
            $contact->addEmail($email);
            $contact->addList(MME);
            $contact->first_name = $firstname;
            $contact->last_name = $surname;

            /*
* The third parameter of addContact defaults to false, but if this were set to true it would tell Constant
* Contact that this action is being performed by the contact themselves, and gives the ability to
* opt contacts back in and trigger Welcome/Change-of-interest emails.
*
* See: http://developer.constantcontact.com/docs/contacts-api/contacts-index.html#opt_in
*/
            $returnContact = $cc->addContact(ACCESS_TOKEN, $contact, false);

            // update the existing contact if address already existed
        } else {
            $action = "Updating Contact";

            $contact = $response->results[0];
            $contact->addList($_POST['list']);
            $contact->first_name = $_POST['first_name'];
            $contact->last_name = $_POST['last_name'];

            /*
* The third parameter of updateContact defaults to false, but if this were set to true it would tell
* Constant Contact that this action is being performed by the contact themselves, and gives the ability to
* opt contacts back in and trigger Welcome/Change-of-interest emails.
*
* See: http://developer.constantcontact.com/docs/contacts-api/contacts-index.html#opt_in
*/
            $returnContact = $cc->updateContact(ACCESS_TOKEN, $contact, false);
        }

        // catch any exceptions thrown during the process and print the errors to screen
    } catch (CtctException $ex) {
        echo '<span class="label label-important">Error ' . $action . '</span>';
        echo '<div class="container alert-error"><pre class="failure-pre">';
        print_r($ex->getErrors());
        echo '</pre></div>';
        die();
    }
}
?>

Help appreciated!

Thanks,

Matt.

8 REPLIES 8
Employee

Re: PHP-SDK: Problems getting addOrUpdateContact.php to work

Hi Matt,

 

I responded to your email about this.  I believe it may be an issue with the version of PHP you're running.  You need PHP 5.3 or higher to use 'use' statements.

 

Best Regards,

Shannon Wallace

Partner API Support Engineer

Highlighted
Solution Provider

Re: PHP-SDK: Problems getting addOrUpdateContact.php to work

I have a similar problem.

When I run the code and populate the Contact record and then submit it, I get a json error/

Turns out that with $returnContact = $cc->addContact(ACCESS_TOKEN, $contact);  is sent with an empty $contact record...

 

I'm puzzled

Highlighted
Honored Contributor

Re: PHP-SDK: Problems getting addOrUpdateContact.php to work

Hello,

 

Are you also working within a Wordpress install? It almost sounds like something is capturing your POST data if the code is attempting to create a blank contact. Is it possible for you to provide the exact response from the API? 

 

Also, do you know what version of PHP your server is running? Would you be able to verify the POST data being sent to the script?

 

Any of this information should help us to find what's happening. If we continue to have trouble finding the issue, there are a few more complicated things that we can check as well.

 

Best Regards,

Elijah G.
API Support Engineer
Highlighted
Solution Provider

Re: PHP-SDK: Problems getting addOrUpdateContact.php to work

Thank you for your reply.

 

I solved the problem. It was due to my input clean up PHP fonction "filter_input".

Would you have another recommendation for data input clean up or is the API cleaning anyway?

 

Best Regards
Felix

Highlighted
Honored Contributor

Re: PHP-SDK: Problems getting addOrUpdateContact.php to work

Hi Felix,

 

Constant Contact does perform some filtering of data to ensure that it is safe ot be in our systems. However, when data does not pass our filters it is usually returned back to you as an error. Because of that, I would suggest that you perform filtering within your application. I also unfortuantely do not have any reccomendations right now for a better method of data cleaning.

 

Best Regards,

Elijah G.
API Support Engineer
Highlighted
Occasional Participant

Re: PHP-SDK: Problems getting addOrUpdateContact.php to work

I still have the same problem 

 

Error Updating Contact

Array

(

    [0] => Array

        (

            [error_key] => json.field.lists.value.invalid

            [error_message] => The contact list 'newlist' does not exist.

        )

 

)

Highlighted
Employee

Re: PHP-SDK: Problems getting addOrUpdateContact.php to work

Hi Bryan,

 

Are you attempting to add a new list to the contact before updating it? If so, it looks like the list might not be getting created or referenced properly. If you are trying to create a brand new list, and then add the contact to it, I'd recommend doing something like this:

 

try { //make new list
	$activeList = new ContactList();
	$activeList->status = "ACTIVE"; //or HIDDEN
	$activeList->name = "New list name";

	$cc = new ConstantContact(APIKEY);
	$activeListResponse = $cc->addList(ACCESS_TOKEN, $activeList);
	print_r($activeListResponse);
	return $activeListResponse->id;
	
} catch (CtctException $ex) {  //catch & print exceptions
    print json_encode(array('status'=>'failure', 'message'=>$ex->getMessage()));
}

//Get contact object
$contact->addList($activeListResponse->id);
$returnContact = $cc->updateContact(ACCESS_TOKEN, $contact, true);

You can also add an existing list to the contact of course. If that's what you're trying to do, it seems like the list isn't getting added to the contact. The addOrUpdate.php code sample should be providing a dropdown in the UI where you can select from the active lists in the account and add the contact to it.

 

Best Regards,

Shannon Wallace

Partner API Support Engineer

 

 

Highlighted
Occasional Visitor

Re: PHP-SDK: Problems getting addOrUpdateContact.php to work

For future reference... I also had this problem. Using the stock example code, just loading the page (without submitting the form) resulted in a blank page with no errors. I changed my PHP version from 7.1.27 to 7.0.33 and the page loaded properly and the form submission resulted in the addition of the new contact. Hopefully this might save someone the headache of trying to figure this out on their own.

 

Cheers

C

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