Getting Rate Limits in PHP

Hey i need to get the rate limit reset in response but its not showing up in the http response headers. I have a function that gets user followers and when it gets a 429 error i want to know what the rate limit is. How can I get the rate limit header? I am using apache_response_headers() to get the headers and rate limit is now showing up. Thanks for the help!

function userFollowers( $ch, $headers,$id){
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, “https://api.twitch.tv/helix/users/follows?to_id=$id”);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, “GET”);

$json = json_decode(curl_exec($ch), true);
if( isset($json[‘total’])){
$result= ($json[‘total’]);
}elseif(isset($json[‘error’])){
$result =“NAN”;
return apache_response_headers();
}else?>{
$result = “ERROR”;
}

return $result;

}//end user followers function

add

curl_setopt($ch, CURLOPT_HEADER, true);

and then use something like

$headers = get_headers_from_curl_response($response);

function get_headers_from_curl_response($response)
{
$headers = array();

$header_text = substr($response, 0, strpos($response, "\r\n\r\n"));

foreach (explode("\r\n", $header_text) as $i => $line)
    if ($i === 0)
        $headers['http_code'] = $line;
    else
    {
        list ($key, $value) = explode(': ', $line);

        $headers[$key] = $value;
    }

return $headers;

}

Something like https://stackoverflow.com/questions/10589889/returning-header-as-array-using-curl

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.