Menu Home

get_headers

osbo.com | PHP | Functions | get_headers

Fetches all the headers sent by the server in response to an HTTP request

Syntax

get_headers ( string $url [, int $format = 0 [, resource $context ]] ) : array

Parameters

url

The target URL.

format

If the optional format parameter is set to non-zero, 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

url

<?

$url = "https://www.php.net";

$return = get_headers($url);

print_r($return);

?>
Array
(
    [0] => HTTP/1.1 200 OK
    [1] => Server: myracloud
    [2] => Date: Tue, 20 Apr 2021 01:40:50 GMT
    [3] => Content-Type: text/html; charset=utf-8
    [4] => Connection: close
    [5] => Last-Modified: Tue, 20 Apr 2021 01:30:12 GMT
    [6] => Content-language: en
    [7] => Permissions-Policy: interest-cohort=()
    [8] => X-Frame-Options: SAMEORIGIN
    [9] => Set-Cookie: COUNTRY=NA%2C198.71.228.76; expires=Tue, 27-Apr-2021 01:40:50 GMT; Max-Age=604800; path=/; domain=.php.net
    [10] => Set-Cookie: LAST_NEWS=1618882850; expires=Wed, 20-Apr-2022 01:40:50 GMT; Max-Age=31536000; path=/; domain=.php.net
    [11] => Link: <https://www.php.net/index>; rel=shorturl
    [12] => Expires: Tue, 20 Apr 2021 01:40:50 GMT
    [13] => Cache-Control: max-age=0
)

format

<?

$url = "https://osbo.com";
$format = 1;

$return = get_headers($url, $format);

print_r($return);

?>
Array
(
    [0] => HTTP/1.1 200 OK
    [Date] => Tue, 20 Apr 2021 01:40:50 GMT
    [Server] => Apache
    [content-security-policy] => frame-ancestors 'self'
    [cache-control] => must-revalidate, no-cache, no-store, no-transform
    [x-content-type-options] => nosniff
    [x-robots-tag] => noarchive, noimageindex
    [Upgrade] => h2,h2c
    [Connection] => Upgrade, close
    [Vary] => Accept-Encoding,User-Agent
    [Content-Type] => text/html; charset=UTF-8
)

context

<?

$options = array('http' => array('method' => 'HEAD'));

$url = "https://osbo.com";
$format = 1;
$context = stream_context_create($options);

$return = get_headers($url, $format, $context);

print_r($return);

?>
Array
(
    [0] => HTTP/1.1 200 OK
    [Date] => Tue, 20 Apr 2021 01:40:50 GMT
    [Server] => Apache
    [content-security-policy] => frame-ancestors 'self'
    [cache-control] => must-revalidate, no-cache, no-store, no-transform
    [x-content-type-options] => nosniff
    [x-robots-tag] => noarchive, noimageindex
    [Upgrade] => h2,h2c
    [Connection] => Upgrade, close
    [Vary] => User-Agent
    [Content-Type] => text/html; charset=UTF-8
)

PHP