适合我(ZF2 版本 2.1.4):
curl -i -H "Accept: test" -H "Authorization: 123456" -X POST http://zf2.localhost
结果是:
HTTP/1.1 200 OK
Date: Thu, 11 Apr 2013 09:44:45 GMT
Server: Apache/2.4.3 (Win32) OpenSSL/1.0.1c PHP/5.4.7
X-Powered-By: PHP/5.4.7
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-ch
Pragma: no-cache
Content-Length: 843
Content-Type: text/html
object(Zend\Http\Headers)#168 (3) {
["pluginClassLoader":protected]=>
NULL
["headersKeys":protected]=>
array(4) {
[0]=>
string(9) "useragent"
[1]=>
string(4) "host"
[2]=>
string(6) "accept"
[3]=>
string(13) "authorization"
}
["headers":protected]=>
array(4) {
[0]=>
array(2) {
["name"]=>
string(10) "User-Agent"
["line"]=>
string(23) "User-Agent: curl/7.26.0"
}
[1]=>
array(2) {
["name"]=>
string(4) "Host"
["line"]=>
string(24) "Host: zf2.localhost"
}
[2]=>
array(2) {
["name"]=>
string(6) "Accept"
["line"]=>
string(12) "Accept: test"
}
[3]=>
array(2) {
["name"]=>
string(13) "Authorization"
["line"]=>
string(21) "Authorization: 123456"
}
}
}
使用以下代码:
$request = $this->getRequest();
var_dump($request->getHeaders());
使用以下方法获取值:
$authVal = $request->getHeaders('authorization')->getFieldValue();
希望这可以帮助 :)