我在Instagram的API中使用这个PHP类:https://github.com/cosenary/Instagram-PHP-API。 它工作得很好,但我似乎无法访问每次通话的速率限制信息。
在课堂内,这是进行调用的方法:
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);
}
我需要从Instagram的API访问的信息是:
- X-Ratelimit限
- X-Ratelimit-剩余
(http://instagram.com/developer/limits/了解有关Instagram限制的更多信息)。
由于显而易见的原因,我需要创建的应用程序在速率限制开始之前“关闭自己”。通过访问速率限制信息,我可以实现此目的。
我找到了一个应该与这个课程一起工作的要点,但我似乎无法让它发挥作用:https://gist.github.com/cosenary/6af4cf4b509518169b88
此处有关Stackoverflow的主题似乎也毫无结果: Instagram API count limits using HTTP header
如果有人能在这里帮助我,那就太棒了!
祝你好运, Peter de Leeuw
答案 0 :(得分:1)
我修改了_makeCall函数,首先添加curl_setopt($ ch,CURLOPT_HEADER,true);并在此要点cosenary上调用函数processHeader()为cosenary/ratelimit.php:
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);
}
处理标题的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;
}
您现在可以通过调用getHeaderLimit()
从另一个类访问X-Ratelimit-Remaining*不要忘记在_makeCall()所在的类中声明公共字段HeaderLimit,在本例中为Instagram.php。
**我已经测试过这个解决方案,它运行得很好。
希望这可以帮助你们:)