array ( 0 => 'index.php', 1 => 'PHP Manual', ), 'head' => array ( 0 => 'UTF-8', 1 => 'pt_BR', ), 'this' => array ( 0 => 'function.get-headers.php', 1 => 'get_headers', ), 'up' => array ( 0 => 'ref.url.php', 1 => 'URL Funções', ), 'prev' => array ( 0 => 'function.base64-encode.php', 1 => 'base64_encode', ), 'next' => array ( 0 => 'function.get-meta-tags.php', 1 => 'get_meta_tags', ), 'alternatives' => array ( ), 'source' => array ( 'lang' => 'pt_BR', 'path' => 'reference/url/functions/get-headers.xml', ), ); $setup["toc"] = $TOC; $setup["toc_deprecated"] = $TOC_DEPRECATED; $setup["parents"] = $PARENTS; manual_setup($setup); ?>
(PHP 5, PHP 7, PHP 8)
get_headers — Retorna todos os cabeçalhos enviados pelo servidor em resposta a uma requisição HTTP
get_headers() retorna um array com os cabeçalhos enviados pelo servidor em resposta a uma requisição HTTP.
url
A URL alvo.
associative
Se o parâmetro opcional associative
estiver
definido como true, get_headers() avalia a resposta e
define as chaves do array.
context
Um recurso de contexto válido criado com
stream_context_create(), ou null
para usar o
contexto padrão
Retorna um array indexado ou associativo com os cabeçalhos, ou false
em
caso de falha.
Versão | Descrição |
---|---|
8.0.0 |
O parâmetro associative foi modificado de int para bool.
|
7.1.0 |
O parâmetro context foi adicionado.
|
Exemplo #1 Exemplo de get_headers()
<?php
$url = 'http://www.example.com';
print_r(get_headers($url));
print_r(get_headers($url, true));
?>
O exemplo acima produzirá algo semelhante a:
Array ( [0] => HTTP/1.1 200 OK [1] => Date: Sat, 29 May 2004 12:28:13 GMT [2] => Server: Apache/1.3.27 (Unix) (Red-Hat/Linux) [3] => Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT [4] => ETag: "3f80f-1b6-3e1cb03b" [5] => Accept-Ranges: bytes [6] => Content-Length: 438 [7] => Connection: close [8] => Content-Type: text/html ) Array ( [0] => HTTP/1.1 200 OK [Date] => Sat, 29 May 2004 12:28:14 GMT [Server] => Apache/1.3.27 (Unix) (Red-Hat/Linux) [Last-Modified] => Wed, 08 Jan 2003 23:11:55 GMT [ETag] => "3f80f-1b6-3e1cb03b" [Accept-Ranges] => bytes [Content-Length] => 438 [Connection] => close [Content-Type] => text/html )
Exemplo #2 Exemplo de get_headers() usando HEAD
<?php
// Por padrão get_headers usa uma requisição GET para obter os cabeçalhos. Se
// você quiser enviar uma requisição HEAD, pode fazer isso utilizando um contexo de stream:
$context = stream_context_create(
[
'http' => array(
'method' => 'HEAD'
)
]
);
$headers = get_headers('http://example.com', false, $context);
?>