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\Activities\Activity;
  7: use Ctct\Components\Activities\AddContacts;
  8: use Ctct\Components\Activities\ExportContacts;
  9: use GuzzleHttp\Exception\ClientException;
 10: use GuzzleHttp\Post\PostBody;
 11: use GuzzleHttp\Post\PostFile;
 12: use GuzzleHttp\Stream\Stream;
 13: 
 14: /**
 15:  * Performs all actions pertaining to scheduling Constant Contact Activities
 16:  *
 17:  * @package Services
 18:  * @author ConstantContact
 19:  */
 20: class ActivityService extends BaseService
 21: {
 22:     /**
 23:      * Get an array of activities
 24:      * @param string $accessToken - Constant Contact OAuth2 access token
 25:      * @param array $params - associative array of query parameters and values to append to the request.
 26:      *      Allowed parameters include:
 27:      *      status - Status of the activity, must be one of UNCONFIRMED, PENDING, QUEUED, RUNNING, COMPLETE, ERROR
 28:      *      type - Type of activity, must be one of ADD_CONTACTS, REMOVE_CONTACTS_FROM_LISTS, CLEAR_CONTACTS_FROM_LISTS,
 29:      *             EXPORT_CONTACTS
 30:      * @return array - Array of all ActivitySummaryReports
 31:      * @throws CtctException
 32:      */
 33:     public function getActivities($accessToken, Array $params = array())
 34:     {
 35:         $baseUrl = Config::get('endpoints.base_url') . Config::get('endpoints.activities');
 36: 
 37:         $request = parent::createBaseRequest($accessToken, 'GET', $baseUrl);
 38:         if ($params) {
 39:             $query = $request->getQuery();
 40:             foreach ($params as $name => $value) {
 41:                 $query->add($name, $value);
 42:             }
 43:         }
 44: 
 45:         try {
 46:             $response = parent::getClient()->send($request);
 47:         } catch (ClientException $e) {
 48:             throw parent::convertException($e);
 49:         }
 50: 
 51:         $activities = array();
 52:         foreach ($response->json() as $activity) {
 53:             $activities[] = Activity::create($activity);
 54:         }
 55:         return $activities;
 56:     }
 57: 
 58:     /**
 59:      * Get an array of activities
 60:      * @param string $accessToken - Constant Contact OAuth2 access token
 61:      * @param string $activityId - Activity id
 62:      * @return array - Array of all ActivitySummaryReports
 63:      * @throws CtctException
 64:      */
 65:     public function getActivity($accessToken, $activityId)
 66:     {
 67:         $baseUrl = Config::get('endpoints.base_url') . sprintf(Config::get('endpoints.activity'), $activityId);
 68: 
 69:         $request = parent::createBaseRequest($accessToken, 'GET', $baseUrl);
 70: 
 71:         try {
 72:             $response = parent::getClient()->send($request);
 73:         } catch (ClientException $e) {
 74:             throw parent::convertException($e);
 75:         }
 76: 
 77:         return Activity::create($response->json());
 78:     }
 79: 
 80:     /**
 81:      * Create an Add Contacts Activity
 82:      * @param string $accessToken - Constant Contact OAuth2 access token
 83:      * @param AddContacts $addContacts
 84:      * @return array - Array of all ActivitySummaryReports
 85:      * @throws CtctException
 86:      */
 87:     public function createAddContactsActivity($accessToken, AddContacts $addContacts)
 88:     {
 89:         $baseUrl = Config::get('endpoints.base_url') . Config::get('endpoints.add_contacts_activity');
 90: 
 91:         $request = parent::createBaseRequest($accessToken, 'POST', $baseUrl);
 92:         $stream = Stream::factory(json_encode($addContacts));
 93:         $request->setBody($stream);
 94: 
 95:         try {
 96:             $response = parent::getClient()->send($request);
 97:         } catch (ClientException $e) {
 98:             throw parent::convertException($e);
 99:         }
100: 
101:         return Activity::create($response->json());
102:     }
103: 
104:     /**
105:      * Create an Add Contacts Activity from a file. Valid file types are txt, csv, xls, xlsx
106:      * @param string $accessToken - Constant Contact OAuth2 access token
107:      * @param string $fileName - The name of the file (ie: contacts.csv)
108:      * @param string $fileLocation - The location of the file on the server, this method uses fopen()
109:      * @param string $lists - Comma separated list of ContactList id's to add the contacts to
110:      * @return Activity
111:      * @throws CtctException
112:      */
113:     public function createAddContactsActivityFromFile($accessToken, $fileName, $fileLocation, $lists)
114:     {
115:         $baseUrl = Config::get('endpoints.base_url') . Config::get('endpoints.add_contacts_activity');
116:         $request = parent::createBaseRequest($accessToken, "POST", $baseUrl);
117:         $request->setHeader("Content-Type", "multipart/form-data");
118: 
119:         $body = new PostBody();
120:         $body->setField("lists", $lists);
121:         $body->setField("file_name", $fileName);
122:         $body->addFile(new PostFile("data", fopen($fileLocation, 'r'), $fileName));
123:         $request->setBody($body);
124: 
125:         try {
126:             $response = parent::getClient()->send($request);
127:         } catch (ClientException $e) {
128:             throw parent::convertException($e);
129:         }
130: 
131:         return Activity::create($response->json());
132:     }
133: 
134:     /**
135:      * Create a clear lists activity. This removes all contacts on the selected lists while keeping
136:      * the list itself intact.
137:      * @param $accessToken - Constant Contact OAuth2 access token
138:      * @param array $lists - Array of list ID's to be cleared
139:      * @return Activity
140:      * @throws CtctException
141:      */
142:     public function addClearListsActivity($accessToken, Array $lists)
143:     {
144:         $baseUrl = Config::get('endpoints.base_url') . Config::get('endpoints.clear_lists_activity');
145:         $request = parent::createBaseRequest($accessToken, "POST", $baseUrl);
146:         $stream = Stream::factory(json_encode(array("lists" => $lists)));
147:         $request->setBody($stream);
148: 
149:         try {
150:             $response = parent::getClient()->send($request);
151:         } catch (ClientException $e) {
152:             throw parent::convertException($e);
153:         }
154: 
155:         return Activity::create($response->json());
156:     }
157: 
158:     /**
159:      * Create an Export Contacts Activity
160:      * @param string $accessToken - Constant Contact OAuth2 access token
161:      * @param ExportContacts $exportContacts
162:      * @return array - Array of all ActivitySummaryReports
163:      * @throws CtctException
164:      */
165:     public function addExportContactsActivity($accessToken, ExportContacts $exportContacts)
166:     {
167:         $baseUrl = Config::get('endpoints.base_url') . Config::get('endpoints.export_contacts_activity');
168: 
169:         $request = parent::createBaseRequest($accessToken, 'POST', $baseUrl);
170:         $stream = Stream::factory(json_encode($exportContacts));
171:         $request->setBody($stream);
172: 
173:         try {
174:             $response = parent::getClient()->send($request);
175:         } catch (ClientException $e) {
176:             throw parent::convertException($e);
177:         }
178: 
179:         return Activity::create($response->json());
180:     }
181: 
182:     /**
183:      * Create a Remove Contacts from Lists Activity
184:      * @param $accessToken - Constant Contact OAuth2 access token
185:      * @param array $emailAddresses - array of email addresses to remove
186:      * @param array $lists - array of list ID's to remove the provided email addresses from
187:      * @return Activity
188:      * @throws CtctException
189:      */
190:     public function addRemoveContactsFromListsActivity($accessToken, Array $emailAddresses, Array $lists)
191:     {
192:         $baseUrl = Config::get('endpoints.base_url') . Config::get('endpoints.remove_from_lists_activity');
193:         $request = parent::createBaseRequest($accessToken, "POST", $baseUrl);
194: 
195:         $payload = array(
196:             'import_data' => array(),
197:             'lists' => $lists
198:         );
199:         foreach($emailAddresses as $emailAddress) {
200:             $payload['import_data'][] = array('email_addresses' => array($emailAddress));
201:         }
202: 
203:         $stream = Stream::factory(json_encode($payload));
204:         $request->setBody($stream);
205: 
206:         try {
207:             $response = parent::getClient()->send($request);
208:         } catch (ClientException $e) {
209:             throw parent::convertException($e);
210:         }
211: 
212:         return Activity::create($response->json());
213:     }
214: 
215:     /**
216:      * Create a Remove Contacts Activity from a file. Valid file types are txt, csv, xls, xlsx
217:      * @param string $accessToken - Constant Contact OAuth2 access token
218:      * @param string $fileName - The name of the file (ie: contacts.csv)
219:      * @param string $fileLocation - The location of the file on the server, this method uses fopen()
220:      * @param string $lists - Comma separated list of ContactList id's to add the contacts to
221:      * @return Activity
222:      * @throws CtctException
223:      */
224:     public function addRemoveContactsFromListsActivityFromFile($accessToken, $fileName, $fileLocation, $lists)
225:     {
226:         $baseUrl = Config::get('endpoints.base_url') . Config::get('endpoints.remove_from_lists_activity');
227:         $request = parent::createBaseRequest($accessToken, "POST", $baseUrl);
228:         $request->setHeader("Content-Type", "multipart/form-data");
229: 
230:         $body = new PostBody();
231:         $body->setField("lists", $lists);
232:         $body->setField("file_name", $fileName);
233:         $body->addFile(new PostFile("data", fopen($fileLocation, 'r'), $fileName));
234:         $request->setBody($body);
235: 
236:         try {
237:             $response = parent::getClient()->send($request);
238:         } catch (ClientException $e) {
239:             throw parent::convertException($e);
240:         }
241: 
242:         return Activity::create($response->json());
243:     }
244: }
245: 
API documentation generated by ApiGen