We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 36723
    • 94 Posts
    Hello,

    I was trying to make a simple snippet to process the constant contact form. I am using the code from their api which autoloads classes. However I can't get it to load the ConstantContact class or anything else. I get a
    Fatal error: Class 'ConstantContact' not found in .../100.include.cache.php on line 27

    This code works perfectly outside of modx on the same server.

    Is is it possible to load this without extensive modification to modx or their code. I know I can use their built in widget but I would like to stay on the same site / url.

    <?php
    // require the autoloader
    require_once  $_SERVER['DOCUMENT_ROOT'].'/Ctct/autoload.php';
    
    // does not work in modx 
    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", "xxxxxxxxxxxxxxxxxx");
    define("ACCESS_TOKEN", "xxxxxxxxxxxxxxxxxx");
    
    $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($_POST['email']);
                $contact->addList($_POST['list']);
                $contact->first_name = $_POST['first_name'];
                $contact->last_name = $_POST['last_name'];
    
                /*
                 * 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();
        }
    }
    
    if (isset($returnContact)) {
        echo '<div class="container alert-success"><pre class="success-pre">';
        print_r($returnContact);
        echo '</pre></div>';
    }
    


    https://github.com/constantcontact/php-sdk/blob/master/examples/addOrUpdateContact.php
      • 3749
      • 24,544 Posts
      This might be relevant: http://stackoverflow.com/questions/10542012/php-namespaces-and-use.

      Worst case, you could always skip the autoloader and just include the class files you need.
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting