0

我正在为 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-剩余

(有关 Instagram 限制的更多信息,请访问http://instagram.com/developer/limits/)。

出于显而易见的原因,我需要我正在创建的应用程序在速率限制开始之前“自行关闭”。通过访问速率限制信息,我可以实现这一点。

我找到了一个应该与这个类一起工作的要点,但我似乎无法让它工作:https ://gist.github.com/cosenary/6af4cf4b509518169b88

Stackoverflow 上的这个话题似乎也没有结果: Instagram API count limits using HTTP header

如果有人可以在这里帮助我,那就太棒了!

最好的问候, Peter de Leeuw

4

1 回答 1

1

我已经修改了函数 _makeCall 并且通过添加第一个 curl_setopt($ch, CURLOPT_HEADER, true); 如下所示 并按照这个要点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);
}

处理标头的 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。

**我已经测试了这个解决方案,它运行良好。

希望这对你们有帮助:)

于 2015-01-04T02:35:30.780 回答