From 38548b561fe0afb5af0bd291055a6b1822fa49d8 Mon Sep 17 00:00:00 2001 From: Athlan Date: Wed, 25 Sep 2013 16:42:33 +0200 Subject: [PATCH] Fetching headers added. --- Instagram/Core/Proxy.php | 4 +--- Instagram/Net/ApiResponse.php | 44 ++++++++++++++++++++++++++++++++--- Instagram/Net/CurlClient.php | 33 ++++++++++++++++++++++++-- 3 files changed, 73 insertions(+), 8 deletions(-) diff --git a/Instagram/Core/Proxy.php b/Instagram/Core/Proxy.php index 22eb5dd..4c88f7a 100644 --- a/Instagram/Core/Proxy.php +++ b/Instagram/Core/Proxy.php @@ -534,7 +534,7 @@ public function deleteMediaComment( $media_id, $comment_id ) { */ private function apiCall( $method, $url, array $params = null, $throw_exception = true ){ - $raw_response = $this->client->$method( + $response = $this->client->$method( $url, array( 'access_token' => $this->access_token, @@ -542,8 +542,6 @@ private function apiCall( $method, $url, array $params = null, $throw_exception ) + (array) $params ); - $response = new \Instagram\Net\ApiResponse( $raw_response ); - if ( !$response->isValid() ) { if ( $throw_exception ) { if ( $response->getErrorType() == 'OAuthAccessTokenException' ) { diff --git a/Instagram/Net/ApiResponse.php b/Instagram/Net/ApiResponse.php index d23eb68..a999a88 100644 --- a/Instagram/Net/ApiResponse.php +++ b/Instagram/Net/ApiResponse.php @@ -25,14 +25,24 @@ class ApiResponse { */ protected $response; + /** + * Response headers + * + * @var array + * @access protected + */ + protected $responseHeaders; + /** * Constructor * * @param $raw_response Response from teh API + * @param $response_headers Response headers * @access public */ - public function __construct( $raw_response ){ + public function __construct( $raw_response, array $response_headers = null ){ $this->response = json_decode( $raw_response ); + $this->responseHeaders = $response_headers; if ( !$this->isValidApiResponse() ) { $this->response = new \StdClass; $this->response->meta = new \StdClass; @@ -88,7 +98,15 @@ public function getRawData() { return isset( $this->response ) ? $this->response : null; } - + /** + * Get the response headers + * + * @return array Return the response's headers + * @access public + */ + public function getHeaders() { + return $this->responseHeaders; + } /** * Get the response's error message @@ -137,7 +155,27 @@ public function getErrorType() { } return null; } - + + /** + * Gets the Rate limit returned from API + * + * @return mixed Returns the Rate limit returned from API + * @access public + */ + public function getRateLimit() { + return isset($this->responseHeaders['x-ratelimit-limit']) ? (int) $this->responseHeaders['x-ratelimit-limit'] : null; + } + + /** + * Gets the Remaining Rate limit returned from API + * + * @return mixed Returns the Remaining Rate limit returned from API + * @access public + */ + public function getRateLimitRemaining() { + return isset($this->responseHeaders['x-ratelimit-remaining']) ? (int) $this->responseHeaders['x-ratelimit-remaining'] : null; + } + /** * Magic to string method * diff --git a/Instagram/Net/CurlClient.php b/Instagram/Net/CurlClient.php index 90bf7b8..4b82f58 100644 --- a/Instagram/Net/CurlClient.php +++ b/Instagram/Net/CurlClient.php @@ -22,6 +22,13 @@ class CurlClient implements ClientInterface { */ protected $curl = null; + /** + * Curl Response header + * + * @var array + */ + protected $curlRequestHeaders = array(); + /** * Constructor * @@ -97,6 +104,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_HEADERFUNCTION, array($this, 'fetchHeader') ); } /** @@ -104,7 +112,7 @@ protected function initializeCurl() { * * Execute the curl object * - * @return StdClass + * @return ApiResponse * @access protected * @throws \Instagram\Core\ApiException */ @@ -114,7 +122,28 @@ protected function fetch() { if ( $error ) { throw new \Instagram\Core\ApiException( $error, 666, 'CurlError' ); } - return $raw_response; + + $response = new ApiResponse($raw_response, $this->curlRequestHeaders); + return $response; + } + + /** + * Method parses the headers returned by CURL and + * stores them into private array. + * + * @return ApiResponse + * @access protected + * @throws \Instagram\Core\ApiException + */ + protected function fetchHeader($ch, $header) { + $i = strpos($header, ':'); + if (!empty($i)) { + $key = strtolower(substr($header, 0, $i)); + $value = trim(substr($header, $i + 2)); + $this->curlRequestHeaders[$key] = $value; + } + + return strlen($header); } } \ No newline at end of file