Skip to content
This repository was archived by the owner on Dec 16, 2021. It is now read-only.

Commit 087a129

Browse files
committed
Merge pull request #68 from constantcontact/development
Updating Master with changes to Dev. Account Info changes and x-ctct-request-source header.
2 parents 096bc84 + d8828e3 commit 087a129

File tree

5 files changed

+148
-6
lines changed

5 files changed

+148
-6
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
namespace Ctct\Components\Account;
3+
4+
use Ctct\Components\Component;
5+
6+
/**
7+
* Represents account info associated with an access token in Constant Contact
8+
*
9+
* @package Components
10+
* @subpackage Account
11+
* @author ewaltman
12+
*/
13+
class AccountInfo extends Component
14+
{
15+
/**
16+
* Website associated with the account
17+
* @var string
18+
*/
19+
public $website;
20+
21+
/**
22+
* Name of organization associated with the account
23+
* @var string
24+
*/
25+
public $organization_name;
26+
27+
/**
28+
* Time zone used with the account
29+
* @var string
30+
*/
31+
public $time_zone;
32+
33+
/**
34+
* First name of the account user
35+
* @var string
36+
*/
37+
public $first_name;
38+
39+
/**
40+
* Last name of the account user
41+
* @var string
42+
*/
43+
public $last_name;
44+
45+
/**
46+
* Email address associated with the account
47+
* @var string
48+
* NOTE: the API returns 'email' field instead of 'email_address', but 'email_address' is used elsewhere
49+
*/
50+
public $email_address;
51+
52+
/**
53+
* Phone number associated with the account
54+
* @var string
55+
*/
56+
public $phone;
57+
58+
/**
59+
* Country code associated with the account
60+
* @var string
61+
*/
62+
public $country_code;
63+
64+
/**
65+
* State code associated with the account
66+
* @var string
67+
*/
68+
public $state_code;
69+
70+
/**
71+
* Array of organization addresses associated with the account
72+
* @var array
73+
*/
74+
public $organization_addresses;
75+
76+
/**
77+
* Factory method to create an AccountInfo object from an array
78+
* @param array $props - associative array of initial properties to set
79+
* @return AccountInfo
80+
*/
81+
public static function create(array $props)
82+
{
83+
$accountInfo = new AccountInfo();
84+
$accountInfo->website = parent::getValue($props, "website");
85+
$accountInfo->organization_name = parent::getValue($props, "organization_name");
86+
$accountInfo->time_zone = parent::getValue($props, "time_zone");
87+
$accountInfo->first_name = parent::getValue($props, "first_name");
88+
$accountInfo->last_name = parent::getValue($props, "last_name");
89+
$accountInfo->email_address = parent::getValue($props, "email");
90+
$accountInfo->phone = parent::getValue($props, "phone");
91+
$accountInfo->country_code = parent::getValue($props, "country_code");
92+
$accountInfo->state_code = parent::getValue($props, "state_code");
93+
$accountInfo->organization_addresses = parent::getValue($props, "organization_addresses");
94+
95+
return $accountInfo;
96+
}
97+
}

src/Ctct/ConstantContact.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,16 @@ public function getVerifiedEmailAddresses($accessToken, $status = null)
683683
return $this->accountService->getVerifiedEmailAddresses($accessToken, $params);
684684
}
685685

686+
/**
687+
* Get details for account associated with an access token
688+
* @param string $accessToken - Constant Contact OAuth2 access token
689+
* @return AccountInfo object
690+
*/
691+
public function getAccountInfo($accessToken, array $params = array())
692+
{
693+
return $this->accountService->getAccountInfo($accessToken, $params);
694+
}
695+
686696
/**
687697
* Get a reporting summary for a Contact
688698
* @param string $accessToken - Constant Contact OAuth2 access token

src/Ctct/Services/AccountService.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
use Ctct\Util\Config;
55
use Ctct\Components\Account\VerifiedEmailAddress;
6+
use Ctct\Components\Account\AccountInfo;
67

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

3435
return $verifiedAddresses;
3536
}
37+
38+
/**
39+
* Get account info associated with an access token
40+
* @param string $accessToken - Constant Contact OAuth2 Access Token
41+
* @param array $params - array of query parameters/values to append to the request
42+
* @return AccountInfo
43+
*/
44+
public function getAccountInfo($accessToken, Array $params)
45+
{
46+
$baseUrl = Config::get('endpoints.base_url')
47+
. sprintf(Config::get('endpoints.account_info'));
48+
49+
$url = $this->buildUrl($baseUrl, $params);
50+
$response = parent::getRestClient()->get($url, parent::getHeaders($accessToken));
51+
return AccountInfo::create(json_decode($response->body, true));
52+
}
3653
}

src/Ctct/Util/Config.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class Config
2121

2222
'base_url' => 'https://api.constantcontact.com/v2/',
2323
'account_verified_addresses' => 'account/verifiedemailaddresses',
24+
'account_info' => 'account/info',
2425
'activity' => 'activities/%s',
2526
'activities' => 'activities',
2627
'export_contacts_activity' => 'activities/exportcontacts',
@@ -109,10 +110,16 @@ class Config
109110
/**
110111
* Errors to be returned for various exceptions
111112
*/
112-
'errors' => array(
113-
'id_or_object' => 'Only an id or %s object are allowed for this method.'
114-
)
115-
113+
'errors' => array(
114+
'id_or_object' => 'Only an id or %s object are allowed for this method.'
115+
),
116+
117+
/**
118+
* Setting the version fo the application used in Rest Calls when setting the version header
119+
*/
120+
'settings' => array(
121+
'version' => '1.2.0'
122+
),
116123
);
117124

118125
/**

src/Ctct/Util/RestClient.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,14 @@ public function delete($url, array $headers = array())
6868
*/
6969
private static function httpRequest($url, $method, array $headers = array(), $data = null)
7070
{
71+
//adding the version header to the existing headers
72+
$headers[] = self::getVersionHeader();
73+
7174
$curl = curl_init();
7275
curl_setopt($curl, CURLOPT_URL, $url);
7376
curl_setopt($curl, CURLOPT_HEADER, 0);
7477
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
75-
curl_setopt($curl, CURLOPT_USERAGENT, "ConstantContact AppConnect PHP Library v1.1");
78+
curl_setopt($curl, CURLOPT_USERAGENT, "ConstantContact AppConnect PHP Library v" . Config::get('settings.version'));
7679
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
7780
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
7881
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
@@ -87,7 +90,7 @@ private static function httpRequest($url, $method, array $headers = array(), $da
8790

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

97100
return $response;
98101
}
102+
103+
/**
104+
* Returns the version header for the rest calls
105+
* @return string
106+
*/
107+
public static function getVersionHeader(){
108+
return 'x-ctct-request-source: sdk.php.' . Config::get('settings.version');
109+
}
99110
}

0 commit comments

Comments
 (0)