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         Constant Contact
 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 $contact_id - Unique contact id
 42:      * @return Contact
 43:      */
 44:     public function getContact($accessToken, $contact_id)
 45:     {
 46:         $baseUrl = Config::get('endpoints.base_url') . sprintf(Config::get('endpoints.contact'), $contact_id);
 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 boolean $actionByVisitor - is the action being taken by the visitor
 57:      * @return Contact
 58:      */
 59:     public function addContact($accessToken, Contact $contact, $actionByVisitor = false)
 60:     {
 61:         $baseUrl = Config::get('endpoints.base_url') . Config::get('endpoints.contacts');
 62:         $params = array();
 63: 
 64:         if ($actionByVisitor == true) {
 65:             $params['action_by'] = "ACTION_BY_VISITOR";
 66:         }
 67:         
 68:         $url = $this->buildUrl($baseUrl, $params);
 69:         $response = parent::getRestClient()->post($url, parent::getHeaders($accessToken), $contact->toJson());
 70:         return Contact::create(json_decode($response->body, true));
 71:     }
 72:     
 73:     /**
 74:      * Delete contact details for a specific contact
 75:      * @param string $accessToken - Constant Contact OAuth2 access token
 76:      * @param int $contact_id - Unique contact id
 77:      * @return boolean
 78:      */
 79:     public function deleteContact($accessToken, $contact_id)
 80:     {
 81:         $baseUrl = Config::get('endpoints.base_url') . sprintf(Config::get('endpoints.contact'), $contact_id);
 82:         $url = $this->buildUrl($baseUrl);
 83:         $response = parent::getRestClient()->delete($url, parent::getHeaders($accessToken));
 84:         return ($response->info['http_code'] == 204) ? true : false;
 85:     }
 86:     
 87:     /**
 88:      * Delete a contact from all contact lists
 89:      * @param string $accessToken - Constant Contact OAuth2 access token
 90:      * @param int $contact_id - Contact id to be removed from lists
 91:      * @return boolean
 92:      */
 93:     public function deleteContactFromLists($accessToken, $contact_id)
 94:     {
 95:         $baseUrl = Config::get('endpoints.base_url') . sprintf(Config::get('endpoints.contact_lists'), $contact_id);
 96:         $url = $this->buildUrl($baseUrl);
 97:         $response = parent::getRestClient()->delete($url, parent::getHeaders($accessToken));
 98:         return ($response->info['http_code'] == 204) ? true : false;
 99:     }
100:     
101:     /**
102:      * Delete a contact from a specific contact list
103:      * @param string $accessToken - Constant Contact OAuth2 access token
104:      * @param int $contact_id - Contact id to be removed
105:      * @param int $list_id - ContactList to remove the contact from
106:      * @return boolean
107:      */
108:     public function deleteContactFromList($accessToken, $contact_id, $list_id)
109:     {
110:         $baseUrl = Config::get('endpoints.base_url') .
111:             sprintf(Config::get('endpoints.contact_list'), $contact_id, $list_id);
112:         $url = $this->buildUrl($baseUrl);
113:         $response = parent::getRestClient()->delete($url, parent::getHeaders($accessToken));
114:         return ($response->info['http_code'] == 204) ? true : false;
115:     }
116:     
117:     /**
118:      * Update contact details for a specific contact
119:      * @param string $accessToken - Constant Contact OAuth2 access token
120:      * @param Contact $contact - Contact to be updated
121:      * @param boolean $actionByVisitor - is the action being taken by the visitor 
122:      * @return Contact
123:      */
124:     public function updateContact($accessToken, Contact $contact, $actionByVisitor = false)
125:     {
126:         $baseUrl = Config::get('endpoints.base_url') . sprintf(Config::get('endpoints.contact'), $contact->id);
127:         $params = array();
128:         if ($actionByVisitor == true) {
129:             $params['action_by'] = "ACTION_BY_VISITOR";
130:         }
131:         $url = $this->buildUrl($baseUrl, $params);
132:         $response = parent::getRestClient()->put($url, parent::getHeaders($accessToken), $contact->toJson());
133:         return Contact::create(json_decode($response->body, true));
134:     }
135: }
136: 
Appconnect PHP SDK API documentation generated by ApiGen 2.8.0