Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Instagram PHP API - Receive ratelimit header
<?php
/**
* Instagram PHP API
*
* @link https://github.com/cosenary/Instagram-PHP-API
* @author Christian Metz
* @since 8.11.2014
*/
// include header in your response
curl_setopt($ch, CURLOPT_HEADER, true);
// execute API request
$response = curl_exec($ch);
// split header from JSON data
// and assign each to a variable
list($headerContent, $jsonData) = explode("\r\n\r\n", $response, 2);
// convert header content into an array
$headers = processHeaders($headerContent);
// get the 'X-Ratelimit-Remaining' header value
$ratelimitRemaining = $headers['X-Ratelimit-Remaining'];
function processHeaders($headerContent) {
$headers = array();
// iterate over the header string line by line
foreach (explode("\r\n", $headerContent) as $i => $line) {
if ($i === 0) {
// the first line contains the HTTP response code
$headers['http_code'] = $line;
} else {
// split headers into key (name) and value
list($key, $value) = explode(': ', $line);
$headers[$key] = $value;
}
}
return $headers;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.