[英]Instagram API access rate limit information with specific PHP Class
I am using this PHP Class for Instagram's API: https://github.com/cosenary/Instagram-PHP-API . 我将这个PHP类用于Instagram的API: https : //github.com/cosenary/Instagram-PHP-API 。 It works perfectly but I can't seem to access the rate limit information for each call I make.
它工作正常,但是我似乎无法访问我拨打的每个电话的限速信息。
Inside the class, this is the method that makes the call: 在类内部,这是进行调用的方法:
protected function _makeCall($function, $auth = false, $params = null, $method = 'GET') {
if (false === $auth) {
$authMethod = '?client_id=' . $this->getApiKey();
} else {
if (true === isset($this->_accesstoken)) {
$authMethod = '?access_token=' . $this->getAccessToken();
} else {
throw new \Exception("Error: _makeCall() | $function - This method requires an authenticated users access token.");
}
}
if (isset($params) && is_array($params)) {
$paramString = '&' . http_build_query($params);
} else {
$paramString = null;
}
$apiCall = self::API_URL . $function . $authMethod . (('GET' === $method) ? $paramString : null);
$headerData = array('Accept: application/json');
if (true === $this->_signedheader && 'GET' !== $method) {
$headerData[] = 'X-Insta-Forwarded-For: ' . $this->_signHeader();
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiCall);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headerData);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
if ('POST' === $method) {
curl_setopt($ch, CURLOPT_POST, count($params));
curl_setopt($ch, CURLOPT_POSTFIELDS, ltrim($paramString, '&'));
} else if ('DELETE' === $method) {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
}
$jsonData = curl_exec($ch);
if (false === $jsonData) {
throw new \Exception("Error: _makeCall() - cURL error: " . curl_error($ch));
}
curl_close($ch);
return json_decode($jsonData);
}
The information I need to access from Instagram's API is: 我需要从Instagram的API访问的信息是:
- X-Ratelimit-Limit
X-Ratelimit限
- X-Ratelimit-Remaining
X-Ratelimit残留
( http://instagram.com/developer/limits/ for more information about Instagram's limits). ( http://instagram.com/developer/limits/了解有关Instagram限制的更多信息)。
For obvious reasons I need the app I'm creating to "shut itself down" before the rate limit kicks in. By accessing the rate limit information I can achieve this. 出于显而易见的原因,在速率限制生效之前,我需要创建的应用程序“自行关闭”。通过访问速率限制信息,我可以实现这一目标。
I have found a Gist that should work with this class but I can't seem to get it to work: https://gist.github.com/cosenary/6af4cf4b509518169b88 我已经找到了一个适用于该类的Gist,但是我似乎无法使其正常工作: https : //gist.github.com/cosenary/6af4cf4b509518169b88
Also this topic here on Stackoverflow seems to be fruitless: Instagram API count limits using HTTP header 同样,这里关于Stackoverflow的这个话题似乎也没有用: 使用HTTP标头的Instagram API计数限制
If anyone could help me out here that would be amazing! 如果有人可以帮助我,那就太好了!
Best regards, Peter de Leeuw 最好的问候,Peter de Leeuw
I have modified the function _makeCall and it is as follows by adding first curl_setopt($ch, CURLOPT_HEADER, true); 我已经修改了函数_makeCall,并通过添加第一个curl_setopt($ ch,CURLOPT_HEADER,true)如下所示; and calling the function processHeader() as cosenary suggested on this gist cosenary/ratelimit.php :
并在此要点cosenary / ratelimit.php上建议作为cosenary建议的函数processHeader():
protected function _makeCall($function, $auth = false, $params = null, $method = 'GET') {
if (false === $auth) {
// if the call doesn't requires authentication
$authMethod = '?client_id=' . $this->getApiKey();
} else {
// if the call needs an authenticated user
if (true === isset($this->_accesstoken)) {
$authMethod = '?access_token=' . $this->getAccessToken();
} else {
throw new \Exception("Error: _makeCall() | $function - This method requires an authenticated users access token.");
}
}
if (isset($params) && is_array($params)) {
$paramString = '&' . http_build_query($params);
} else {
$paramString = null;
}
$apiCall = self::API_URL . $function . $authMethod . (('GET' === $method) ? $paramString : null);
// signed header of POST/DELETE requests
$headerData = array('Accept: application/json');
if (true === $this->_signedheader && 'GET' !== $method) {
$headerData[] = 'X-Insta-Forwarded-For: ' . $this->_signHeader();
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiCall);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headerData);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
if ('POST' === $method) {
curl_setopt($ch, CURLOPT_POST, count($params));
curl_setopt($ch, CURLOPT_POSTFIELDS, ltrim($paramString, '&'));
} else if ('DELETE' === $method) {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
}
$jsonData = curl_exec($ch);
// split header from JSON data
// and assign each to a variable
list($headerContent, $jsonData) = explode("\r\n\r\n", $jsonData, 2);
// convert header content into an array
$headers = $this->processHeaders($headerContent);
// get the 'X-Ratelimit-Remaining' header value
$ratelimitRemaining = $headers['X-Ratelimit-Remaining'];
$this->setHeaderLimit($ratelimitRemaining);
if (false === $jsonData) {
throw new \Exception("Error: _makeCall() - cURL error: " . curl_error($ch));
}
curl_close($ch);
return json_decode($jsonData);
}
the processHeader() method which processes the header and the set and get methods for the $rateLimitRemaining are as follows : processHeader()方法处理标头以及$ rateLimitRemaining的set和get方法如下:
private function processHeaders($headerContent){
$headers = array();
foreach (explode("\r\n", $headerContent) as $i => $line) {
if($i===0){
$headers['http_code'] = $line;
}else{
list($key,$value) = explode(':', $line);
$headers[$key] = $value;
}
}
return $headers;
}
private function setHeaderLimit($HeaderLimit){
$this->HeaderLimit = $HeaderLimit;
}
public function getHeaderLimit(){
return $this->HeaderLimit;
}
You can access the X-Ratelimit-Remaining now from another class just by calling the getHeaderLimit() 您可以通过调用getHeaderLimit()从另一个类现在访问X-Ratelimit-Remaining
*Don't forget to declare the public field HeaderLimit within the class where _makeCall() resides, which in this case is Instagram.php. *不要忘记在_makeCall()所在的类(在本例中为Instagram.php)中声明公共字段HeaderLimit。
**I have tested this solution and it works perfectly. **我已经测试了此解决方案,并且效果很好。
Hope this helps you guys :) 希望这对你们有帮助:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.