Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add remaining request logic #51

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions Instagram/Core/Proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ class Proxy {
*/
protected $api_url = 'https://api.instagram.com/v1';

/**
* @var int
* @access protected
*/
protected $requests_remaining = -1;

/**
* Constructor
*
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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;
}

Expand Down
8 changes: 8 additions & 0 deletions Instagram/Instagram.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

}
49 changes: 49 additions & 0 deletions Instagram/Net/ApiResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,24 @@ class ApiResponse {
*/
protected $response;

/**
* Response
*
* This is the headers from the response
*
* @var StdClass
* @access protected
*/
protected $headers;

/**
* Constructor
*
* @param $raw_response Response from teh API
* @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;
Expand Down Expand Up @@ -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;
}


/**
Expand Down Expand Up @@ -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
*
Expand All @@ -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;
}

}
1 change: 1 addition & 0 deletions Instagram/Net/CurlClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down