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.

CC Integration namespace error

Highlighted
Occasional Participant

CC Integration namespace error

Trying to integrate a simple cc contact form, capturing email address, posting to constant contact.  Hvae all mashery keys etc.  Actually have simple sample version of script working in directory.

 

As soon as I put the constant contact integration form into my registration form, it stops displaying the form and all other fields as if there is a php error.  The type you see when you have a wrong path on an include etc.

 

The autoload is loading fine.

 

I commented out the namespace references below and the page displays again.

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

 

Any ideas on what I am doing wrong?

 

Example URL :   site.com/index.php?page=includes/register

 

CC code is in this page, src/Ctct code is in same includes directory.

7 REPLIES 7
Highlighted
Employee

Re: CC Integration namespace error

Hi,

 

It sounds like it very well might be a path issue.  Make sure you have the correct relative url in your require autoload line.  My script is in the top-level folder "php-sdk" at the same level as the docs, src, and test folders, and my require/use's appear as:

 

require 'src/Ctct/autoload.php';

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

 

 

Best Regards,

Shannon W.

API Support Specialist

Highlighted
Occasional Participant

Re: CC Integration namespace error

All of the source for the constant contact integration is in public_html/includes/  The page that is loading the form is in this same folder.   Path for the form is index.php?page=includes/register2

 

In the form we have the code below.  the src folder is in the includes folder.

 

include 'src/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;

 

Inside of this site page, it fails, but if I just call constant contact straight from a php file, it displays fine.  I am also able to call it with index.php?page=test_constant_contact and it works fine.

 

Seems as though it may be a conflict with my code, but I have no clue what it could be as this has never happened before.  I would not say I am a PHP expert, but I have been doing this for over 10 years.

 

Any ideas?  I really do not want to recode the registration page.

Highlighted
Employee

Re: CC Integration namespace error

Hi,

 

I responded to your private message as well, but it's probably going to be hard for me to come up with troubleshooting steps that you haven't already.  A couple more ideas:

 

1.  Make sure you adjusted the path of the autoload; it looks like your live page might be up one level from the test page.

2.  Can you copy your registration page that's not working, and remove the non-Constant Contact code until the form works?

 

Best Regards,

Shannon W.

API Support Specialist

Highlighted
Employee

Re: CC Integration namespace error

Here is a more basic example of the php code you need for a signup form, html omitted.  This is in response to your private message, but I think other users might benifit from this in the future, so I wanted to post here.

 

<?php

//Contact add/update
require 'src/Ctct/autoload.php';

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

$apiKey = "value";
$accessToken = "value";

$cc = new ConstantContact($apiKey);

$email = 'test_email@test.com';
$contact = new Contact();
$contact->addEmail($email);
$contact->addList("1");

try {//search for contact by email to see if it exists in the account
    $response = $cc->getContactByEmail($accessToken, $email);

    if (empty($response->results)) {//add contact if it isn't in the account
        echo "adding ...";
        $responseContact = $cc->addContact($accessToken, $contact, $actionByVisitor = true); 
    } else {//else update contact
        echo "updating ....";
        $contact->id = $response->results[0]->id;
        $contact->first_name = "test";
        $responseContact = $cc->updateContact($accessToken, $contact, $actionByVisitor = true);  
    }

    print_r($responseContact);

} catch (CtctException $ex) {
    foreach ($ex->getErrors() as $error) {
        print_r($error);
    }              
}
?>

 

 

Best Regards,

Shannon W.

API Support Specialist

Highlighted
Occasional Participant

Re: CC Integration namespace error

thank you for the responses.  Unfortunatley it still doesnt work.  As soon as I comment out the namespace calls it  opens fine but obviously will not send the email to the constant contact db.

 

I am trying to put this insde of some php code that is executed when the normal registration form is submitted.

Highlighted
Occasional Participant

Re: CC Integration namespace error

so I started really debugging the issue.  I took the calls to the namespace and put them at the very top of the php file and

 

require 'src/Ctct/autoload.php';

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

 

then the code goes through its logic , is form submitted etc.

 

Inside of the test for a new account being registered I have the logic to actually create the contact

 

if (isset($_POST['submit'])) {

///////////////////////////////////////////////////////////
//CONSTANT CONTACT INTEGRATION
///////////////////////////////////////////////////////////
//Contact add/update
define("APIKEY", "BLUR");
define("ACCESS_TOKEN", "BLUR");
$cc = new ConstantContact($apiKey);

$email = $_POST['email'];

 

The page now loads and allows me to register a new account but I am getting this out of constant contact 

 

Array ( [error_key] => mashery.not.authorized.inactive [error_message] => The Mashery account associated with this request is not active, or an invalid API key was used. ) 
Warning: Cannot modify header information - headers already sent by (output started at /home/BLUR:101) in /home/BLUR on line 140

 

I have checked the mashery id and key etc over and over and everything seems fine there.  Could this be because of the way I have split the logic to declare the namespaces vs checking for the submit?

Highlighted
Employee

Re: CC Integration namespace error

Hi,

 

You're defining the API Key, and most likely access token, in a different way than it is used in the script.

 

You're providing:

 

define("APIKEY", "BLUR");

 

Then calling:

 

$cc = new ConstantContact($apiKey);

To use the above line, you need to have set the value of apiKey previously, like below, changing "value" to your actual API Key value.

 

$apiKey = "value";

 

The warning you'r receiving has to do with your environment--headers are already being set by your server or somewhere else.

 

Best Regards,

Shannon W.

API Support Specialist

 

 

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