0

I'm trying to reach Apache response headers in our CakePHP 3 application.

apache_response_headers() and headers_list() gives this data:

response headers:Array
(
    [Expires] => Thu, 19 Nov 1981 08:52:00 GMT
    [Cache-Control] => no-store, no-cache, must-revalidate, post-check=0, pre-check=0
    [Pragma] => no-cache
)
headers list:Array
(
    [0] => Expires: Thu, 19 Nov 1981 08:52:00 GMT
    [1] => Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
    [2] => Pragma: no-cache
)

When I check response headers from browser, additionally I see these values: "content-encoding, content-length, content-type, date, ms-author-via, server, status, vary, via, x-original-content-length, x-powered-by". Does Cakephp's response object has a get all headers method ? https://github.com/cakephp/cakephp/blob/3.0.11/src/Network/Response.php

How can I get all response headers ?

trante
  • 33,518
  • 47
  • 192
  • 272

2 Answers2

1

If the information is not there when using vanilla PHP commands, Cake wouldn't be able to do any better.

I believe your problem lies to output_buffering being enabled in php.ini. This will cause the response to delay until the script exits and you obviously won't be having the complete response headers in PHP during runtime. You can turn output buffering off or flush your output before checking the headers.

user221931
  • 1,852
  • 1
  • 13
  • 16
1

Did you try this function?

$this->response->header()

This function allows you to set headers and it'll always return you the list of header defined. And even if you don't give any value to this function, it will just return you the list of headers.

SamHecquet
  • 1,818
  • 4
  • 19
  • 26
  • In CakePHP 3.5+, [use](https://stackoverflow.com/questions/43184833/cakephp3-4-how-to-send-a-json-object-response) `$this->response = $this->response->withHeader('X-something', 'a value')` – ᴍᴇʜᴏᴠ Jun 30 '20 at 06:08