Overview

Namespaces

  • Ctct
    • Auth
    • Components
      • Account
      • Activities
      • Contacts
      • EmailMarketing
      • Library
      • Tracking
    • Exceptions
    • Services
    • Util
    • WebHooks

Classes

  • Ctct\Auth\CtctOAuth2
  • Ctct\Auth\SessionDataStore
  • Ctct\Components\Account\AccountInfo
  • Ctct\Components\Account\VerifiedEmailAddress
  • Ctct\Components\Activities\Activity
  • Ctct\Components\Activities\ActivityError
  • Ctct\Components\Activities\AddContacts
  • Ctct\Components\Activities\AddContactsImportData
  • Ctct\Components\Activities\ExportContacts
  • Ctct\Components\Component
  • Ctct\Components\Contacts\Address
  • Ctct\Components\Contacts\Contact
  • Ctct\Components\Contacts\ContactList
  • Ctct\Components\Contacts\CustomField
  • Ctct\Components\Contacts\EmailAddress
  • Ctct\Components\Contacts\Note
  • Ctct\Components\EmailMarketing\Campaign
  • Ctct\Components\EmailMarketing\ClickThroughDetails
  • Ctct\Components\EmailMarketing\MessageFooter
  • Ctct\Components\EmailMarketing\Schedule
  • Ctct\Components\EmailMarketing\TestSend
  • Ctct\Components\Library\File
  • Ctct\Components\Library\FileUploadStatus
  • Ctct\Components\Library\Folder
  • Ctct\Components\Library\Thumbnail
  • Ctct\Components\ResultSet
  • Ctct\Components\Tracking\BounceActivity
  • Ctct\Components\Tracking\ClickActivity
  • Ctct\Components\Tracking\ForwardActivity
  • Ctct\Components\Tracking\OpenActivity
  • Ctct\Components\Tracking\SendActivity
  • Ctct\Components\Tracking\TrackingActivity
  • Ctct\Components\Tracking\TrackingSummary
  • Ctct\Components\Tracking\UnsubscribeActivity
  • Ctct\ConstantContact
  • Ctct\Services\AccountService
  • Ctct\Services\ActivityService
  • Ctct\Services\BaseService
  • Ctct\Services\CampaignScheduleService
  • Ctct\Services\CampaignTrackingService
  • Ctct\Services\ContactService
  • Ctct\Services\ContactTrackingService
  • Ctct\Services\EmailMarketingService
  • Ctct\Services\LibraryService
  • Ctct\Services\ListService
  • Ctct\SplClassLoader
  • Ctct\Util\Config
  • Ctct\WebHooks\CTCTWebhookUtil

Interfaces

  • Ctct\Auth\CtctDataStore

Exceptions

  • Ctct\Exceptions\CtctException
  • Ctct\Exceptions\IllegalArgumentException
  • Ctct\Exceptions\OAuth2Exception
  • Overview
  • Namespace
  • Class
  1: <?php
  2: namespace Ctct\Services;
  3: 
  4: use Ctct\Exceptions\CtctException;
  5: use Ctct\Util\Config;
  6: use Ctct\Components\Contacts\Contact;
  7: use Ctct\Components\ResultSet;
  8: use GuzzleHttp\Exception\ClientException;
  9: use GuzzleHttp\Stream\Stream;
 10: 
 11: /**
 12:  * Performs all actions pertaining to Constant Contact Contacts
 13:  *
 14:  * @package Services
 15:  * @author ContactContact
 16:  */
 17: class ContactService extends BaseService
 18: {
 19:     /**
 20:      * Get a ResultSet of contacts
 21:      * @param string $accessToken - Constant Contact OAuth2 access token
 22:      * @param array $params - associative array of query parameters and values to append to the request.
 23:      *      Allowed parameters include:
 24:      *      limit - Specifies the number of results displayed per page of output, from 1 - 500, default = 50.
 25:      *      modified_since - ISO-8601 formatted timestamp.
 26:      *      next - the next link returned from a previous paginated call. May only be used by itself.
 27:      *      email - full email address string to restrict results by
 28:      *      status - a contact status to filter results by. Must be one of ACTIVE, OPTOUT, REMOVED, UNCONFIRMED.
 29:      * @return ResultSet
 30:      * @throws CtctException
 31:      */
 32:     public function getContacts($accessToken, Array $params = array())
 33:     {
 34:         $baseUrl = Config::get('endpoints.base_url') . Config::get('endpoints.contacts');
 35: 
 36:         $request = parent::createBaseRequest($accessToken, 'GET', $baseUrl);
 37:         if ($params) {
 38:             $query = $request->getQuery();
 39:             foreach ($params as $name => $value) {
 40:                 $query->add($name, $value);
 41:             }
 42:         }
 43: 
 44:         try {
 45:             $response = parent::getClient()->send($request);
 46:         } catch (ClientException $e) {
 47:             throw parent::convertException($e);
 48:         }
 49: 
 50:         $body = $response->json();
 51:         $contacts = array();
 52:         foreach ($body['results'] as $contact) {
 53:             $contacts[] = Contact::create($contact);
 54:         }
 55:         return new ResultSet($contacts, $body['meta']);
 56:     }
 57: 
 58:     /**
 59:      * Get all contacts from an individual list
 60:      * @param string $accessToken - Constant Contact OAuth2 access token
 61:      * @param string $listId - {@link ContactList} id to retrieve contacts for
 62:      * @param array $params - associative array of query parameters and values to append to the request.
 63:      *      Allowed parameters include:
 64:      *      limit - Specifies the number of results displayed per page of output, from 1 - 500, default = 50.
 65:      *      modified_since - ISO-8601 formatted timestamp.
 66:      *      next - the next link returned from a previous paginated call. May only be used by itself.
 67:      *      email - full email address string to restrict results by
 68:      *      status - a contact status to filter results by. Must be one of ACTIVE, OPTOUT, REMOVED, UNCONFIRMED.
 69:      * @return ResultSet
 70:      * @throws CtctException
 71:      */
 72:     public function getContactsFromList($accessToken, $listId, Array $params = array())
 73:     {
 74:         $baseUrl = Config::get('endpoints.base_url') . sprintf(Config::get('endpoints.list_contacts'), $listId);
 75: 
 76:         $request = parent::createBaseRequest($accessToken, 'GET', $baseUrl);
 77:         if ($params) {
 78:             $query = $request->getQuery();
 79:             foreach ($params as $name => $value) {
 80:                 $query->add($name, $value);
 81:             }
 82:         }
 83: 
 84:         try {
 85:             $response = parent::getClient()->send($request);
 86:         } catch (ClientException $e) {
 87:             throw parent::convertException($e);
 88:         }
 89: 
 90:         $body = $response->json();
 91:         $contacts = array();
 92:         foreach ($body['results'] as $contact) {
 93:             $contacts[] = Contact::create($contact);
 94:         }
 95:         return new ResultSet($contacts, $body['meta']);
 96:     }
 97: 
 98:     /**
 99:      * Get contact details for a specific contact
100:      * @param string $accessToken - Constant Contact OAuth2 access token
101:      * @param int $contactId - Unique contact id
102:      * @return Contact
103:      * @throws CtctException
104:      */
105:     public function getContact($accessToken, $contactId)
106:     {
107:         $baseUrl = Config::get('endpoints.base_url') . sprintf(Config::get('endpoints.contact'), $contactId);
108: 
109:         $request = parent::createBaseRequest($accessToken, 'GET', $baseUrl);
110: 
111:         try {
112:             $response = parent::getClient()->send($request);
113:         } catch (ClientException $e) {
114:             throw parent::convertException($e);
115:         }
116: 
117:         return Contact::create($response->json());
118:     }
119: 
120:     /**
121:      * Add a new contact to the Constant Contact account
122:      * @param string $accessToken - Constant Contact OAuth2 access token
123:      * @param Contact $contact - Contact to add
124:      * @param array $params - associative array of query parameters and values to append to the request.
125:      *      Allowed parameters include:
126:      *      action_by - Whether the contact is taking the action, or the account owner. Must be one of
127:      *                  ACTION_BY_OWNER or ACTION_BY_VISITOR
128:      * @return Contact
129:      * @throws CtctException
130:      */
131:     public function addContact($accessToken, Contact $contact, Array $params = array())
132:     {
133:         $baseUrl = Config::get('endpoints.base_url') . Config::get('endpoints.contacts');
134: 
135:         $request = parent::createBaseRequest($accessToken, 'POST', $baseUrl);
136:         if ($params) {
137:             $query = $request->getQuery();
138:             foreach ($params as $name => $value) {
139:                 $query->add($name, $value);
140:             }
141:         }
142:         $stream = Stream::factory(json_encode($contact));
143:         $request->setBody($stream);
144: 
145:         try {
146:             $response = parent::getClient()->send($request);
147:         } catch (ClientException $e) {
148:             throw parent::convertException($e);
149:         }
150: 
151:         return Contact::create($response->json());
152:     }
153: 
154:     /**
155:      * Opt out a contact
156:      * @param string $accessToken - Constant Contact OAuth2 access token
157:      * @param int $contactId - Unique contact id
158:      * @return boolean
159:      * @throws CtctException
160:      */
161:     public function unsubscribeContact($accessToken, $contactId) {
162:         $baseUrl = Config::get('endpoints.base_url') . sprintf(Config::get('endpoints.contact'), $contactId);
163: 
164:         $request = parent::createBaseRequest($accessToken, 'DELETE', $baseUrl);
165: 
166:         try {
167:             $response = parent::getClient()->send($request);
168:         } catch (ClientException $e) {
169:             throw parent::convertException($e);
170:         }
171: 
172:         return ($response->getStatusCode() == 204) ? true : false;
173:     }
174: 
175:     /**
176:      * Update contact details for a specific contact
177:      * @param string $accessToken - Constant Contact OAuth2 access token
178:      * @param Contact $contact - Contact to be updated
179:      * @param array $params - associative array of query parameters and values to append to the request.
180:      *      Allowed parameters include:
181:      *      action_by - Whether the contact is taking the action, or the account owner. Must be one of
182:      *                  ACTION_BY_OWNER or ACTION_BY_VISITOR
183:      * @return Contact
184:      * @throws CtctException
185:      */
186:     public function updateContact($accessToken, Contact $contact, Array $params = array())
187:     {
188:         $baseUrl = Config::get('endpoints.base_url') . sprintf(Config::get('endpoints.contact'), $contact->id);
189: 
190:         $request = parent::createBaseRequest($accessToken, 'PUT', $baseUrl);
191:         if ($params) {
192:             $query = $request->getQuery();
193:             foreach ($params as $name => $value) {
194:                 $query->add($name, $value);
195:             }
196:         }
197: 
198:         try {
199:             $response = parent::getClient()->send($request);
200:         } catch (ClientException $e) {
201:             throw parent::convertException($e);
202:         }
203: 
204:         return Contact::create($response->json());
205:     }
206: }
207: 
API documentation generated by ApiGen