-1
(string) HTTP/1.1 200 OK
Content-Language: en
Content-Type: application/json; charset=utf-8
Date: Wed, 24 Oct 2012 21:38:50 GMT
Server: nginx
Vary: Accept-Language, Cookie, Accept-Encoding
X-Ratelimit-Limit: 5000
X-Ratelimit-Remaining: 4976
transfer-encoding: chunked
Connection: keep-alive

从上面的标题中,我想要X-Ratelimit-Remaining的值。我如何将其存储在 php.ini 中的变量中。

4

3 回答 3

1

使用get_headers()( Documentation ) 从标头中检索任何值。

于 2012-10-24T21:59:11.063 回答
0

正如@justderb的第一个答案已经说过,使用get_headers() PHP 函数。

如果您有一个带有标题信息的字符串。无论如何,您都可以转换为数组并获取它。

例子:

$url = "http://www.your-web.com";
$xr = get_headers($url, 1);
echo 'The X-Ratelimit-Remaining value is: '.$xr['X-Ratelimit-Remaining'];

或者:

$url = Array(
    "0" => "HTTP/1.1 200 OK",
    "Date" => "Sat, 29 May 2004 12:28:14 GMT",
    "Server" => "Apache/1.3.27 (Unix)  (Red-Hat/Linux)",
    "X-Ratelimit-Remaining" => "4976"
);
echo 'The X-Ratelimit-Remaining value is: '.$url['X-Ratelimit-Remaining'];

希望能帮助到你,

祝你好运!

于 2012-10-24T22:15:37.590 回答
0

这是使用 Curl 的替代解决方案:

$ch = curl_init('http://example.com/'); 
curl_setopt_array($ch, array(
    CURLOPT_HEADER => true, 
    CURLOPT_NOBODY => true, 
    CURLOPT_RETURNTRANSFER => true
)); 

$headers = curl_exec($ch);
preg_match('/^X-Ratelimit-Remaining: (.+)$/m', $headers, $m);
list(, $remaining) = $m;

echo '$remaining: ' . $remaining;
于 2012-10-24T22:06:45.020 回答