Overview

Namespaces

  • Ctct
    • Auth
    • Components
      • Account
      • Activities
      • Contacts
      • EmailMarketing
      • Tracking
    • Exceptions
    • Services
  • PHP

Classes

  • AccountService
  • ActivityService
  • BaseService
  • CampaignScheduleService
  • CampaignTrackingService
  • ContactService
  • ContactTrackingService
  • EmailMarketingService
  • ListService
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: namespace Ctct\Services;
  3: 
  4: use Ctct\Util\Config;
  5: use Ctct\Util\RestClient;
  6: use Ctct\Components\Contacts\Contact;
  7: use Ctct\Components\ResultSet;
  8: 
  9: /**
 10:  * Performs all actions pertaining to Constant Contact Contacts
 11:  *
 12:  * @package Services
 13:  * @author ContactContact
 14:  */
 15: class ContactService extends BaseService
 16: {
 17: 
 18:     /**
 19:      * Get a ResultSet of contacts
 20:      * @param string $accessToken - Constant Contact OAuth2 access token
 21:      * @param array $params - array of query parameters to be appended to the url
 22:      * @return ResultSet
 23:      */
 24:     public function getContacts($accessToken, Array $params = null)
 25:     {
 26:         $baseUrl = Config::get('endpoints.base_url') . Config::get('endpoints.contacts');
 27:         $url = $this->buildUrl($baseUrl, $params);
 28: 
 29:         $response = parent::getRestClient()->get($url, parent::getHeaders($accessToken));
 30:         $body = json_decode($response->body, true);
 31:         $contacts = array();
 32:         foreach ($body['results'] as $contact) {
 33:             $contacts[] = Contact::create($contact);
 34:         }
 35:         return new ResultSet($contacts, $body['meta']);
 36:     }
 37:     
 38:     /**
 39:      * Get contact details for a specific contact
 40:      * @param string $accessToken - Constant Contact OAuth2 access token
 41:      * @param int $contactId - Unique contact id
 42:      * @return Contact
 43:      */
 44:     public function getContact($accessToken, $contactId)
 45:     {
 46:         $baseUrl = Config::get('endpoints.base_url') . sprintf(Config::get('endpoints.contact'), $contactId);
 47:         $url = $this->buildUrl($baseUrl);
 48:         $response = parent::getRestClient()->get($url, parent::getHeaders($accessToken));
 49:         return Contact::create(json_decode($response->body, true));
 50:     }
 51:     
 52:     /**
 53:      * Add a new contact to the Constant Contact account
 54:      * @param string $accessToken - Constant Contact OAuth2 access token
 55:      * @param Contact $contact - Contact to add
 56:      * @param array $params - query params to be appended to the request
 57:      * @return Contact
 58:      */
 59:     public function addContact($accessToken, Contact $contact, Array $params)
 60:     {
 61:         $baseUrl = Config::get('endpoints.base_url') . Config::get('endpoints.contacts');
 62:         $url = $this->buildUrl($baseUrl, $params);
 63:         $response = parent::getRestClient()->post($url, parent::getHeaders($accessToken), $contact->toJson());
 64:         return Contact::create(json_decode($response->body, true));
 65:     }
 66:     
 67:     /**
 68:      * Delete contact details for a specific contact
 69:      * @param string $accessToken - Constant Contact OAuth2 access token
 70:      * @param int $contactId - Unique contact id
 71:      * @return boolean
 72:      */
 73:     public function deleteContact($accessToken, $contactId)
 74:     {
 75:         $baseUrl = Config::get('endpoints.base_url') . sprintf(Config::get('endpoints.contact'), $contactId);
 76:         $url = $this->buildUrl($baseUrl);
 77:         $response = parent::getRestClient()->delete($url, parent::getHeaders($accessToken));
 78:         return ($response->info['http_code'] == 204) ? true : false;
 79:     }
 80:     
 81:     /**
 82:      * Delete a contact from all contact lists
 83:      * @param string $accessToken - Constant Contact OAuth2 access token
 84:      * @param int $contactId - Contact id to be removed from lists
 85:      * @return boolean
 86:      */
 87:     public function deleteContactFromLists($accessToken, $contactId)
 88:     {
 89:         $baseUrl = Config::get('endpoints.base_url') . sprintf(Config::get('endpoints.contact_lists'), $contactId);
 90:         $url = $this->buildUrl($baseUrl);
 91:         $response = parent::getRestClient()->delete($url, parent::getHeaders($accessToken));
 92:         return ($response->info['http_code'] == 204) ? true : false;
 93:     }
 94:     
 95:     /**
 96:      * Delete a contact from a specific contact list
 97:      * @param string $accessToken - Constant Contact OAuth2 access token
 98:      * @param int $contactId - Contact id to be removed
 99:      * @param int $listId - ContactList to remove the contact from
100:      * @return boolean
101:      */
102:     public function deleteContactFromList($accessToken, $contactId, $listId)
103:     {
104:         $baseUrl = Config::get('endpoints.base_url') .
105:             sprintf(Config::get('endpoints.contact_list'), $contactId, $listId);
106:         $url = $this->buildUrl($baseUrl);
107:         $response = parent::getRestClient()->delete($url, parent::getHeaders($accessToken));
108:         return ($response->info['http_code'] == 204) ? true : false;
109:     }
110:     
111:     /**
112:      * Update contact details for a specific contact
113:      * @param string $accessToken - Constant Contact OAuth2 access token
114:      * @param Contact $contact - Contact to be updated
115:      * @param array $params - query params to be appended to the request
116:      * @return Contact
117:      */
118:     public function updateContact($accessToken, Contact $contact, Array $params)
119:     {
120:         $baseUrl = Config::get('endpoints.base_url') . sprintf(Config::get('endpoints.contact'), $contact->id);
121:         $url = $this->buildUrl($baseUrl, $params);
122:         $response = parent::getRestClient()->put($url, parent::getHeaders($accessToken), $contact->toJson());
123:         return Contact::create(json_decode($response->body, true));
124:     }
125: }
126: 
Appconnect PHP SDK API documentation generated by ApiGen 2.8.0