Getting request Authorization header in ZF2 controller
Works fine for me (ZF2 Version 2.1.4):
curl -i -H "Accept: test" -H "Authorization: 123456" -X POST http://zf2.localhost
Results in:
HTTP/1.1 200 OKDate: Thu, 11 Apr 2013 09:44:45 GMTServer: Apache/2.4.3 (Win32) OpenSSL/1.0.1c PHP/5.4.7X-Powered-By: PHP/5.4.7Expires: Thu, 19 Nov 1981 08:52:00 GMTCache-Control: no-store, no-cache, must-revalidate, post-chPragma: no-cacheContent-Length: 843Content-Type: text/htmlobject(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" } }}
With the following code:
$request = $this->getRequest();var_dump($request->getHeaders());
Use the following to get the value:
$authVal = $request->getHeaders('authorization')->getFieldValue();
Hope this helps :)
I finally found the answer here:
Had to add the following to the projects .htaccess:
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
hope this will help you in zf2
$headers = $request->getHeaders(); $authorization = $headers->get('Authorization')->getFieldValue();
$request is object of Zend\Http\Request
;