Skip to content
This repository was archived by the owner on Dec 16, 2021. It is now read-only.
97 changes: 97 additions & 0 deletions src/Ctct/Components/Account/AccountInfo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php
namespace Ctct\Components\Account;

use Ctct\Components\Component;

/**
* Represents account info associated with an access token in Constant Contact
*
* @package Components
* @subpackage Account
* @author ewaltman
*/
class AccountInfo extends Component
{
/**
* Website associated with the account
* @var string
*/
public $website;

/**
* Name of organization associated with the account
* @var string
*/
public $organization_name;

/**
* Time zone used with the account
* @var string
*/
public $time_zone;

/**
* First name of the account user
* @var string
*/
public $first_name;

/**
* Last name of the account user
* @var string
*/
public $last_name;

/**
* Email address associated with the account
* @var string
* NOTE: the API returns 'email' field instead of 'email_address', but 'email_address' is used elsewhere
*/
public $email_address;

/**
* Phone number associated with the account
* @var string
*/
public $phone;

/**
* Country code associated with the account
* @var string
*/
public $country_code;

/**
* State code associated with the account
* @var string
*/
public $state_code;

/**
* Array of organization addresses associated with the account
* @var array
*/
public $organization_addresses;

/**
* Factory method to create an AccountInfo object from an array
* @param array $props - associative array of initial properties to set
* @return AccountInfo
*/
public static function create(array $props)
{
$accountInfo = new AccountInfo();
$accountInfo->website = parent::getValue($props, "website");
$accountInfo->organization_name = parent::getValue($props, "organization_name");
$accountInfo->time_zone = parent::getValue($props, "time_zone");
$accountInfo->first_name = parent::getValue($props, "first_name");
$accountInfo->last_name = parent::getValue($props, "last_name");
$accountInfo->email_address = parent::getValue($props, "email");
$accountInfo->phone = parent::getValue($props, "phone");
$accountInfo->country_code = parent::getValue($props, "country_code");
$accountInfo->state_code = parent::getValue($props, "state_code");
$accountInfo->organization_addresses = parent::getValue($props, "organization_addresses");

return $accountInfo;
}
}
10 changes: 10 additions & 0 deletions src/Ctct/ConstantContact.php
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,16 @@ public function getVerifiedEmailAddresses($accessToken, $status = null)
return $this->accountService->getVerifiedEmailAddresses($accessToken, $params);
}

/**
* Get details for account associated with an access token
* @param string $accessToken - Constant Contact OAuth2 access token
* @return AccountInfo object
*/
public function getAccountInfo($accessToken, array $params = array())
{
return $this->accountService->getAccountInfo($accessToken, $params);
}

/**
* Get a reporting summary for a Contact
* @param string $accessToken - Constant Contact OAuth2 access token
Expand Down
17 changes: 17 additions & 0 deletions src/Ctct/Services/AccountService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use Ctct\Util\Config;
use Ctct\Components\Account\VerifiedEmailAddress;
use Ctct\Components\Account\AccountInfo;

/**
* Performs all actions pertaining to scheduling Constant Contact Account's
Expand Down Expand Up @@ -33,4 +34,20 @@ public function getVerifiedEmailAddresses($accessToken, Array $params)

return $verifiedAddresses;
}

/**
* 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));
}
}
15 changes: 11 additions & 4 deletions src/Ctct/Util/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class Config

'base_url' => 'https://api.constantcontact.com/v2/',
'account_verified_addresses' => 'account/verifiedemailaddresses',
'account_info' => 'account/info',
'activity' => 'activities/%s',
'activities' => 'activities',
'export_contacts_activity' => 'activities/exportcontacts',
Expand Down Expand Up @@ -109,10 +110,16 @@ class Config
/**
* Errors to be returned for various exceptions
*/
'errors' => array(
'id_or_object' => 'Only an id or %s object are allowed for this method.'
)

'errors' => array(
'id_or_object' => 'Only an id or %s object are allowed for this method.'
),

/**
* Setting the version fo the application used in Rest Calls when setting the version header
*/
'settings' => array(
'version' => '1.2.0'
),
);

/**
Expand Down
15 changes: 13 additions & 2 deletions src/Ctct/Util/RestClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,14 @@ public function delete($url, array $headers = array())
*/
private static function httpRequest($url, $method, array $headers = array(), $data = null)
{
//adding the version header to the existing headers
$headers[] = self::getVersionHeader();

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_USERAGENT, "ConstantContact AppConnect PHP Library v1.1");
curl_setopt($curl, CURLOPT_USERAGENT, "ConstantContact AppConnect PHP Library v" . Config::get('settings.version'));
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
Expand All @@ -87,7 +90,7 @@ private static function httpRequest($url, $method, array $headers = array(), $da

// check if any errors were returned
$body = json_decode($response->body, true);
if (isset($body[0]) && array_key_exists('error_key', $body[0])) {
if ((isset($body[0]) && array_key_exists('error_key', $body[0])) || ($response->error !== false)) {
$ex = new CtctException($response->body);
$ex->setCurlInfo($response->info);
$ex->setErrors($body);
Expand All @@ -96,4 +99,12 @@ private static function httpRequest($url, $method, array $headers = array(), $da

return $response;
}

/**
* 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');
}
}