22
33require_once (dirname (__FILE__ ) . "/exceptions/RequestException.php " );
44
5+ /**
6+ * Class AC_Connector
7+ */
58class 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