We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

getHeader() problems

Hi, I want use following HMAC REST api powered by phalcon : https://github.com/jeteokeeffe/php-hmac-rest-api

I have confuse about getting header values with below command , I'm set custom header like API_ID, ... but following code return NULL!

$app->request->getHeader('API_ID');

Also I check this method for fetch all header values :

$app->request->getHeaders();

Result :

array (
  'Accept' => '*/*',
  'Content-Length' => '0',
  'Content-Type' => 'application/x-www-form-urlencoded; charset=windows-1256',
  'Host' => 'localhost',
  'Connection' => 'Keep-Alive',
  'User-Agent' => 'Apache-HttpClient/4.3.6 (java 1.5)',
  'Accept-Encoding' => 'gzip,deflate',
)

Also I get all header values using getallheaders() native PHP function Result :

array (
  'Accept' => '*/*',
  'API_ID' => '110',
  'API_TIME' => '145215523',
  'API_HASH' => 'sadsdadt5verwervearevaerv4ae4r5ea4rva',
  'Content-Length' => '0',
  'Content-Type' => 'application/x-www-form-urlencoded; charset=windows-1256',
  'Host' => 'localhost',
  'Connection' => 'Keep-Alive',
  'User-Agent' => 'Apache-HttpClient/4.3.6 (java 1.5)',
  'Accept-Encoding' => 'gzip,deflate',
)

What happend to my custom value in header?

Thanks.



622
Accepted
answer
edited Sep '15

$app->request->getHeaders(); works by iterating over $_SERVER keys and extracting KeyValue pairs when the key starts with HTTP_ or if it belongs to the content headers whitelist (["CONTENT_TYPE", "CONTENT_LENGTH"]).

see: https://github.com/phalcon/cphalcon/blob/f0b1bd64536a93f09a2725d34fc60c12642ca946/phalcon/http/request.zep#L761

How do you inject your custom headers, are they really sent by client or set by HTTP Daemon or application bootstrap? If so, please show your code.

Won't help you, but keep in mind that custom HTTP headers are supposed to be prefixed with X- to avoid naming collision and triggering any default behavior switch in your HTTP daemon stack.

I using PhpStorm REST Client for setting headers. I see that when I use - instead of _ in my client, the $app->request->getHeaders(); return my value and everythings is Ok.

So I have to change my variable name from API_ID to API-ID

Thanks for your reply.

edited Dec '15

There's an example nginx configuration on that project. It states:

# Very Important
underscores_in_headers on;

Also, you can fetch the custom headers from the superglobal $_SERVER assoc array. But the webserver will just ditch headers with underline if you don't set it properly to process underscores in headers.