1

I tried use ti contant contact SDK in magento2 but not success.

This code:

$path = pathinfo($_SERVER["SCRIPT_FILENAME"]);

require_once __DIR__ .'/php-sdk-master/src/Ctct/autoload.php';
require_once $path['dirname'].'/vendor/autoload.php';
use Ctct\ConstantContact;
use Ctct\Components\Contacts\Contact;
use Ctct\Exceptions\CtctException;

public function addContacts($memberData)
    {   
        $apiKey = $this->getApiKey();
        $accessToken = $this->getAccessToken();
        $email = $memberData['email_address'];
        $list = $memberData['list'];
        $cc = new ConstantContact($apiKey);

        // attempt to fetch lists in the account, catching any exceptions and printing the errors to screen
        try {
            $lists = $cc->listService->getLists($accessToken);
        } catch (CtctException $ex) {
            foreach ($ex->getErrors() as $error) {
                print_r($error);
            }
            if (!isset($lists)) {
                $lists = null;
            }
        }
            $action = "Getting Contact By Email Address";
            try {
                // check to see if a contact with the email address already exists in the account
                $response = $cc->contactService->getContacts($accessToken, array("email" => $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($list);
                    $contact->first_name = "";
                    $contact->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->contactService->addContact($accessToken, $contact);

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

                    $contact = $response->results[0];
                    if ($contact instanceof Contact) {
                        $contact->addList($list);
                        $contact->first_name = '';
                        $contact->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->contactService->updateContact($accessToken, $contact);
                    } else {
                        $e = new CtctException();
                        $e->setErrors(array("type", "Contact type not returned"));
                        throw $e;
                    }
                }

                // 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();
            }
        print_r($response); die();
        return $response;
    }

after i get message Class 'GuzzleHttp\Client' not found in ...\php-sdk-master\src\Ctct\Services\BaseService.php

i tried get source https://github.com/guzzle/guzzle but not run.

Pls help me how to require GuzzleHttp\Client in my source.

Many thanks!

1

Use composer to add constantcontact/constantcontact as dependensies for you module/magento app": composer require "constantcontact/constantcontact:2.1.*"

After this you no need use custom autoloader (require_once DIR .'/php-sdk-master/src/Ctct/autoload.php';)

  • Thanks @Kandy In my source has folder standalone/vendor zip. I extract it and require_once __DIR__ .'/php-sdk-master/src/Ctct/autoload.php'; require_once __DIR__ .'/php-sdk-master/standalone/vendor/autoload.php'; in file then it good work. – Iner Nov 29 '16 at 1:59
  • thank's, i'm still confused about how/where to use require_once(autloader), mine is simple handmade script that loads statically every needed file (not much), I tried in registration.php but it causes a login issue (admin and front) and don't know how to debug it – medmek Jun 27 '18 at 17:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.