muhamin
muhamin

Reputation: 185

Instagram API count limits using HTTP header

According to Instagram you can check API limit count remaining using the HTTP headers that they supply with the call. I am quite new to this and am unable to find relevant data on how to access this information with PHP. Could anyone please clarify this for me?

I found the following from the Instagram API developers forum on Google Groups:

"We just rolled this out to production; all API calls now have the additional HTTP headers:

  • X-Ratelimit-Limit (total # of possible calls per hour)
  • X-Ratelimit-Remaining (how many calls are left for this particular token or client ID)"

Upvotes: 3

Views: 2093

Answers (1)

quietmint
quietmint

Reputation: 14164

If you're using file_get_contents to make the request (or anything that uses the HTTP wrapper for that matter), the special variable $http_response_header contains an array of lines with the HTTP response headers of the most recent request.

Maybe something like this:

// Make your API request here
...
file_get_contents('http://example.com', false, $context);

// Check HTTP response headers
foreach ($http_response_header as $line) {
    if(preg_match('!X-Ratelimit-Remaining: ([0-9]+)!i', $line, $matches)) {
        $remaining = $matches[1];
        break;
    }
}

// Do something based on the number of remaining attempts
echo "Remaining attempts: $remaining";

Upvotes: 2

Related Questions