在ZF2控制器中获取请求授权标头

Kai*_*aan 3 curl zend-framework http zend-framework2

我正在使用ZF2,出于某种原因,我可以获取我发送的所有标题,除了授权标题 - 就像它被过滤掉了一样.

我试图在控制器中获取所有标题,如下所示:

    public function createAction($data) 
    {
        $request  = $this->request;
        print_r ($request->getHeaders());
        exit();

    }
Run Code Online (Sandbox Code Playgroud)

我通过cURL发送请求,如下所示:

curl -i -H "Accept: test" -H "Authorization: 123456" -H "Content-Type: qwerty" -X POST http://localhost/test
Run Code Online (Sandbox Code Playgroud)

所有标题打印出EXCEPT授权标题.我可以添加任意任意标题并将其打印出来 - 只是没有'授权'标题...

我也试过get()/ has()获取授权头,但它不存在.

Ayd*_*san 6

对我来说很好(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"
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

使用以下代码:

$request  = $this->getRequest();
var_dump($request->getHeaders());
Run Code Online (Sandbox Code Playgroud)

使用以下命令获取值:

$authVal = $request->getHeaders('authorization')->getFieldValue();
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助 :)