Skip to content
Browse files

Handle status code 429 Too Many Requests used by API Throttling

  • Loading branch information...
1 parent 252fd8c commit 4c8f2c50646b405c0316ebc8fac585b99f39d219 @rafaelp rafaelp committed
Showing with 14 additions and 1 deletion.
  1. +14 −1 ActiveResource.php
View
15 ActiveResource.php
@@ -647,8 +647,9 @@ function _fetch ($url, $method, $params) {
$res = curl_exec ($ch);
- // Check HTTP status code for denied access
$http_code = curl_getinfo ($ch, CURLINFO_HTTP_CODE);
+
+ // Check HTTP status code for denied access
if ($http_code == 401) {
$this->errno = $http_code;
$this->error = "HTTP Basic: Access denied.";
@@ -656,6 +657,18 @@ function _fetch ($url, $method, $params) {
return false;
}
+ // Check HTTP status code for rate limit
+ if ($http_code == 429) {
+ if (preg_match ('/Retry-After: ([0-9]+)/', $res, $retry_after)) {
+ sleep(intval($retry_after[1]));
+ return $this->_fetch ($url, $method, $params);
+ }
+ $this->errno = $http_code;
+ $this->error = "Too Many Requests";
+ curl_close ($ch);
+ return false;
+ }
+
if (! $res) {
$this->errno = curl_errno ($ch);
$this->error = curl_error ($ch);

0 comments on commit 4c8f2c5

Please sign in to comment.
Something went wrong with that request. Please try again.