Created
November 8, 2014 16:10
-
-
Save cosenary/6af4cf4b509518169b88 to your computer and use it in GitHub Desktop.
Instagram PHP API - Receive ratelimit header
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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