Skip to content

Instantly share code, notes, and snippets.

@cosenary
Created November 8, 2014 16:10
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cosenary/6af4cf4b509518169b88 to your computer and use it in GitHub Desktop.
Save cosenary/6af4cf4b509518169b88 to your computer and use it in GitHub Desktop.
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