(PHP 5, PHP 7, PHP 8)
headers_list — Devuelve la lista de los encabezados de respuesta del script actual
headers_list() devuelve un array con la lista de los encabezados que serán transmitidos al navegador. Para determinar si estos encabezados han sido ya enviados o no, utilice la función headers_sent() .
Esta función no contiene ningún parámetro.
Devuelve un array de encabezados indexado numéricamente.
Ejemplo #1 Ejemplo con headers_list()
<?php
/* setcookie() va añadir un encabezado */
setcookie('foo', 'bar');
/* Define un encabezado de respuesta
Será ignorado por la mayoría de los navegadores */
header("Example-Test: foo");
/* Especificación de la respuesta en texto simple */
header('Content-Type: text/plain; charset=UTF-8');
/* ¿Cuáles son los encabezados que serán enviados? */
var_dump(headers_list());
?>Resultado del ejemplo anterior es similar a:
array(4) {
[0]=>
string(19) "Set-Cookie: foo=bar"
[1]=>
string(17) "Example-Test: foo"
[2]=>
string(39) "Content-Type: text/plain; charset=UTF-8"
}
Nota: Los encabezados solo serán accesibles y se mostrarán cuando se utilice un SAPI que los soporte.
note that it does not return the status header
<?php
header('HTTP/1.1 301 Moved Permanently', true, 301);
header('foo: bar');
header('a: b');
header('colon less example');
print_r(headers_list());
?>
Array
(
[0] => X-Powered-By: PHP/5.4.7
[1] => foo: bar
[2] => a: b
)This function won't work for when you're running PHP from the command line. If will always return an empty array. This can be an issue when testing your project using PHPUnit or Codeception.
To solve this, install the xdebug extension and use `xdebug_get_headers` when on the cli.
<?php
$headers = php_sapi_name() === 'cli' ? xdebug_get_headers() : headers_list();
?>