get_headers
Description
The get_headers of URL for PHP fetches all the headers sent by the server in response to an HTTP request.
Syntax
get_headers(
string $url,
bool $associative = false,
?resource $context = null
): array|falseParameters
url
The target URL.
associative
If the optional associative parameter is set to true, get_headers() parses the response and sets the array's keys.
context
A valid context resource created with stream_context_create().
Return
Returns an indexed or associative array with the headers, or false on failure.
Examples
1 · url
<? $url = "https://osbo.com"; $return = get_headers($url); print_r($return);
Array
(
[0] => HTTP/1.1 200 OK
[1] => Connection: close
[2] => Cache-Control: no-store,no-transform
[3] => link: <https://osbo.com/>;rel="canonical"
[4] => x-robots-tag: noarchive,noimageindex
[5] => Content-Type: text/html; charset=UTF-8
[6] => Transfer-Encoding: chunked
[7] => Date: Tue, 17 Mar 2026 15:44:33 GMT
[8] => Strict-Transport-Security: max-age=31536000; includeSubDomains; preload
[9] => X-Content-Type-Options: nosniff
)
2 · associative
<? $url = "https://osbo.com"; $associative = true; $return = get_headers($url, $associative); print_r($return);
Array
(
[0] => HTTP/1.1 200 OK
[Connection] => close
[Cache-Control] => no-store,no-transform
[link] => <https://osbo.com/>;rel="canonical"
[x-robots-tag] => noarchive,noimageindex
[Content-Type] => text/html; charset=UTF-8
[Transfer-Encoding] => chunked
[Date] => Tue, 17 Mar 2026 15:44:33 GMT
[Strict-Transport-Security] => max-age=31536000; includeSubDomains; preload
[X-Content-Type-Options] => nosniff
)
3 · context
<?
$options =
[
'http' =>
[
'method' => 'HEAD'
]
];
$url = "https://osbo.com";
$associative = true;
$context = stream_context_create($options);
$return = get_headers($url, $associative, $context);
print_r($return);
Array
(
[0] => HTTP/1.1 200 OK
[Connection] => close
[Cache-Control] => no-store,no-transform
[link] => <https://osbo.com/>;rel="canonical"
[x-robots-tag] => noarchive,noimageindex
[Content-Type] => text/html; charset=UTF-8
[Date] => Tue, 17 Mar 2026 15:44:34 GMT
[Strict-Transport-Security] => max-age=31536000; includeSubDomains; preload
[X-Content-Type-Options] => nosniff
)
4 · status
<? $url = "https://osbo.com"; $return = get_headers($url); $string = $return[0]; $offset = 9; $length = 3; $status = substr($string, $offset, $length); echo $status;
200