Skip to content
This repository has been archived by the owner on Jan 16, 2019. It is now read-only.

Commit

Permalink
update ActiveResource to handle Status 429 Too Many Requests
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelp committed Jan 19, 2013
1 parent bc527f9 commit 59b3fbd
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions lib/ActiveResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
* ?>
*
* @author John Luxford <[email protected]>
* @version 0.14 beta
* @author Rafael Lima - http://rafael.adm.br
* @version 0.15 beta
* @license http://opensource.org/licenses/lgpl-2.1.php
*/
class ActiveResource {
Expand Down Expand Up @@ -635,15 +636,28 @@ 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.";
curl_close ($ch);
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);
Expand Down

0 comments on commit 59b3fbd

Please sign in to comment.