Created
June 9, 2015 22:38
-
-
Save jrtaylor-com/89c7afc19639c5e3c212 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Utilize Constant Contact API v2 endpoints | |
*/ | |
class ConstantContactV2 { | |
/** | |
* Username. | |
* | |
* @var string | |
*/ | |
protected $username; | |
/** | |
* Password. | |
* | |
* @var string | |
*/ | |
protected $password; | |
/** | |
* API key. | |
* | |
* @var string | |
*/ | |
protected $api_key; | |
/** | |
* Base URL for posting/retrieving data from Constant Contact. | |
* | |
* @var string | |
*/ | |
protected $base_url; | |
/** | |
* Determines the action is being performed by the user. | |
* See: http://developer.constantcontact.com/docs/contacts-api/contacts-index.html#opt_in | |
* @var boolean | |
*/ | |
protected $opt_in; | |
/** | |
* Used in curl requests | |
* | |
* @var array | |
*/ | |
protected $headers; | |
/** | |
* Create a new instance. | |
* | |
* @param Array $config (Array of credentials for accessing Constant Contact API and lists) | |
* @return object | |
*/ | |
public static function factory($config = array()) { | |
return new ConstantContactV2($config); | |
} | |
/** | |
* Constructor | |
* | |
* @param Array $config (Array of credentials for accessing Constant Contact API and lists) | |
*/ | |
public function __construct($config = array()) { | |
// Init vars | |
$this->username = $config['username']; | |
$this->password = $config['password']; | |
$this->api_key = $config['api_key']; | |
$this->access_token = $config['access_token']; | |
$this->opt_in = (isset($config['opt_in'])) ? $config['opt_in'] : FALSE; | |
$this->timeout = ($config['timeout']) ? $config['timeout'] : 30; | |
$this->debug = (isset($config['debug'])) ? $config['debug'] : FALSE; | |
// Set base URL | |
$this->base_url = 'https://api.constantcontact.com/v2/'; | |
// Headers | |
$this->headers = array( | |
'Content-Type: application/json', | |
'Accept: application/json', | |
'Authorization: Bearer ' . $this->access_token | |
); | |
} | |
/** | |
* Send cURL request | |
* | |
* @param String $url | |
* @param String $type default: GET (GET, POST, PUT, DELETE) | |
* @param array $post_vars (Request defaults to GET if this value is empty) | |
* @return response | |
*/ | |
public function request($url, $type = 'GET', $post_vars = array()) { | |
$curl = curl_init($url); | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); | |
curl_setopt($curl, CURLOPT_HTTPHEADER, $this->headers); | |
if ($type == 'POST') { | |
curl_setopt($curl, CURLOPT_POST, TRUE); | |
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($post_vars)); | |
} elseif ($type == 'PUT') { | |
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT'); | |
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($post_vars)); | |
} | |
$response = curl_exec($curl); | |
curl_close($curl); | |
return $response; | |
} | |
/** | |
* Get an existing contact based on their email address | |
* | |
* @param String $email | |
* @return array | |
*/ | |
public function getContactByEmail($email) { | |
// Init return | |
$contact = FALSE; | |
// Send request | |
if (!$response = $this->request($this->base_url . 'contacts?email=' . $email . '&status=ALL&limit=1&api_key=' . $this->api_key)) { | |
return FALSE; | |
} | |
$response = json_decode($response, TRUE); | |
if (!empty($response['results'][0])) { | |
$contact = $response['results'][0]; | |
} | |
return $contact; | |
} | |
/** | |
* Add a contact | |
* | |
* @param Array $contact (contact data) | |
* @param String $originated_by (whether the request was originated by customer or contact) | |
* @param String $single_list (If list name is specified, contact will only be added to that list) | |
*/ | |
public function addOrUpdateContact($contact, $originated_by = 'customer', $single_list = '') { | |
// Setup required vars | |
$post_vars = array( | |
'email_addresses' => array(), | |
'lists' => array() | |
); | |
if (empty($contact['email_address']) or empty($single_list)) { | |
$missing = (empty($contact['email_address'])) ? 'email_address' : 'list'; | |
return array('error', 'Missing ' . $missing); | |
} | |
// Make sure list exists and grab ID | |
if (!$list_id = $this->getListIdByName($single_list)) { | |
return array('error', 'Bad list name'); | |
} | |
// Add the values to request | |
$post_vars['email_addresses'][] = array('email_address' => $contact['email_address']); | |
$post_vars['lists'][] = array('id' => $list_id); | |
if ($existing_contact = $this->getContactByEmail($contact['email_address'])) { | |
$contact['id'] = $existing_contact['id']; | |
} | |
foreach ((array)$contact as $k => $v) { | |
if ($k == 'email_address') { | |
continue; | |
} | |
$post_vars[$k] = $v; | |
} | |
$originated_by = ($originated_by == 'contact') ? 'ACTION_BY_VISITOR' : 'ACTION_BY_OWNER'; | |
$url = $this->base_url . 'contacts' . ((!empty($contact['id'])) ? '/' . $contact['id'] : '') . '?action_by=' . $originated_by . '&api_key=' . $this->api_key; | |
// Make the request | |
$request_type = (empty($contact['id'])) ? 'POST' : 'PUT'; | |
$response = $this->request($url, $request_type, $post_vars); | |
if ($response === FALSE) { | |
return array('error', 'Bad response'); | |
} | |
return $response; | |
} | |
/** | |
* Get the list ID based on supplied list name | |
* | |
* @param String $list_name | |
* @return int | |
*/ | |
public function getListIdByName($list_name) { | |
// Init return | |
$list_id = FALSE; | |
// Send request | |
$response = $this->request($this->base_url . 'lists?api_key=' . $this->api_key); | |
if ($response === FALSE) { | |
return FALSE; | |
} | |
$response = json_decode($response, TRUE); | |
if (!empty($response)) { | |
foreach ((array)$response as $list) { | |
if ($list['name'] == $list_name) { | |
$list_id = $list['id']; | |
break; | |
} | |
} | |
} | |
return $list_id; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment