/** * Update information of the account. * @param string $accessToken - Constant Contact OAuth2 Access Token * @param AccountInfo $accountInfo - Updated AccountInfo * @return AccountInfo */ public function updateAccountInfo($accessToken, AccountInfo $accountInfo) { $baseUrl = Config::get('endpoints.base_url') . Config::get('endpoints.account_info'); $url = $this->buildUrl($baseUrl); $response = parent::getRestClient()->put($url, parent::getHeaders($accessToken), $accountInfo->toJson()); return AccountInfo::create(json_decode($response->body, true)); }
/** * Get account info associated with an access token * @param string $accessToken - Constant Contact OAuth2 Access Token * @param array $params - array of query parameters/values to append to the request * @return AccountInfo */ public function getAccountInfo($accessToken, array $params) { $baseUrl = Config::get('endpoints.base_url') . sprintf(Config::get('endpoints.account_info')); $url = $this->buildUrl($baseUrl, $params); $response = parent::getRestClient()->get($url, parent::getHeaders($accessToken)); return AccountInfo::create(json_decode($response->body, true)); }
/** * Get an information about an access token * @param string $accessToken - Constant Contact OAuth2 access token * @return array * @throws \Ctct\Exceptions\CtctException */ public function getTokenInfo($accessToken) { $restClient = new RestClient(); $url = Config::get('auth.base_url') . Config::get('auth.token_info'); $response = $this->restClient->post($url, array(), "access_token=" . $accessToken); return json_decode($response->body, true); }
public function authorizationUrlProvider() { $requestParams = "&client_id=apiKey&redirect_uri=redirectUri"; $serverParams = "?response_type=" . Config::get('auth.response_type_code') . $requestParams; $clientParams = "?response_type=" . Config::get('auth.response_type_token') . $requestParams; return array(array(true, Config::get('auth.base_url') . Config::get('auth.authorization_endpoint') . $serverParams), array(false, Config::get('auth.base_url') . Config::get('auth.authorization_endpoint') . $clientParams)); }
/** * Update a specific email campaign * @param string $accessToken - Constant Contact OAuth2 access token * @param Campaign $campaign - Campaign to be updated * @return Campaign */ public function updateCampaign($accessToken, Campaign $campaign) { $baseUrl = Config::get('endpoints.base_url') . sprintf(Config::get('endpoints.campaign'), $campaign->id); $url = $this->buildUrl($baseUrl); $response = parent::getRestClient()->put($url, parent::getHeaders($accessToken), $campaign->toJson()); return Campaign::create(json_decode($response->body, true)); }
/** * Get all verified email addresses associated with an account * @param string $accessToken - Constant Contact OAuth2 Access Token * @param array $params - array of query parameters/values to append to the request * @return array of VerifiedEmailAddress */ public function getVerifiedEmailAddresses($accessToken, array $params) { $baseUrl = Config::get('endpoints.base_url') . sprintf(Config::get('endpoints.account_verified_addresses')); $url = $this->buildUrl($baseUrl, $params); $response = parent::getRestClient()->get($url, parent::getHeaders($accessToken)); $verifiedAddresses = array(); foreach (json_decode($response->body, true) as $verifiedAddress) { $verifiedAddresses[] = VerifiedEmailAddress::create($verifiedAddress); } return $verifiedAddresses; }
/** * Get folders from the Library * @param string $accessToken - Constant Contact OAuth2 Access Token * @param array $params - array of query parameters/values to append to the request * @return ResultSet */ public function getLibraryFolders($accessToken, array $params) { $baseUrl = Config::get('endpoints.base_url') . Config::get('endpoints.library_folders'); $url = $this->buildUrl($baseUrl, $params); $response = parent::getRestClient()->get($url, parent::getHeaders($accessToken)); $body = json_decode($response->body, true); $libraryFolders = array(); foreach ($body['results'] as $folder) { $libraryFolders[] = Folder::create($folder); } return new ResultSet($libraryFolders, $body['meta']); }
/** * Get an information about an access token * @param string $accessToken - Constant Contact OAuth2 access token * @return array * @throws CtctException */ public function getTokenInfo($accessToken) { $baseUrl = Config::get('auth.base_url') . Config::get('auth.token_info'); $request = $this->client->createRequest("POST", $baseUrl); $request->setQuery(array("access_token" => $accessToken)); try { $response = $this->client->send($request)->json(); } catch (ClientException $e) { throw $this->convertException($e); } return $response; }
/** * Get all contacts from an individual list * @param string $accessToken - Constant Contact OAuth2 access token * @param string $list_id - list id to retrieve contacts for * @param array $params - query params to attach to request * @return ResultSet */ public function getContactsFromList($accessToken, $list_id, $params = null) { $baseUrl = Config::get('endpoints.base_url') . sprintf(Config::get('endpoints.list_contacts'), $list_id); $url = $this->buildUrl($baseUrl, $params); $response = parent::getRestClient()->get($url, parent::getHeaders($accessToken)); $body = json_decode($response->body, true); $contacts = array(); foreach ($body['results'] as $contact) { $contacts[] = Contact::create($contact); } return new ResultSet($contacts, $body['meta']); }
/** * Add a contact list to set of lists associated with this email * @param mixed $contact_list - Contact list id, or ContactList object */ public function addList($contact_list) { if ($contact_list instanceof ContactList) { $list = $contact_list; } elseif (is_numeric($contact_list)) { $list = new ContactList($contact_list); } else { throw new IllegalArgumentException(sprintf(Config::get('errors.id_or_object'), 'ContactList')); } $this->sent_to_contact_lists[] = $list; }
/** * Get a summary of reporting data for a given campaign * @param string $accessToken - Constant Contact OAuth2 access token * @param int $campaign_id - Campaign id * @return TrackingSummary */ public function getSummary($accessToken, $campaign_id) { $baseUrl = Config::get('endpoints.base_url') . sprintf(Config::get('endpoints.campaign_tracking_summary'), $campaign_id); $url = $this->buildUrl($baseUrl); $response = parent::getRestClient()->get($url, parent::getHeaders($accessToken)); return TrackingSummary::create(json_decode($response->body, true)); }
/** * Update contact details for a specific contact * @param string $accessToken - Constant Contact OAuth2 access token * @param Contact $contact - Contact to be updated * @param array $params - associative array of query parameters and values to append to the request. * Allowed parameters include: * action_by - Whether the contact is taking the action, or the account owner. Must be one of * ACTION_BY_OWNER or ACTION_BY_VISITOR * @return Contact * @throws CtctException */ public function updateContact($accessToken, Contact $contact, array $params = array()) { $baseUrl = Config::get('endpoints.base_url') . sprintf(Config::get('endpoints.contact'), $contact->id); $request = parent::createBaseRequest($accessToken, 'PUT', $baseUrl); if ($params) { $query = $request->getQuery(); foreach ($params as $name => $value) { $query->add($name, $value); } } $stream = Stream::factory(json_encode($contact)); $request->setBody($stream); try { $response = parent::getClient()->send($request); } catch (ClientException $e) { throw parent::convertException($e); } return Contact::create($response->json()); }
/** * Update contact details for a specific contact * @param string $accessToken - Constant Contact OAuth2 access token * @param Contact $contact - Contact to be updated * @param boolean $actionByVisitor - is the action being taken by the visitor * @return Contact */ public function updateContact($accessToken, Contact $contact, $actionByVisitor = false) { $baseUrl = Config::get('endpoints.base_url') . sprintf(Config::get('endpoints.contact'), $contact->id); $params = array(); if ($actionByVisitor == true) { $params['action_by'] = "ACTION_BY_VISITOR"; } $url = $this->buildUrl($baseUrl, $params); $response = parent::getRestClient()->put($url, parent::getHeaders($accessToken), $contact->toJson()); return Contact::create(json_decode($response->body, true)); }
/** * Create a Remove Contacts Activity from a file. Valid file types are txt, csv, xls, xlsx * @param string $accessToken - Constant Contact OAuth2 access token * @param string $fileName - The name of the file (ie: contacts.csv) * @param string $fileLocation - The location of the file on the server, this method uses fopen() * @param string $lists - Comma separated list of ContactList id's to add the contacts to * @return Activity * @throws CtctException */ public function addRemoveContactsFromListsActivityFromFile($accessToken, $fileName, $fileLocation, $lists) { $baseUrl = Config::get('endpoints.base_url') . Config::get('endpoints.remove_from_lists_activity'); $request = parent::createBaseRequest($accessToken, "POST", $baseUrl); $request->setHeader("Content-Type", "multipart/form-data"); $body = new PostBody(); $body->setField("lists", $lists); $body->setField("file_name", $fileName); $body->addFile(new PostFile("data", fopen($fileLocation, 'r'), $fileName)); $request->setBody($body); try { $response = parent::getClient()->send($request); } catch (ClientException $e) { throw parent::convertException($e); } return Activity::create($response->json()); }
private function setUrls() { foreach ($this->cache_services as $key => $service) { $this->{$service}->baseUrl = Config::get('endpoints.base_url') . Config::get('endpoints.' . $key); } }
public function __construct(array $contacts, array $lists, array $columnNames = array()) { if (!empty($contacts)) { if ($contacts[0] instanceof AddContactsImportData) { $this->import_data = $contacts; } else { $msg = sprintf(Config::get('errors.id_or_object'), "AddContactsImportData"); throw new IllegalArgumentException($msg); } } $this->lists = $lists; if (empty($columnNames)) { $usedColumns[] = Config::get('activities_columns.email'); $contact = $contacts[0]; if (isset($contact->first_name)) { $usedColumns[] = Config::get('activities_columns.first_name'); } if (isset($contact->last_name)) { $usedColumns[] = Config::get('activities_columns.last_name'); } if (isset($contact->birthday_day)) { $usedColumns[] = Config::get('activities_columns.birthday_day'); } if (isset($contact->birthday_month)) { $usedColumns[] = Config::get('activities_columns.birthday_month'); } if (isset($contact->anniversary)) { $usedColumns[] = Config::get('activities_columns.anniversary'); } if (isset($contact->job_title)) { $usedColumns[] = Config::get('activities_columns.job_title'); } if (isset($contact->company_name)) { $usedColumns[] = Config::get('activities_columns.company_name'); } if (isset($contact->work_phone)) { $usedColumns[] = Config::get('activities_columns.work_phone'); } if (isset($contact->home_phone)) { $usedColumns[] = Config::get('activities_columns.home_phone'); } if (isset($contact->birthday_day)) { $usedColumns[] = Config::get('activities_columns.birthday_day'); } if (isset($contact->birthday_month)) { $usedColumns[] = Config::get('activities_columns.birthday_month'); } if (isset($contact->anniversary)) { $usedColumns[] = Config::get('activities_columns.anniversary'); } // Addresses if (!empty($contact->addresses)) { $address = $contact->addresses[0]; if (isset($address->line1)) { $usedColumns[] = Config::get('activities_columns.address1'); } if (isset($address->line2)) { $usedColumns[] = Config::get('activities_columns.address2'); } if (isset($address->line3)) { $usedColumns[] = Config::get('activities_columns.address3'); } if (isset($address->city)) { $usedColumns[] = Config::get('activities_columns.city'); } if (isset($address->state_code)) { $usedColumns[] = Config::get('activities_columns.state'); } if (isset($address->state_province)) { $usedColumns[] = Config::get('activities_columns.state_province'); } if (isset($address->country)) { $usedColumns[] = Config::get('activities_columns.country'); } if (isset($address->postal_code)) { $usedColumns[] = Config::get('activities_columns.postal_code'); } if (isset($address->sub_postal_code)) { $usedColumns[] = Config::get('activities_columns.sub_postal_code'); } } // Custom Fields if (!empty($contact->custom_fields)) { foreach ($contact->custom_fields as $customField) { if (strpos($customField->name, 'custom_field_') !== false) { $customFieldNumber = substr($customField->name, 13); $usedColumns[] = Config::get('activities_columns.custom_field_' . $customFieldNumber); } } } $this->column_names = $usedColumns; } else { $this->column_names = $columnNames; } }
/** * Helper function to return required headers for making an http request with constant contact * @param $accessToken - OAuth2 access token to be placed into the Authorization header * @return array - authorization headers */ private static function getHeaders($accessToken) { return array('User-Agent' => 'ConstantContact AppConnect PHP Library v' . Config::get('settings.version'), 'Content-Type' => 'application/json', 'Accept' => 'application/json', 'Authorization' => 'Bearer ' . $accessToken, 'x-ctct-request-source' => 'sdk.php' . Config::get('settings.version')); }
/** * Get the status of a File upload * @param string $accessToken - Constant Contact OAuth2 token * @param string $uploadStatusIds - Single ID or ID's of statuses to check, separated by commas (no spaces) * @return FileUploadStatus[] - Array of FileUploadStatus * @throws CtctException */ public function getFileUploadStatus($accessToken, $uploadStatusIds) { $baseUrl = Config::get('endpoints.base_url') . sprintf(Config::get('endpoints.library_file_upload_status'), $uploadStatusIds); $request = parent::createBaseRequest($accessToken, "GET", $baseUrl); try { $response = parent::getClient()->send($request); } catch (ClientException $e) { throw parent::convertException($e); } $fileUploadStatuses = array(); foreach ($response->json() as $fileUploadStatus) { $fileUploadStatuses[] = FileUploadStatus::create($fileUploadStatus); } return $fileUploadStatuses; }
/** * Returns the version header for the rest calls * @return string */ public static function getVersionHeader() { return 'x-ctct-request-source: sdk.php.' . Config::get('settings.version'); }
/** * Get an individual contact list * @param $accessToken - Constant Contact OAuth2 access token * @param $listId - list id * @return ContactList * @throws CtctException */ public function getList($accessToken, $listId) { $baseUrl = Config::get('endpoints.base_url') . sprintf(Config::get('endpoints.list'), $listId); $request = parent::createBaseRequest($accessToken, 'GET', $baseUrl); try { $response = parent::getClient()->send($request); } catch (ClientException $e) { throw parent::convertException($e); } return ContactList::create($response->json()); }
/** * Create an Remove Contacts Activity from a file. Valid file types are txt, csv, xls, xlsx * @param string $accessToken - Constant Contact OAuth2 access token * @param string $fileName - The name of the file (ie: contacts.csv) * @param string $contents - The contents of the file * @param string $lists - Comma separated list of ContactList id' to add the contacts too * @return \Ctct\Components\Activities\Activity */ public function addRemoveContactsFromListsActivityFromFile($accessToken, $fileName, $contents, $lists) { $eol = "\r\n"; $data = ''; $boundary = md5(time()); $data .= '--' . $boundary . $eol; $data .= 'Content-Disposition: form-data; name="file_name"' . $eol; $data .= 'Content-Type: text/plain' . $eol . $eol; $data .= $fileName . $eol; $data .= '--' . $boundary . $eol; $data .= 'Content-Disposition: form-data; name="lists"' . $eol; $data .= 'Content-Type: text/plain' . $eol . $eol; $data .= $lists . $eol; $data .= '--' . $boundary . $eol; $data .= 'Content-Disposition: form-data; name="data"' . $eol . $eol; $data .= $contents . $eol; $data .= "--" . $boundary . "--" . $eol; $headers = array("Authorization: Bearer {$accessToken}", "Content-Type: multipart/form-data; boundary={$boundary}"); $baseUrl = Config::get('endpoints.base_url') . Config::get('endpoints.remove_from_lists_activity'); $url = $this->buildUrl($baseUrl); $response = parent::getRestClient()->post($url, $headers, $data); return Activity::create(json_decode($response->body, true)); }
/** * Send a test send of a campaign * @param string $accessToken - Constant Contact OAuth2 access token * @param int $campaignId - Id of campaign to send test of * @param TestSend $test_send - Test send details * @return TestSend */ public function sendTest($accessToken, $campaignId, TestSend $test_send) { $baseUrl = Config::get('endpoints.base_url') . sprintf(Config::get('endpoints.campaign_test_sends'), $campaignId); $url = $this->buildUrl($baseUrl); $response = parent::getRestClient()->post($url, parent::getHeaders($accessToken), $test_send->toJson()); return TestSend::create(json_decode($response->body, true)); }
/** * Get a preview of an email campaign * @param string $accessToken - Constant Contact OAuth2 access token * @param int $campaignId - Valid campaign id * @return CampaignPreview * @throws CtctException */ public function getPreview($accessToken, $campaignId) { $baseUrl = Config::get('endpoints.base_url') . sprintf(Config::get('endpoints.campaign_preview'), $campaignId); $request = parent::createBaseRequest($accessToken, 'GET', $baseUrl); try { $response = parent::getClient()->send($request); } catch (ClientException $e) { throw parent::convertException($e); } return CampaignPreview::create($response->json()); }
/** * Send a test send of a campaign * @param string $accessToken - Constant Contact OAuth2 access token * @param int $campaignId - Id of campaign to send test of * @param TestSend $testSend - Test send details * @return TestSend * @throws CtctException */ public function sendTest($accessToken, $campaignId, TestSend $testSend) { $baseUrl = Config::get('endpoints.base_url') . sprintf(Config::get('endpoints.campaign_test_sends'), $campaignId); $request = parent::createBaseRequest($accessToken, 'POST', $baseUrl); $stream = Stream::factory(json_encode($testSend)); $request->setBody($stream); try { $response = parent::getClient()->send($request); } catch (ClientException $e) { throw parent::convertException($e); } return TestSend::create($response->json()); }
/** * Update contact details for a specific contact * @param string $accessToken - Constant Contact OAuth2 access token * @param Contact $contact - Contact to be updated * @param array $params - query params to be appended to the request * @return Contact */ public function updateContact($accessToken, Contact $contact, array $params = array()) { $baseUrl = Config::get('endpoints.base_url') . sprintf(Config::get('endpoints.contact'), $contact->id); $url = $this->buildUrl($baseUrl, $params); $response = parent::getRestClient()->put($url, parent::getHeaders($accessToken), $contact->toJson()); return Contact::create(json_decode($response->body, true)); }
/** * Get the id of object, or attempt to convert the argument to an int * @param mixed $item - object or a numeric value * @param string $class_name - class name to test the given object against * @throws IllegalArgumentException - if the item is not an instance of the class name given, or cannot be * converted to a numeric value * @return int */ private function getArgumentId($item, $class_name) { $id = null; if (is_numeric($item)) { $id = $item; } elseif (join('', array_slice(explode('\\', get_class($item)), -1)) == $class_name) { $id = $item->id; } else { throw new IllegalArgumentException(sprintf(Config::get('errors.id_or_object'), $class_name)); } return $id; }
/** * Update information of the account. * @param string $accessToken - Constant Contact OAuth2 Access Token * @param AccountInfo $accountInfo - Updated AccountInfo * @return AccountInfo * @throws CtctException */ public function updateAccountInfo($accessToken, AccountInfo $accountInfo) { $baseUrl = Config::get('endpoints.base_url') . Config::get('endpoints.account_info'); $request = parent::createBaseRequest($accessToken, 'PUT', $baseUrl); $stream = Stream::factory(json_encode($accountInfo)); $request->setBody($stream); try { $response = parent::getClient()->send($request); } catch (ClientException $e) { throw parent::convertException($e); } return AccountInfo::create($response->json()); }