View source
<?php
class cc {
var $http_user_agent = 'justphp 3.0';
var $api_key = '7f704175-b47b-4abb-9afa-c4651be239d0';
var $api_username = '';
var $api_password = '';
var $api_url = 'https://api.constantcontact.com';
var $api_uri = '';
var $last_error = '';
var $action_type = 'ACTION_BY_CUSTOMER';
var $list_meta_data;
var $member_meta_data;
var $http_host;
var $http_port;
var $http_url_bits;
var $http_request_timeout = 120;
var $http_user;
var $http_pass;
var $http_content_type;
var $http_default_content_type = 'text/html';
var $http_response_code;
var $http_response;
var $http_response_body;
var $http_request;
var $http_method;
var $http_linebreak = "\r\n";
var $http_request_headers = array();
var $http_response_headers = array();
var $xml_known_encodings = array(
'UTF-8',
'US-ASCII',
'ISO-8859-1',
);
function cc($api_username, $api_password) {
$this->api_username = $api_username;
$this->api_password = $api_password;
$this->api_url .= '/ws/customers/' . rawurlencode($api_username) . '/';
$this->api_uri .= '/ws/customers/' . urlencode($api_username) . '/';
$this->http_user = $this->api_key . "%" . $api_username;
$this->http_pass = $api_password;
$this
->http_set_content_type($this->http_default_content_type);
}
function set_action_type($action_type = 'customer') {
$this->action_type = strtolower($action_type) == 'customer' ? 'ACTION_BY_CUSTOMER' : 'ACTION_BY_CONTACT';
}
function show_last_connection() {
print_r($this->http_request);
print_r($this->http_response);
}
function show_request() {
print_r($this->http_request);
}
function show_response() {
print_r($this->http_response);
}
function set_api_key($api_key) {
$this->api_key = $api_key;
}
function get_service_description() {
return $this
->load_url();
}
function get_all_lists($action = 'lists', $exclude = 3, $callback = '') {
$lists = $this
->get_lists($action, $exclude);
if (count($lists) > 0) {
if (isset($this->list_meta_data->next_page) and !is_null($this->list_meta_data->next_page)) {
while (!is_null($this->list_meta_data->next_page)) {
$lists = array_merge($lists, $this
->get_lists($this->list_meta_data->next_page, 0));
}
}
$callback = $callback ? $callback : array(
"cc",
"sort_lists",
);
if (is_array($lists)) {
usort($lists, $callback);
}
}
return $lists;
}
function sort_lists($a, $b) {
if (!isset($a['SortOrder'], $b['SortOrder']) || $a['SortOrder'] == $b['SortOrder']) {
return 0;
}
return $a['SortOrder'] < $b['SortOrder'] ? -1 : 1;
}
function get_lists($action = 'lists', $exclude = 3) {
$xml = $this
->load_url($action);
if (!$xml) {
return false;
}
$lists = array();
$_lists = isset($xml['feed']['entry']) ? $xml['feed']['entry'] : false;
if (isset($xml['feed']['link']['2_attr']['rel']) && $xml['feed']['link']['2_attr']['rel'] == 'first') {
$this->list_meta_data->first_page = $this
->get_id_from_link($xml['feed']['link']['2_attr']['href']);
$this->list_meta_data->current_page = $this
->get_id_from_link($xml['feed']['link']['3_attr']['href']);
$this->list_meta_data->next_page = '';
}
elseif (isset($xml['feed']['link']['2_attr']['rel']) && $xml['feed']['link']['2_attr']['rel'] == 'next') {
$this->list_meta_data->next_page = $this
->get_id_from_link($xml['feed']['link']['2_attr']['href']);
$this->list_meta_data->current_page = $this
->get_id_from_link($xml['feed']['link']['3_attr']['href']);
$this->list_meta_data->first_page = $this
->get_id_from_link($xml['feed']['link']['4_attr']['href']);
}
else {
$this->list_meta_data->next_page = NULL;
$this->list_meta_data->current_page = NULL;
$this->list_meta_data->first_page = NULL;
}
if (is_array($_lists) && count($_lists) > 3) {
if ($exclude) {
$_lists = array_slice($_lists, $exclude);
}
if (isset($_lists[0]['link_attr']['href'])) {
foreach ($_lists as $k => $v) {
$id = $this
->get_id_from_link($v['link_attr']['href']);
$list = array(
'id' => $id,
'Name' => $v['content']['ContactList']['Name'],
'ShortName' => $v['content']['ContactList']['ShortName'],
);
if (isset($v['content']['ContactList']['OptInDefault'])) {
$list['OptInDefault'] = $v['content']['ContactList']['OptInDefault'];
}
if (isset($v['content']['ContactList']['SortOrder'])) {
$list['SortOrder'] = $v['content']['ContactList']['SortOrder'];
}
$lists[] = $list;
}
}
else {
$id = $this
->get_id_from_link($_lists['link_attr']['href']);
$list = array(
'id' => $id,
'Name' => $_lists['content']['ContactList']['Name'],
'ShortName' => $_lists['content']['ContactList']['ShortName'],
);
if (isset($_lists['content']['ContactList']['OptInDefault'])) {
$list['OptInDefault'] = $_lists['content']['ContactList']['OptInDefault'];
}
if (isset($_lists['content']['ContactList']['SortOrder'])) {
$list['SortOrder'] = $_lists['content']['ContactList']['SortOrder'];
}
$lists[] = $list;
}
}
return $lists;
}
function get_list($listid) {
$xml = $this
->load_url("lists/{$listid}");
if (!$xml) {
return false;
}
$list = false;
$_list = isset($xml['entry']) ? $xml['entry'] : false;
if (is_array($_list)) {
$id = $this
->get_id_from_link($_list['link_attr']['href']);
$list = array(
'id' => $id,
'Name' => $_list['content']['ContactList']['Name'],
'ShortName' => $_list['content']['ContactList']['ShortName'],
'OptInDefault' => $_list['content']['ContactList']['OptInDefault'],
'SortOrder' => $_list['content']['ContactList']['SortOrder'],
);
}
return $list;
}
function delete_list($listid) {
$this
->http_set_content_type('text/html');
$this
->load_url("lists/{$listid}", 'delete', array(), 204);
if (intval($this->http_response_code) === 204) {
return true;
}
return false;
}
function update_list($id, $name, $default = "false", $sort_order = 99) {
$url = $this
->get_list_url($id);
$xml_data = '
<entry xmlns="http://www.w3.org/2005/Atom">
<id>' . $url . '</id>
<title type="text">' . $name . '</title>
<author />
<updated>2008-04-16T18:39:35.710Z</updated>
<content type="application/vnd.ctct+xml">
<ContactList xmlns="http://ws.constantcontact.com/ns/1.0/"
id="' . $url . '">
<OptInDefault>' . $default . '</OptInDefault>
<Name>' . $name . '</Name>
<ShortName>' . $name . '</ShortName>
<SortOrder>' . $sort_order . '</SortOrder>
</ContactList>
</content>
<link href="/ws/customers/' . $this->api_username . '/lists/' . $id . '" rel="update" />
</entry>
';
$this
->http_set_content_type('application/atom+xml');
$xml = $this
->load_url("lists/{$id}", 'put', $xml_data, 204);
if (intval($this->http_response_code) === 204) {
return true;
}
return false;
}
function create_list($name, $default = "false", $sort_order = 99) {
$xml_post = '<entry xmlns="http://www.w3.org/2005/Atom">
<id>data:,</id>
<title/>
<author/>
<updated>2008-04-16</updated>
<content type="application/vnd.ctct+xml">
<ContactList xmlns="http://ws.constantcontact.com/ns/1.0/">
<OptInDefault>' . $default . '</OptInDefault>
<Name>' . $name . '</Name>
<SortOrder>' . $sort_order . '</SortOrder>
</ContactList>
</content>
</entry>';
$this
->http_set_content_type('application/atom+xml');
$xml = $this
->load_url("lists", 'post', $xml_post, 201);
if ($xml) {
return true;
}
return false;
}
function get_list_url($id, $full_address = true) {
if ($full_address) {
$_url = str_replace('https:', 'http:', $this->api_url . "lists");
}
else {
$_url = $this->api_uri . "lists";
}
return "{$_url}/{$id}";
}
function get_http_api_url() {
return str_replace('https:', 'http:', $this->api_url);
}
function get_list_members($listid, $action = 'members') {
$xml = $this
->load_url("lists/{$listid}/{$action}");
if (!$xml) {
return false;
}
$contacts = array();
$_members = isset($xml['feed']['entry']) ? $xml['feed']['entry'] : false;
if (isset($xml['feed']['link']['2_attr']['rel']) && $xml['feed']['link']['2_attr']['rel'] == 'first') {
$this->member_meta_data->first_page = $this
->get_id_from_link($xml['feed']['link']['2_attr']['href']);
$this->member_meta_data->current_page = $this
->get_id_from_link($xml['feed']['link']['3_attr']['href']);
$this->member_meta_data->next_page = '';
}
elseif (isset($xml['feed']['link']['2_attr']['rel']) && $xml['feed']['link']['2_attr']['rel'] == 'next') {
$this->member_meta_data->next_page = $this
->get_id_from_link($xml['feed']['link']['2_attr']['href']);
$this->member_meta_data->current_page = $this
->get_id_from_link($xml['feed']['link']['3_attr']['href']);
$this->member_meta_data->first_page = $this
->get_id_from_link($xml['feed']['link']['4_attr']['href']);
}
else {
$this->member_meta_data->next_page = NULL;
$this->member_meta_data->current_page = NULL;
$this->member_meta_data->first_page = NULL;
}
if (is_array($_members)) {
if (isset($_members[0]['link_attr']['href'])) {
foreach ($_members as $k => $v) {
$EmailAddress = $v['content']['ContactListMember']['EmailAddress'];
$Name = $v['content']['ContactListMember']['Name'];
$id = $this
->get_id_from_link($v['link_attr']['href']);
$contact = array(
'id' => $id,
'EmailAddress' => $EmailAddress,
'Name' => $Name,
);
$contacts[] = $contact;
}
}
else {
$EmailAddress = $_members['content']['ContactListMember']['EmailAddress'];
$Name = $_members['content']['ContactListMember']['Name'];
$id = $this
->get_id_from_link($_members['link_attr']['href']);
$contact = array(
'id' => $id,
'EmailAddress' => $EmailAddress,
'Name' => $Name,
);
$contacts[] = $contact;
}
}
return $contacts;
}
function create_contact($email, $lists = array(), $additional_fields = array()) {
$lists_url = str_replace('https:', 'http:', $this->api_url . "lists");
$email = strtolower($email);
$xml_post = '
<entry xmlns="http://www.w3.org/2005/Atom">
<title type="text"> </title>
<updated>2008-07-23T14:21:06.407Z</updated>
<author></author>
<id>data:,none</id>
<summary type="text">Contact</summary>
<content type="application/vnd.ctct+xml">
<Contact xmlns="http://ws.constantcontact.com/ns/1.0/">
<EmailAddress>' . $email . '</EmailAddress>
<OptInSource>' . $this->action_type . '</OptInSource>
';
if ($additional_fields) {
foreach ($additional_fields as $field => $value) {
$xml_post .= "<{$field}>{$value}</{$field}>\n";
}
}
$xml_post .= '<ContactLists>';
if ($lists) {
if (is_array($lists)) {
foreach ($lists as $k => $id) {
$xml_post .= '<ContactList id="' . $lists_url . '/' . $id . '" />';
}
}
else {
$xml_post .= '<ContactList id="' . $lists_url . '/' . $lists . '" />';
}
}
$xml_post .= '</ContactLists>';
$xml_post .= '
</Contact>
</content>
</entry>';
$this
->http_set_content_type('application/atom+xml');
$xml = $this
->load_url("contacts", 'post', $xml_post, 201);
if ($xml) {
return true;
}
return false;
}
function update_contact($id, $email, $lists = array(), $additional_fields = array()) {
$_url = str_replace('https:', 'http:', $this->api_url . "contacts");
$url = "{$_url}/{$id}";
$email = strtolower($email);
$xml_data = '<entry xmlns="http://www.w3.org/2005/Atom">
<id>' . $url . '</id>
<title type="text">Contact: ' . $email . '</title>
<updated>2008-04-25T19:29:06.096Z</updated>
<author></author>
<content type="application/vnd.ctct+xml">
<Contact xmlns="http://ws.constantcontact.com/ns/1.0/" id="' . $url . '">
<EmailAddress>' . $email . '</EmailAddress>
<OptInSource>' . $this->action_type . '</OptInSource>
<OptInTime>2009-11-19T14:48:41.761Z</OptInTime>
';
if ($additional_fields) {
foreach ($additional_fields as $field => $value) {
$xml_data .= "<{$field}>{$value}</{$field}>\n";
}
}
$xml_data .= "<ContactLists>\n";
if ($lists) {
if (is_array($lists)) {
foreach ($lists as $k => $list_id) {
$xml_data .= '<ContactList id="' . $this
->get_list_url($list_id) . '"></ContactList>';
}
}
else {
$xml_data .= '<ContactList id="' . $this
->get_list_url($lists) . '"></ContactList>';
}
}
$xml_data .= "</ContactLists>\n";
$xml_data .= '
</Contact>
</content>
</entry>
';
$this
->http_set_content_type('application/atom+xml');
$xml = $this
->load_url("contacts/{$id}", 'put', $xml_data, 204);
if ($xml or is_array($xml)) {
return true;
}
return false;
}
function get_contacts($action = 'contacts') {
$xml = $this
->load_url($action);
if (!$xml) {
return false;
}
$contacts = array();
$_contacts = isset($xml['feed']['entry']) ? $xml['feed']['entry'] : false;
if (isset($xml['feed']['link']['2_attr']['rel'], $xml['feed']['link']['3_attr']['href']) && $xml['feed']['link']['2_attr']['rel'] == 'first') {
$this->contact_meta_data->first_page = $this
->get_id_from_link($xml['feed']['link']['2_attr']['href']);
$this->contact_meta_data->current_page = $this
->get_id_from_link($xml['feed']['link']['3_attr']['href']);
$this->contact_meta_data->next_page = '';
}
elseif (isset($xml['feed']['link']['2_attr']['rel'], $xml['feed']['link']['3_attr']['href'], $xml['feed']['link']['4_attr']['href']) && $xml['feed']['link']['2_attr']['rel'] == 'next') {
$this->contact_meta_data->next_page = $this
->get_id_from_link($xml['feed']['link']['2_attr']['href']);
$this->contact_meta_data->current_page = $this
->get_id_from_link($xml['feed']['link']['3_attr']['href']);
$this->contact_meta_data->first_page = $this
->get_id_from_link($xml['feed']['link']['4_attr']['href']);
}
else {
$this->contact_meta_data->next_page = NULL;
$this->contact_meta_data->current_page = NULL;
$this->contact_meta_data->first_page = NULL;
}
if (is_array($_contacts)) {
if (isset($_contacts[0]['link_attr']['href'])) {
foreach ($_contacts as $k => $v) {
$id = $this
->get_id_from_link($v['link_attr']['href']);
$contact = $v['content']['Contact'];
$contact['id'] = $id;
$contacts[] = $contact;
}
}
else {
$id = $this
->get_id_from_link($_contacts['link_attr']['href']);
$contact = $_contacts['content']['Contact'];
$contact['id'] = $id;
$contacts[] = $contact;
}
}
return $contacts;
}
function get_contact($id) {
$xml = $this
->load_url("contacts/{$id}");
if (!$xml) {
return false;
}
$contact = false;
$_contact = isset($xml['entry']) ? $xml['entry'] : false;
if (is_array($_contact)) {
$id = $this
->get_id_from_link($_contact['link_attr']['href']);
$contact = $_contact['content']['Contact'];
if (isset($_contact['content']['Contact']['ContactLists']['ContactList'])) {
$_lists = $_contact['content']['Contact']['ContactLists']['ContactList'];
unset($_lists['0_attr']);
unset($_lists['ContactList_attr']);
}
else {
$_lists = false;
}
$lists = array();
if (is_array($_lists) && count($_lists) > 0) {
unset($_lists['id']);
if (isset($_lists['link_attr']['href'])) {
$list_id = $this
->get_id_from_link($_lists['link_attr']['href']);
$lists[$list_id] = $list_id;
}
else {
foreach ($_lists as $k => $v) {
if (isset($v['link_attr']['href'])) {
$list_id = $this
->get_id_from_link($v['link_attr']['href']);
$lists[$list_id] = $list_id;
}
}
}
unset($contact['ContactLists']);
}
$contact['lists'] = $lists;
$contact['id'] = $id;
}
return $contact;
}
function query_contacts($email) {
$xml = $this
->load_url('contacts?email=' . strtolower(urlencode($email)));
if (!$xml) {
return false;
}
$contact = false;
$_contact = isset($xml['feed']['entry']) ? $xml['feed']['entry'] : false;
if (is_array($_contact)) {
$id = $this
->get_id_from_link($_contact['link_attr']['href']);
$contact = $_contact['content']['Contact'];
$contact['id'] = $id;
}
return $contact;
}
function delete_contact($id) {
$this
->http_set_content_type('text/html');
$this
->load_url("contacts/{$id}", 'delete', array(), 204);
if (intval($this->http_response_code) === 204) {
return true;
}
return false;
}
function download_activity_file($filename) {
$this
->http_set_content_type(NULL);
$this
->load_url("activities/{$filename}", 'get');
return $this->http_response_body;
}
function get_activity($id) {
$xml = $this
->load_url("activities/{$id}");
if (!$xml) {
return false;
}
$_activity = isset($xml['entry']) ? $xml['entry'] : false;
$activity = $_activity['content']['Activity'];
$activity['id'] = $id;
if (isset($activity['FileName'])) {
$activity['FileName'] = $this
->get_id_from_link($activity['FileName']);
}
return $activity;
}
function get_activities($action = 'activities') {
$xml = $this
->load_url($action);
if (!$xml) {
return false;
}
$activities = array();
$_activities = isset($xml['feed']['entry']) ? $xml['feed']['entry'] : false;
if (is_array($_activities)) {
if (isset($_activities[0]['link_attr']['href'])) {
foreach ($_activities as $k => $v) {
$id = $this
->get_id_from_link($v['link_attr']['href']);
$activity = $v['content']['Activity'];
$activity['id'] = $id;
$activities[] = $activity;
}
}
else {
$id = $this
->get_id_from_link($_activities['link_attr']['href']);
$activity = $_activities['content']['Activity'];
$activity['id'] = $id;
$activities[] = $activity;
}
}
return $activities;
}
function clear_contacts($lists) {
$params['activityType'] = 'CLEAR_CONTACTS_FROM_LISTS';
$lists_string = '';
if (is_array($lists)) {
foreach ($lists as $id) {
$params['lists'][] = $this
->get_list_url($id);
}
}
$this
->http_set_content_type('application/x-www-form-urlencoded');
$xml = $this
->load_url("activities", 'post', $params, 201);
if ($xml) {
return true;
}
return false;
}
function get_export_file_columns() {
$columns = array(
'FIRST NAME',
'MIDDLE NAME',
'LAST NAME',
'JOB TITLE',
'COMPANY NAME',
'WORK PHONE',
'HOME PHONE',
'ADDRESS LINE 1',
'ADDRESS LINE 2',
'ADDRESS LINE 3',
'CITY',
'STATE',
'STATE/PROVINCE (US/CANADA)',
'COUNTRY',
'POSTAL CODE',
'SUB POSTAL CODE',
);
$new = array();
foreach ($columns as $column) {
$new[$column] = $column;
}
return $new;
}
function export_contacts($list_id, $export_type = 'CSV', $columns = array(), $sort_by = 'DATE_DESC') {
if (!is_array($columns) || !count($columns)) {
$columns = $this
->get_export_file_columns();
}
$params['activityType'] = 'EXPORT_CONTACTS';
$params['fileType'] = $export_type;
$params['exportOptDate'] = 'true';
$params['exportOptSource'] = 'true';
$params['exportListName'] = 'false';
$params['sortBy'] = $sort_by;
$params['columns'] = $columns;
$params['listId'] = $this
->get_list_url($list_id);
$this
->http_set_content_type('application/x-www-form-urlencoded');
$xml = $this
->load_url("activities", 'post', $params, 201);
if ($xml) {
return true;
}
return false;
}
function create_contacts($contacts, $lists) {
$params['activityType'] = 'SV_ADD';
if (is_array($contacts) && count($contacts) > 0) {
$fieldnames = array_keys($contacts[0]);
$contacts = array_values($contacts);
$contacts_string = '';
foreach ($contacts as $k => $contact) {
foreach ($fieldnames as $k => $fieldname) {
if (isset($contact[$fieldname]) || is_null($contact[$fieldname])) {
$contacts_string .= $contact[$fieldname] . ",";
}
else {
$this->last_error = 'contacts array is not formatted correctly, please ensure all contact entries have the same fields and values';
return false;
}
}
$contacts_string .= "{$this->http_linebreak}";
}
$params['data'] = implode(',', $fieldnames) . "{$this->http_linebreak}" . $contacts_string;
}
elseif (file_exists($contacts) && is_readable($contacts)) {
$params['data'] = file_get_contents($contacts);
}
if (is_array($lists)) {
foreach ($lists as $id) {
$params['lists'][] = $this
->get_list_url($id);
}
}
$this
->http_set_content_type('application/x-www-form-urlencoded');
$xml = $this
->load_url("activities", 'post', $params, 201);
if ($xml) {
return true;
}
return false;
}
function get_campaigns($action = 'campaigns') {
$xml = $this
->load_url($action);
if (!$xml) {
return false;
}
$campaigns = array();
$_campaigns = isset($xml['feed']['entry']) ? $xml['feed']['entry'] : false;
if (is_array($_campaigns)) {
if (isset($_campaigns[0]['link_attr']['href'])) {
foreach ($_campaigns as $k => $v) {
$id = $this
->get_id_from_link($v['link_attr']['href']);
$campaign = $v['content']['Campaign'];
$campaign['id'] = $id;
$campaigns[] = $campaign;
}
}
else {
$id = $this
->get_id_from_link($_campaigns['link_attr']['href']);
$campaign = $_campaigns['content']['Campaign'];
$campaign['id'] = $id;
$campaigns[] = $campaign;
}
}
return $campaigns;
}
function get_campaign($id) {
$xml = $this
->load_url("campaigns/{$id}");
if (!$xml) {
return false;
}
$campaign = false;
$_campaign = isset($xml['entry']) ? $xml['entry'] : false;
if (is_array($_campaign)) {
$id = $this
->get_id_from_link($_campaign['link_attr']['href']);
$campaign = $_campaign['content']['Campaign'];
$campaign['id'] = $id;
}
return $campaign;
}
function delete_campaign($id) {
$this
->http_set_content_type('text/html');
$this
->load_url("campaigns/{$id}", 'delete', array(), 204);
if (intval($this->http_response_code) === 204) {
return true;
}
return false;
}
function create_campaign($title, $contact_lists = array(), $options = array()) {
$email = isset($options['EmailAddress']) ? $options['EmailAddress'] : '';
unset($options['EmailAddress']);
$xml_post = '<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns="http://www.w3.org/2005/Atom">
<link href="/ws/customers/' . $this->api_username . '/campaigns" rel="edit" />
<id>' . $this
->get_http_api_url() . 'campaigns</id>
<title type="text">' . $title . '</title>
<updated>2009-10-19T18:34:53.105Z</updated>
<author>
<name>Constant Contact</name>
</author>
<content type="application/vnd.ctct+xml">
<Campaign xmlns="http://ws.constantcontact.com/ns/1.0/"
id="' . $this
->get_http_api_url() . 'campaigns/1100546096289">
<Name>' . $title . '</Name>
<Status>Draft</Status>
<Date>2009-10-19T18:34:53.105Z</Date>
';
if (!is_array($options)) {
trigger_error('Third argument to create_campaign() should be an array', E_USER_ERROR);
}
foreach ($options as $fieldname => $fieldvalue) {
if (isset($options[$fieldname])) {
$xml_post .= "<{$fieldname}>{$fieldvalue}</{$fieldname}>\n";
}
}
if (is_array($contact_lists)) {
$xml_post .= '<ContactLists>';
foreach ($contact_lists as $id) {
$xml_post .= '
<ContactList id="' . $this
->get_list_url($id) . '">
<link xmlns="http://www.w3.org/2005/Atom" href="' . $this
->get_list_url($id, 0) . '" rel="self" />
</ContactList>
';
}
$xml_post .= '</ContactLists>';
}
if ($email) {
$email = $this
->get_emails($email);
if (!isset($email['id'])) {
$this->last_error = 'Invalid Email Address, the email address must exist in your constant contact account to be able to send an email from this address';
return false;
}
$xml_post .= '
<FromEmail>
<Email id="' . $this
->get_http_api_url() . 'settings/emailaddresses/' . $email['id'] . '">
<link xmlns="http://www.w3.org/2005/Atom" href="' . $this->api_uri . 'settings/emailaddresses/' . $email['id'] . '" rel="self" />
</Email>
<EmailAddress>' . $email['EmailAddress'] . '</EmailAddress>
</FromEmail>
<ReplyToEmail>
<Email id="' . $this
->get_http_api_url() . 'settings/emailaddresses/' . $email['id'] . '">
<link xmlns="http://www.w3.org/2005/Atom" href="' . $this->api_uri . 'settings/emailaddresses/' . $email['id'] . '" rel="self" />
</Email>
<EmailAddress>' . $email['EmailAddress'] . '</EmailAddress>
</ReplyToEmail>
';
}
$xml_post .= '
</Campaign>
</content>
<source>
<id>' . $this
->get_http_api_url() . 'campaigns</id>
<title type="text">Campaigns for customer: ' . $this->api_username . '</title>
<link href="campaigns" />
<link href="campaigns" rel="self" />
<author>
<name>' . $this->api_username . '</name>
</author>
<updated>2009-10-19T19:36:12.622Z</updated>
</source>
</entry>';
$this
->http_set_content_type('application/atom+xml');
$xml = $this
->load_url("campaigns", 'post', $xml_post, 201);
if ($xml) {
return true;
}
return false;
}
function query_campaigns($status = 'SENT') {
$xml = $this
->load_url('campaigns?status=' . urlencode($status));
if (!$xml) {
return false;
}
$campaigns = array();
$_campaigns = isset($xml['feed']['entry']) ? $xml['feed']['entry'] : false;
if (is_array($_campaigns)) {
if (isset($_campaigns[0]['link_attr']['href'])) {
foreach ($_campaigns as $k => $v) {
$id = $this
->get_id_from_link($v['link_attr']['href']);
$campaign = $v['content']['Campaign'];
$campaign['id'] = $id;
$campaigns[] = $campaign;
}
}
else {
$id = $this
->get_id_from_link($_campaigns['link_attr']['href']);
$campaign = $_campaigns['content']['Campaign'];
$campaign['id'] = $id;
$campaigns[] = $campaign;
}
}
return $campaigns;
}
function get_emails($return_email = '') {
$xml = $this
->load_url("settings/emailaddresses");
if (!$xml) {
return false;
}
$emails = array();
$_emails = isset($xml['feed']['entry']) ? $xml['feed']['entry'] : false;
if (is_array($_emails)) {
if (isset($_emails[0]['link_attr']['href'])) {
foreach ($_emails as $k => $v) {
$id = $this
->get_id_from_link($v['link_attr']['href']);
$email = $v['content']['Email'];
$email['id'] = $id;
if ($return_email && $return_email == $v['content']['Email']['EmailAddress']) {
return $email;
}
$emails[] = $email;
}
}
else {
$id = $this
->get_id_from_link($_emails['link_attr']['href']);
$email = $_emails['content']['Email'];
$email['id'] = $id;
$emails[] = $email;
if ($return_email && $return_email == $_emails['content']['Email']['EmailAddress']) {
return $email;
}
}
}
return $emails;
}
function convert_timestamp($timestamp) {
$timestamp_bits = explode('T', $timestamp);
if (isset($timestamp_bits[0], $timestamp_bits[0])) {
$date_bits = explode('-', $timestamp_bits[0]);
$time_bits = explode(':', $timestamp_bits[1]);
$year = $date_bits[0];
$month = $date_bits[1];
$day = $date_bits[2];
$hour = $time_bits[0];
$minute = $time_bits[1];
$second = $time_bits[2];
return mktime($hour, $minute, 0, $month, $day, $year);
}
return false;
}
function get_id_from_link($link) {
$link_bits = explode('/', $link);
return $link_bits[count($link_bits) - 1];
}
function xml_to_array($contents, $get_attributes = 1, $priority = 'tag') {
if (!$contents) {
return array();
}
if (!function_exists('xml_parser_create')) {
trigger_error("XML not supported: " . "http://www.php.net/manual/en/ref.xml.php", E_USER_ERROR);
return array();
}
$output_encoding = 'ISO-8859-1';
$input_encoding = NULL;
$detect_encoding = true;
list($parser, $source) = $this
->xml_create_parser($contents, $output_encoding, $input_encoding, $detect_encoding);
if (!is_resource($parser)) {
trigger_error("Failed to create an instance of PHP's XML parser. " . "http://www.php.net/manual/en/ref.xml.php", E_USER_ERROR);
}
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
xml_parse_into_struct($parser, trim($contents), $xml_values);
xml_parser_free($parser);
if (!$xml_values) {
return;
}
$xml_array = array();
$parents = array();
$opened_tags = array();
$arr = array();
$current =& $xml_array;
$repeated_tag_index = array();
foreach ($xml_values as $data) {
unset($attributes, $value);
extract($data);
$result = array();
$attributes_data = array();
if (isset($value)) {
if ($priority == 'tag') {
$result = $value;
}
else {
$result['value'] = $value;
}
}
if (isset($attributes) and $get_attributes) {
foreach ($attributes as $attr => $val) {
if ($priority == 'tag') {
$attributes_data[$attr] = $val;
}
else {
$result['attr'][$attr] = $val;
}
}
}
if ($type == "open") {
$parent[$level - 1] =& $current;
if (!is_array($current) or !in_array($tag, array_keys($current))) {
$current[$tag] = $result;
if ($attributes_data) {
$current[$tag . '_attr'] = $attributes_data;
}
$repeated_tag_index[$tag . '_' . $level] = 1;
$current =& $current[$tag];
}
else {
if (isset($current[$tag][0])) {
$current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result;
$repeated_tag_index[$tag . '_' . $level]++;
}
else {
$current[$tag] = array(
$current[$tag],
$result,
);
$repeated_tag_index[$tag . '_' . $level] = 2;
if (isset($current[$tag . '_attr'])) {
$current[$tag]['0_attr'] = $current[$tag . '_attr'];
unset($current[$tag . '_attr']);
}
}
$last_item_index = $repeated_tag_index[$tag . '_' . $level] - 1;
$current =& $current[$tag][$last_item_index];
}
}
elseif ($type == "complete") {
if (!isset($current[$tag])) {
$current[$tag] = $result;
$repeated_tag_index[$tag . '_' . $level] = 1;
if ($priority == 'tag' and $attributes_data) {
$current[$tag . '_attr'] = $attributes_data;
}
}
else {
if (isset($current[$tag][0]) and is_array($current[$tag])) {
$current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result;
if ($priority == 'tag' and $get_attributes and $attributes_data) {
$current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data;
}
$repeated_tag_index[$tag . '_' . $level]++;
}
else {
$current[$tag] = array(
$current[$tag],
$result,
);
$repeated_tag_index[$tag . '_' . $level] = 1;
if ($priority == 'tag' and $get_attributes) {
if (isset($current[$tag . '_attr'])) {
$current[$tag]['0_attr'] = $current[$tag . '_attr'];
unset($current[$tag . '_attr']);
}
if ($attributes_data) {
$current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data;
}
}
$repeated_tag_index[$tag . '_' . $level]++;
}
}
}
elseif ($type == 'close') {
$current =& $parent[$level - 1];
}
}
return $xml_array;
}
function xml_create_parser($source, $out_enc, $in_enc, $detect) {
if (substr(phpversion(), 0, 1) == 5) {
$parser = $this
->xml_php5_create_parser($in_enc, $detect);
}
else {
list($parser, $source) = $this
->xml_php4_create_parser($source, $in_enc, $detect);
}
if ($out_enc) {
$this->xml_encoding = $out_enc;
xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, $out_enc);
}
return array(
$parser,
$source,
);
}
function xml_php5_create_parser($in_enc, $detect) {
if (!$detect && $in_enc) {
return xml_parser_create($in_enc);
}
else {
return xml_parser_create('');
}
}
function xml_php4_create_parser($source, $in_enc, $detect) {
if (!$detect) {
return array(
xml_parser_create($in_enc),
$source,
);
}
if (!$in_enc) {
if (preg_match('/<?xml.*encoding=[\'"](.*?)[\'"].*?>/m', $source, $m)) {
$in_enc = strtoupper($m[1]);
$this->xml_source_encoding = $in_enc;
}
else {
$in_enc = 'UTF-8';
}
}
if ($this
->xml_known_encoding($in_enc)) {
return array(
xml_parser_create($in_enc),
$source,
);
}
if (function_exists('iconv')) {
$encoded_source = iconv($in_enc, 'UTF-8', $source);
if ($encoded_source) {
return array(
xml_parser_create('UTF-8'),
$encoded_source,
);
}
}
if (function_exists('mb_convert_encoding')) {
$encoded_source = mb_convert_encoding($source, 'UTF-8', $in_enc);
if ($encoded_source) {
return array(
xml_parser_create('UTF-8'),
$encoded_source,
);
}
}
trigger_error("Feed is in an unsupported character encoding. ({$in_enc}) " . "You may see strange artifacts, and mangled characters.", E_USER_ERROR);
}
function xml_known_encoding($enc) {
$enc = strtoupper($enc);
if (in_array($enc, $this->xml_known_encodings)) {
return $enc;
}
else {
return false;
}
}
function load_url($action = '', $method = 'get', $params = array(), $expected_http_code = 200) {
$this
->http_send($this->api_url . $action, $method, $params);
if (intval($expected_http_code) === $this->http_response_code) {
if ($this->http_content_type) {
return $this
->xml_to_array($this->http_response_body);
}
else {
return $this->http_response_body;
}
}
else {
$this->last_error = "Invalid status code {$this->http_response_code}";
return false;
}
}
function http_set_content_type($content_type) {
$this->http_content_type = $content_type;
}
function http_send($the_url, $method, $params = array()) {
$this->http_response = '';
$this->http_response_code = '';
$this->http_method = $method;
$method = strtoupper($method);
$ch = curl_init($the_url);
curl_setopt($ch, CURLOPT_USERAGENT, $this->http_user_agent);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, $this->http_user . ':' . $this->http_pass);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-Type: {$this->http_content_type}",
));
curl_setopt($ch, CURLOPT_TIMEOUT, $this->http_request_timeout);
if ($method == 'POST' or $method == 'PUT' and count($params) > 0) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
}
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
$this->http_response_body = curl_exec($ch);
$this->http_response_info = curl_getinfo($ch);
$this->http_response_error = curl_error($ch);
$this->http_response_code = $this->http_response_info['http_code'];
curl_close($ch);
}
function http_get_response_code_error($code) {
$errors = array(
200 => 'Success - The request was successful',
201 => 'Created (Success) - The request was successful. The requested object/resource was created.',
400 => 'Invalid Request - There are many possible causes for this error, but most commonly there is a problem with the structure or content of XML your application provided. Carefully review your XML. One simple test approach is to perform a GET on a URI and use the GET response as an input to a PUT for the same resource. With minor modifications, the input can be used for a POST as well.',
401 => 'Unauthorized - This is an authentication problem. Primary reason is that the API call has either not provided a valid API Key, Account Owner Name and Associated Password or the API call attempted to access a resource (URI) which does not match the same as the Account Owner provided in the login credientials.',
404 => 'URL Not Found - The URI which was provided was incorrect. Compare the URI you provided with the documented URIs. Start here.',
409 => 'Conflict - There is a problem with the action you are trying to perform. Commonly, you are trying to "Create" (POST) a resource which already exists such as a Contact List or Email Address that already exists. In general, if a resource already exists, an application can "Update" the resource with a "PUT" request for that resource.',
415 => 'Unsupported Media Type - The Media Type (Content Type) of the data you are sending does not match the expected Content Type for the specific action you are performing on the specific Resource you are acting on. Often this is due to an error in the content-type you define for your HTTP invocation (GET, PUT, POST). You will also get this error message if you are invoking a method (PUT, POST, DELETE) which is not supported for the Resource (URI) you are referencing.
To understand which methods are supported for each resource, and which content-type is expected, see the documentation for that Resource.',
500 => 'Server Error',
);
if (array_key_exists($code, $errors)) {
return $errors[$code];
}
return '';
}
}