有一个函数:
array get_headers ( string $url [, int $format = 0 ] )
Parameters
url
The target URL.
format
If the optional format parameter is set to non-zero, get_headers() parses the response and sets the array's keys.
设置为非0返会解析响应关联数组。
Return Values
Returns an indexed or associative array with the headers, or FALSE on failure.
array getallheaders ( void ) Fetches all HTTP headers from the current request.
This function is an alias for apache_request_headers(). Please read the apache_request_headers() documentation for more information on how this function works.
An associative array of all the HTTP headers in the current request, or FALSE on failure.
我们可以自己写这个函数:
在PHP里,想要得到所有的HTTP请求头,可以使用getallheaders方法,不过此方法并不是在任何环境下都存在,比如说,你使用fastcgi方式运行PHP的话,就没有这个方法,所以说我们还需要考虑别的方法,幸运的是$_SERVER里有我们想要的东西,它里面键名以HTTP_开头的就是HTTP请求头:
$headers = array();
foreach ($_SERVER as $key => $value) {
if ('HTTP_' == substr($key, 0, 5)) {
$headers[str_replace('_', '-', substr($key, 5))] = $value;
}
} 代码很简单,需要说明的是RFC里明确指出了信息头的名字是不区分大小写的。
不过并不是所有的HTTP请求头都是以HTTP_开头的的键的形式存在与$_SERVER里,比如说Authorization,Content-Length,Content-Type就不是这样,所以说为了取得所有的HTTP请求头,还需要加上下面这段代码: