【问题标题】:Getting request Authorization header in ZF2 controller在 ZF2 控制器中获取请求授权标头
【发布时间】:2013-04-03 10:31:26
【问题描述】:

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

我正在尝试像这样获取控制器中的所有标题:

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

    }

我像这样通过 cURL 发送请求:

curl -i -H "Accept: test" -H "Authorization: 123456" -H "Content-Type: qwerty" -X POST http://localhost/test

所有标题都打印出除了授权标题。我可以添加任意标题并将其打印出来 - 只是没有“授权”标题...

我也尝试过 get()/has() 作为授权标头,但它不存在。

【问题讨论】:

    标签: http zend-framework curl zend-framework2


    【解决方案1】:

    希望这对你在 zf2 中有所帮助

      $headers = $request->getHeaders();
      $authorization = $headers->get('Authorization')->getFieldValue();
    

    $request 是Zend\Http\Request 的对象;

    【讨论】:

      【解决方案2】:

      我终于在这里找到了答案:

      http://zend-framework-community.634137.n4.nabble.com/HTTP-Digest-authentication-does-not-work-with-PHP-as-CGi-td4658790.html

      必须将以下内容添加到项目 .htaccess 中:

      RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
      

      【讨论】:

        【解决方案3】:

        对我来说很好用(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();
        

        希望这会有所帮助:)

        【讨论】:

        • 它对我不起作用。我在他的项目中尝试了一台同事的机器,它在那里工作,但在我的机器上却不行。到底是什么导致这个特定的标头不被解析?!
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-12-25
        • 2020-08-01
        • 2019-03-23
        • 2019-11-11
        • 1970-01-01
        • 2016-02-21
        相关资源
        最近更新 更多