Skip to content

Commit 2bf5b72

Browse files
Merge pull request #84 from cristiangrama/develop-cleanup
Add docblocks and set curl timeout on the class Fixes #79
2 parents 99d817c + 9ce7dc4 commit 2bf5b72

File tree

3 files changed

+180
-11
lines changed

3 files changed

+180
-11
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
},
1414
"require": {
1515
"php": ">=5.3.0",
16-
"lib-curl": "*"
16+
"ext-curl": "*"
1717
},
1818
"autoload": {
1919
"classmap": [

includes/ActiveCampaign.class.php

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,90 @@
66

77
require_once("Connector.class.php");
88

9+
/**
10+
* Class ActiveCampaign
11+
*/
912
class ActiveCampaign extends AC_Connector {
1013

14+
/**
15+
* @var
16+
*/
1117
public $url_base;
18+
19+
/**
20+
* @var
21+
*/
1222
public $url;
23+
24+
/**
25+
* @var
26+
*/
1327
public $api_key;
28+
29+
/**
30+
* @var
31+
*/
1432
public $track_email;
33+
34+
/**
35+
* @var
36+
*/
1537
public $track_actid;
38+
39+
/**
40+
* @var
41+
*/
1642
public $track_key;
43+
44+
/**
45+
* @var int
46+
*/
1747
public $version = 1;
48+
49+
/**
50+
* @var bool
51+
*/
1852
public $debug = false;
53+
54+
/**
55+
* @var string
56+
*/
1957
public $curl_response_error = "";
2058

59+
/**
60+
* ActiveCampaign constructor.
61+
*
62+
* @param $url
63+
* @param $api_key
64+
* @param string $api_user
65+
* @param string $api_pass
66+
*/
2167
function __construct($url, $api_key, $api_user = "", $api_pass = "") {
2268
$this->url_base = $this->url = $url;
2369
$this->api_key = $api_key;
2470
parent::__construct($url, $api_key, $api_user, $api_pass);
2571
}
2672

73+
/**
74+
* Set the version on the url
75+
*
76+
* @param $version
77+
*/
2778
function version($version) {
2879
$this->version = (int)$version;
2980
if ($version == 2) {
3081
$this->url_base = $this->url_base . "/2";
3182
}
3283
}
3384

85+
/**
86+
* Make api calls
87+
*
88+
* @param $path
89+
* @param array $post_data
90+
*
91+
* @return mixed
92+
*/
3493
function api($path, $post_data = array()) {
3594
// IE: "contact/view"
3695
$components = explode("/", $path);
@@ -91,7 +150,8 @@ function api($path, $post_data = array()) {
91150
}
92151

93152
$class = new $class($this->version, $this->url_base, $this->url, $this->api_key);
94-
// IE: $contact->view()
153+
154+
$class->set_curl_timeout($this->get_curl_timeout());
95155

96156
if ($add_tracking) {
97157
$class->track_email = $this->track_email;
@@ -130,5 +190,3 @@ function api($path, $post_data = array()) {
130190
require_once("Tracking.class.php");
131191
require_once("User.class.php");
132192
require_once("Webhook.class.php");
133-
134-
?>

includes/Connector.class.php

Lines changed: 118 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,44 @@
22

33
require_once(dirname(__FILE__) . "/exceptions/RequestException.php");
44

5+
/**
6+
* Class AC_Connector
7+
*/
58
class AC_Connector {
69

7-
const DEFAULT_TIMEOUT = 30;
10+
/**
11+
* Default curl timeout
12+
*/
13+
const DEFAULT_TIMEOUT = 30;
814

15+
/**
16+
* @var string
17+
*/
918
public $url;
19+
20+
/**
21+
* @var
22+
*/
1023
public $api_key;
24+
25+
/**
26+
* @var string
27+
*/
1128
public $output = "json";
29+
30+
/**
31+
* @var int
32+
*/
1233
private $timeout = self::DEFAULT_TIMEOUT;
1334

35+
/**
36+
* AC_Connector constructor.
37+
*
38+
* @param $url
39+
* @param $api_key
40+
* @param string $api_user
41+
* @param string $api_pass
42+
*/
1443
function __construct($url, $api_key, $api_user = "", $api_pass = "") {
1544
// $api_pass should be md5() already
1645
$base = "";
@@ -31,6 +60,12 @@ function __construct($url, $api_key, $api_user = "", $api_pass = "") {
3160
$this->api_key = $api_key;
3261
}
3362

63+
/**
64+
* Test the api credentials
65+
*
66+
* @return bool|mixed
67+
* @throws \RequestException
68+
*/
3469
public function credentials_test() {
3570
$test_url = "{$this->url}&api_action=user_me&api_output={$this->output}";
3671
$r = $this->curl($test_url);
@@ -45,7 +80,14 @@ public function credentials_test() {
4580
return $r;
4681
}
4782

48-
// debug function (nicely outputs variables)
83+
/**
84+
* Debug helper function
85+
*
86+
* @param $var
87+
* @param int $continue
88+
* @param string $element
89+
* @param string $extra
90+
*/
4991
public function dbg($var, $continue = 0, $element = "pre", $extra = "") {
5092
echo "<" . $element . ">";
5193
echo "Vartype: " . gettype($var) . "\n";
@@ -60,14 +102,35 @@ public function dbg($var, $continue = 0, $element = "pre", $extra = "") {
60102
if (!$continue) exit();
61103
}
62104

105+
/**
106+
* Set curl timeout
107+
*
108+
* @param $seconds
109+
*/
63110
public function set_curl_timeout($seconds) {
64111
$this->timeout = $seconds;
65112
}
66113

114+
/**
115+
* Get curl timeout
116+
*
117+
* @return int
118+
*/
67119
public function get_curl_timeout() {
68120
return $this->timeout;
69121
}
70122

123+
/**
124+
* Make the curl request
125+
*
126+
* @param $url
127+
* @param array $params_data
128+
* @param string $verb
129+
* @param string $custom_method
130+
*
131+
* @return mixed
132+
* @throws \RequestException
133+
*/
71134
public function curl($url, $params_data = array(), $verb = "", $custom_method = "") {
72135
if ($this->version == 1) {
73136
// find the method from the URL.
@@ -82,12 +145,17 @@ public function curl($url, $params_data = array(), $verb = "", $custom_method =
82145
$method = $custom_method;
83146
$url .= "?api_key=" . $this->api_key;
84147
}
148+
85149
$debug_str1 = "";
150+
86151
$request = curl_init();
152+
87153
$debug_str1 .= "\$ch = curl_init();\n";
154+
88155
curl_setopt($request, CURLOPT_HEADER, 0);
89156
curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
90157
curl_setopt($request, CURLOPT_TIMEOUT, $this->timeout);
158+
91159
$debug_str1 .= "curl_setopt(\$ch, CURLOPT_HEADER, 0);\n";
92160
$debug_str1 .= "curl_setopt(\$ch, CURLOPT_RETURNTRANSFER, true);\n";
93161
$debug_str1 .= "curl_setopt(\$ch, CURLOPT_TIMEOUT, " . $this->timeout . ");\n";
@@ -109,20 +177,26 @@ public function curl($url, $params_data = array(), $verb = "", $custom_method =
109177
$verb = "GET";
110178
}
111179
}
180+
112181
$debug_str1 .= "curl_setopt(\$ch, CURLOPT_URL, \"" . $url . "\");\n";
182+
113183
if ($this->debug) {
114184
$this->dbg($url, 1, "pre", "Description: Request URL");
115185
}
186+
116187
if ($verb == "POST" || $verb == "PUT" || $verb == "DELETE") {
117188
if ($verb == "PUT") {
118189
curl_setopt($request, CURLOPT_CUSTOMREQUEST, "PUT");
190+
119191
$debug_str1 .= "curl_setopt(\$ch, CURLOPT_CUSTOMREQUEST, \"PUT\");\n";
120192
} elseif ($verb == "DELETE") {
121193
curl_setopt($request, CURLOPT_CUSTOMREQUEST, "DELETE");
194+
122195
$debug_str1 .= "curl_setopt(\$ch, CURLOPT_CUSTOMREQUEST, \"DELETE\");\n";
123196
} else {
124197
$verb = "POST";
125198
curl_setopt($request, CURLOPT_POST, 1);
199+
126200
$debug_str1 .= "curl_setopt(\$ch, CURLOPT_POST, 1);\n";
127201
}
128202
$data = "";
@@ -172,44 +246,68 @@ public function curl($url, $params_data = array(), $verb = "", $custom_method =
172246
}
173247

174248
$data = rtrim($data, "& ");
249+
175250
curl_setopt($request, CURLOPT_HTTPHEADER, array("Expect:"));
251+
176252
$debug_str1 .= "curl_setopt(\$ch, CURLOPT_HTTPHEADER, array(\"Expect:\"));\n";
253+
177254
if ($this->debug) {
178255
curl_setopt($request, CURLINFO_HEADER_OUT, 1);
256+
179257
$debug_str1 .= "curl_setopt(\$ch, CURLINFO_HEADER_OUT, 1);\n";
258+
180259
$this->dbg($data, 1, "pre", "Description: POST data");
181260
}
261+
182262
curl_setopt($request, CURLOPT_POSTFIELDS, $data);
263+
183264
$debug_str1 .= "curl_setopt(\$ch, CURLOPT_POSTFIELDS, \"" . $data . "\");\n";
184265
}
266+
185267
curl_setopt($request, CURLOPT_SSL_VERIFYPEER, false);
186268
curl_setopt($request, CURLOPT_SSL_VERIFYHOST, 0);
269+
187270
$debug_str1 .= "curl_setopt(\$ch, CURLOPT_SSL_VERIFYPEER, false);\n";
188271
$debug_str1 .= "curl_setopt(\$ch, CURLOPT_SSL_VERIFYHOST, 0);\n";
272+
189273
$response = curl_exec($request);
274+
190275
$curl_error = curl_error($request);
276+
191277
if (!$response && $curl_error) {
192278
return $curl_error;
193279
}
280+
194281
$debug_str1 .= "curl_exec(\$ch);\n";
282+
195283
if ($this->debug) {
196284
$this->dbg($response, 1, "pre", "Description: Raw response");
197285
}
286+
198287
$http_code = curl_getinfo($request, CURLINFO_HTTP_CODE);
199288
if (!preg_match("/^[2-3][0-9]{2}/", $http_code)) {
200289
// If not 200 or 300 range HTTP code, return custom error.
201290
return "HTTP code $http_code returned";
202291
}
292+
203293
$debug_str1 .= "\$http_code = curl_getinfo(\$ch, CURLINFO_HTTP_CODE);\n";
294+
204295
if ($this->debug) {
205296
$this->dbg($http_code, 1, "pre", "Description: Response HTTP code");
297+
206298
$request_headers = curl_getinfo($request, CURLINFO_HEADER_OUT);
299+
207300
$debug_str1 .= "\$request_headers = curl_getinfo(\$ch, CURLINFO_HEADER_OUT);\n";
301+
208302
$this->dbg($request_headers, 1, "pre", "Description: Request headers");
209303
}
304+
210305
curl_close($request);
306+
211307
$debug_str1 .= "curl_close(\$ch);\n";
308+
212309
$object = json_decode($response);
310+
213311
if ($this->debug) {
214312
$this->dbg($object, 1, "pre", "Description: Response object (json_decode)");
215313
}
@@ -220,9 +318,7 @@ public function curl($url, $params_data = array(), $verb = "", $custom_method =
220318
return $response;
221319
}
222320

223-
$requestException = new RequestException;
224-
$requestException->setFailedMessage($response);
225-
throw $requestException;
321+
$this->throwRequestException($response);
226322
}
227323

228324
if ($this->debug) {
@@ -233,18 +329,33 @@ public function curl($url, $params_data = array(), $verb = "", $custom_method =
233329

234330
if (isset($object->result_code)) {
235331
$object->success = $object->result_code;
332+
236333
if (!(int)$object->result_code) {
237334
$object->error = $object->result_message;
238335
}
239-
}
240-
elseif (isset($object->succeeded)) {
336+
} elseif (isset($object->succeeded)) {
241337
// some calls return "succeeded" only
242338
$object->success = $object->succeeded;
339+
243340
if (!(int)$object->succeeded) {
244341
$object->error = $object->message;
245342
}
246343
}
344+
247345
return $object;
248346
}
249347

348+
/**
349+
* Throw the request exception
350+
*
351+
* @param $message
352+
*
353+
* @throws \RequestException
354+
*/
355+
protected function throwRequestException($message) {
356+
$requestException = new RequestException;
357+
$requestException->setFailedMessage($message);
358+
359+
throw $requestException;
360+
}
250361
}

0 commit comments

Comments
 (0)