0

对于我发出的每个 Twitter 请求,返回的 HTTP 标头应包含 X-RateLimit-Limit

但是,我似乎无法使用 PHP 检索这些。谁能告诉我我犯了什么愚蠢的错误?

我已经以正常方式设置了我的 curl 并且能够成功地 GET 和 POST 请求。

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLINFO_HEADER_OUT, TRUE);
curl_setopt($ch, CURLOPT_VERBOSE, TRUE);

$response = curl_exec($ch);
$response_info=curl_getinfo($ch);
$erno = curl_errno($ch);
$er = curl_error($ch);
curl_close($ch);

我可以获得一些响应信息,例如 http_code

$response_info['http_code']

但是这一行只返回 null

//Doesn't bloody work. No idea why!
$rate_limit = $response_info['X-RateLimit-Limit'];

我正在运行 PHP 版本 5.3.10。

编辑

这是 print_r($response_info); 的结果

Array
(
[url] => https://api.twitter.com/1/statuses/home_timeline.json...
[content_type] => application/json;charset=utf-8
[http_code] => 200
[header_size] => 695
[request_size] => 410
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 1.239977
[namelookup_time] => 0.007361
[connect_time] => 0.155783
[pretransfer_time] => 0.465397
[size_upload] => 0
[size_download] => 99425
[speed_download] => 80182
[speed_upload] => 0
[download_content_length] => 99425
[upload_content_length] => 0
[starttransfer_time] => 0.794829
[redirect_time] => 0
[certinfo] => Array()
[redirect_url] => 
[request_header] => GET /1/statuses/home_timeline.json... HTTP/1.1
Host: api.twitter.com
Accept: */*
)
4

1 回答 1

3

curl_getinfo不返回响应标头,仅返回有关请求的其他元信息。要检索标头,请设置CURLOPT_HEADERtrue. 这将包括输出中的标题。要将它们与响应正文分开,请执行以下操作:

list($headers, $body) = explode("\n\n", $response, 2);

要解析标题,请再次分解:

$headers = explode("\n", $headers);
foreach ($headers as $header) {
    list($key, $value) = explode(':', $header, 2);
    $headers[trim($key)] = trim($value);
}

echo $headers['X-RateLimit-Limit'];
于 2012-05-25T08:02:11.087 回答