cosenary / Instagram-PHP-API

An easy-to-use PHP Class for accessing Instagram's API.
http://cosenary.github.com/Instagram-PHP-API
BSD 3-Clause "New" or "Revised" License
1.46k stars 781 forks source link

Pagination for UserLikes Broken #178

Open kishanio opened 9 years ago

kishanio commented 9 years ago

Pagination for UserLikes won't work by using native pagination method since instagram changed parameter variable from next_max_id to next_max_like_id & beside why can't we use the URL provided by instagram to fetch next feed rather than creating custom URL?

lauroBRCWB commented 8 years ago

My suggestion is 3 steps

1) to refactor _makeCall function in Instagram.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;
    }

    return $this->_makeCallUrl(self::API_URL . $function . $authMethod . (('GET' === $method) ? $paramString : null),$params,$paramString,$method);
}

2) Create a new _makeCallUrl function:

private function _makeCallUrl($apiCall,$params = NULL, $paramString = NULL, $method = 'GET'){
    // 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_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);
}

3) Refactor pagination method to:

  public function pagination($obj, $limit = 0) {

    if (true === is_object($obj) && !is_null($obj->pagination)) {
        return $this->_makeCallUrl($obj->pagination->next_url);
    } else {
      throw new \Exception("Error: pagination() | This method doesn't support pagination.");
    }
  }