diff --git a/Instagram/Core/Proxy.php b/Instagram/Core/Proxy.php index 22eb5dd..a356601 100644 --- a/Instagram/Core/Proxy.php +++ b/Instagram/Core/Proxy.php @@ -50,6 +50,12 @@ class Proxy { */ protected $api_url = 'https://api.instagram.com/v1'; + /** + * @var int + * @access protected + */ + protected $requests_remaining = -1; + /** * Constructor * @@ -303,6 +309,15 @@ public function getLocation( $id ) { return $response->getData(); } + /** + * Get the number of requests remaining for this client. Returns -1 if unknown + * + * @return int + */ + public function getRequestsRemaining() { + return $this->requests_remaining; + } + /** * Search users * @@ -557,6 +572,12 @@ private function apiCall( $method, $url, array $params = null, $throw_exception return false; } } + + $headers = $response->getHeaders(); + if (isset($headers['X-Ratelimit-Remaining'])) { + $this->requests_remaining = $headers['X-Ratelimit-Remaining']; + } + return $response; } diff --git a/Instagram/Instagram.php b/Instagram/Instagram.php index a9829a5..822224f 100644 --- a/Instagram/Instagram.php +++ b/Instagram/Instagram.php @@ -280,4 +280,12 @@ public function getTagMedia( $tag, array $params = null ) { return new TagMediaCollection( $this->proxy->getTagMedia( $tag, $params ), $this->proxy ); } + /** + * Return the number of requests remaining on this client connection + * @return int + */ + public function getRequestsRemaining() { + return $this->proxy->getRequestsRemaining(); + } + } diff --git a/Instagram/Net/ApiResponse.php b/Instagram/Net/ApiResponse.php index d23eb68..758e391 100644 --- a/Instagram/Net/ApiResponse.php +++ b/Instagram/Net/ApiResponse.php @@ -25,6 +25,16 @@ class ApiResponse { */ protected $response; + /** + * Response + * + * This is the headers from the response + * + * @var StdClass + * @access protected + */ + protected $headers; + /** * Constructor * @@ -32,6 +42,7 @@ class ApiResponse { * @access public */ public function __construct( $raw_response ){ + $this->headers = $this->_strip_headers( $raw_response ); $this->response = json_decode( $raw_response ); if ( !$this->isValidApiResponse() ) { $this->response = new \StdClass; @@ -88,6 +99,15 @@ public function getRawData() { return isset( $this->response ) ? $this->response : null; } + /** + * Get the HTTP headers from the response + * + * @return array Returns the headers from the response + * @access public + */ + public function getHeaders() { + return isset( $this->headers ) ? $this->headers : null; + } /** @@ -138,6 +158,16 @@ public function getErrorType() { return null; } + /** + * Return the remaining request limit + * + * @return int Returns the number of requests left in the hour. -1 if unknown + * @access public + */ + public function getRemainingRequests() { + return isset( $this->headers['X-Ratelimit-Remaining'] ) ? $this->headers['X-Ratelimit-Remaining'] : -1; + } + /** * Magic to string method * @@ -148,4 +178,23 @@ public function __toString() { return json_encode( $this->response ); } + private function _strip_headers( &$raw_response ) { + $headers = array(); + + $header_text = substr($raw_response, 0, strpos($raw_response, "\r\n\r\n")); + $raw_response = substr($raw_response, strlen($header_text)); + + foreach (explode("\r\n", $header_text) as $i => $line) + if ($i === 0) + $headers['http_code'] = $line; + else + { + list ($key, $value) = explode(': ', $line); + + $headers[$key] = $value; + } + + return $headers; + } + } \ No newline at end of file diff --git a/Instagram/Net/CurlClient.php b/Instagram/Net/CurlClient.php index 90bf7b8..af17aba 100644 --- a/Instagram/Net/CurlClient.php +++ b/Instagram/Net/CurlClient.php @@ -97,6 +97,7 @@ protected function initializeCurl() { $this->curl = curl_init(); curl_setopt( $this->curl, CURLOPT_RETURNTRANSFER, true ); curl_setopt( $this->curl, CURLOPT_SSL_VERIFYPEER, false ); + curl_setopt($this->curl, CURLOPT_HEADER, 1); } /**