@@ -15,6 +15,8 @@ class MailChimp
1515 private $ api_key ;
1616 private $ api_endpoint = 'https://<dc>.api.mailchimp.com/3.0 ' ;
1717
18+ const TIMEOUT = 10 ;
19+
1820 /* SSL Verification
1921 Read before disabling:
2022 http://snippets.webaware.com.au/howto/stop-turning-off-curlopt_ssl_verifypeer-and-fix-your-php-config/
@@ -113,7 +115,7 @@ public function getLastRequest()
113115 * @param int $timeout Timeout limit for request in seconds
114116 * @return array|false Assoc array of API response, decoded from JSON
115117 */
116- public function delete ($ method , $ args = array (), $ timeout = 10 )
118+ public function delete ($ method , $ args = array (), $ timeout = self :: TIMEOUT )
117119 {
118120 return $ this ->makeRequest ('delete ' , $ method , $ args , $ timeout );
119121 }
@@ -125,7 +127,7 @@ public function delete($method, $args = array(), $timeout = 10)
125127 * @param int $timeout Timeout limit for request in seconds
126128 * @return array|false Assoc array of API response, decoded from JSON
127129 */
128- public function get ($ method , $ args = array (), $ timeout = 10 )
130+ public function get ($ method , $ args = array (), $ timeout = self :: TIMEOUT )
129131 {
130132 return $ this ->makeRequest ('get ' , $ method , $ args , $ timeout );
131133 }
@@ -137,7 +139,7 @@ public function get($method, $args = array(), $timeout = 10)
137139 * @param int $timeout Timeout limit for request in seconds
138140 * @return array|false Assoc array of API response, decoded from JSON
139141 */
140- public function patch ($ method , $ args = array (), $ timeout = 10 )
142+ public function patch ($ method , $ args = array (), $ timeout = self :: TIMEOUT )
141143 {
142144 return $ this ->makeRequest ('patch ' , $ method , $ args , $ timeout );
143145 }
@@ -149,7 +151,7 @@ public function patch($method, $args = array(), $timeout = 10)
149151 * @param int $timeout Timeout limit for request in seconds
150152 * @return array|false Assoc array of API response, decoded from JSON
151153 */
152- public function post ($ method , $ args = array (), $ timeout = 10 )
154+ public function post ($ method , $ args = array (), $ timeout = self :: TIMEOUT )
153155 {
154156 return $ this ->makeRequest ('post ' , $ method , $ args , $ timeout );
155157 }
@@ -161,7 +163,7 @@ public function post($method, $args = array(), $timeout = 10)
161163 * @param int $timeout Timeout limit for request in seconds
162164 * @return array|false Assoc array of API response, decoded from JSON
163165 */
164- public function put ($ method , $ args = array (), $ timeout = 10 )
166+ public function put ($ method , $ args = array (), $ timeout = self :: TIMEOUT )
165167 {
166168 return $ this ->makeRequest ('put ' , $ method , $ args , $ timeout );
167169 }
@@ -175,7 +177,7 @@ public function put($method, $args = array(), $timeout = 10)
175177 * @return array|false Assoc array of decoded result
176178 * @throws \Exception
177179 */
178- private function makeRequest ($ http_verb , $ method , $ args = array (), $ timeout = 10 )
180+ private function makeRequest ($ http_verb , $ method , $ args = array (), $ timeout = self :: TIMEOUT )
179181 {
180182 if (!function_exists ('curl_init ' ) || !function_exists ('curl_setopt ' )) {
181183 throw new \Exception ("cURL support is required, but can't be found. " );
@@ -245,10 +247,10 @@ private function makeRequest($http_verb, $method, $args = array(), $timeout = 10
245247
246248 $ responseContent = curl_exec ($ ch );
247249
250+ $ response ['headers ' ] = curl_getinfo ($ ch );
248251 if ($ responseContent === false ) {
249252 $ this ->last_error = curl_error ($ ch );
250253 } else {
251- $ response ['headers ' ] = curl_getinfo ($ ch );
252254 $ headerSize = $ response ['headers ' ]['header_size ' ];
253255
254256 $ response ['httpHeaders ' ] = $ this ->getHeadersAsArray (substr ($ responseContent , 0 , $ headerSize ));
@@ -263,7 +265,7 @@ private function makeRequest($http_verb, $method, $args = array(), $timeout = 10
263265
264266 $ formattedResponse = $ this ->formatResponse ($ response );
265267
266- $ this ->determineSuccess ($ response , $ formattedResponse );
268+ $ this ->determineSuccess ($ response , $ formattedResponse, $ timeout );
267269
268270 return $ formattedResponse ;
269271 }
@@ -371,9 +373,10 @@ private function formatResponse($response)
371373 * Check if the response was successful or a failure. If it failed, store the error.
372374 * @param array $response The response from the curl request
373375 * @param array|false $formattedResponse The response body payload from the curl request
376+ * @param int $timeout The timeout supplied to the curl request.
374377 * @return bool If the request was successful
375378 */
376- private function determineSuccess ($ response , $ formattedResponse )
379+ private function determineSuccess ($ response , $ formattedResponse, $ timeout )
377380 {
378381 $ status = $ this ->findHTTPStatus ($ response , $ formattedResponse );
379382
@@ -387,6 +390,11 @@ private function determineSuccess($response, $formattedResponse)
387390 return false ;
388391 }
389392
393+ if ( $ timeout > 0 && $ response ['headers ' ] && $ response ['headers ' ]['total_time ' ] >= $ timeout ) {
394+ $ this ->last_error = sprintf ('Request timed out after %f seconds. ' , $ response ['headers ' ]['total_time ' ] );
395+ return false ;
396+ }
397+
390398 $ this ->last_error = 'Unknown error, call getLastResponse() to find out what happened. ' ;
391399 return false ;
392400 }
0 commit comments