Jesus · Bible · HTML · CSS · JS · PHP · SVG · Applications

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|false

Parameters

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-cache,no-store,no-transform
    [3] => x-robots-tag: noarchive,noimageindex
    [4] => content-type: text/html; charset=UTF-8
    [5] => transfer-encoding: chunked
    [6] => date: Tue, 15 Oct 2024 09:50:00 GMT
    [7] => server: LiteSpeed
    [8] => platform: hostinger
    [9] => strict-transport-security: max-age=31536000; includeSubDomains; preload
    [10] => 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-cache,no-store,no-transform
    [x-robots-tag] => noarchive,noimageindex
    [content-type] => text/html; charset=UTF-8
    [transfer-encoding] => chunked
    [date] => Tue, 15 Oct 2024 09:50:00 GMT
    [server] => LiteSpeed
    [platform] => hostinger
    [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-cache,no-store,no-transform
    [x-robots-tag] => noarchive,noimageindex
    [content-type] => text/html; charset=UTF-8
    [date] => Tue, 15 Oct 2024 09:50:01 GMT
    [server] => LiteSpeed
    [platform] => hostinger
    [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
HomeMenu