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

Commit d8828e3

Browse files
committed
Merge pull request #67 from csciuto/development
Added x-ctct-request-source header on REST calls
2 parents f273c31 + 23aec43 commit d8828e3

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

src/Ctct/Util/Config.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,16 @@ class Config
110110
/**
111111
* Errors to be returned for various exceptions
112112
*/
113-
'errors' => array(
114-
'id_or_object' => 'Only an id or %s object are allowed for this method.'
115-
)
116-
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+
),
117123
);
118124

119125
/**

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)