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

get_headers

Description

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

1 · 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: Fri, 23 Feb 2024 20:33:07 GMT
    [3] => Content-Type: text/html; charset=utf-8
    [4] => Transfer-Encoding: chunked
    [5] => Connection: close
    [6] => Last-Modified: Fri, 23 Feb 2024 20:20:10 GMT
    [7] => Content-language: en
    [8] => Permissions-Policy: interest-cohort=()
    [9] => X-Frame-Options: SAMEORIGIN
    [10] => Set-Cookie: COUNTRY=NA%2C80.90.13.131; expires=Fri, 01 Mar 2024 20:33:07 GMT; Max-Age=604800; path=/; domain=.php.net
    [11] => Set-Cookie: LAST_NEWS=1708720387; expires=Sat, 22 Feb 2025 20:33:07 GMT; Max-Age=31536000; path=/; domain=.php.net
    [12] => Link: <https://www.php.net/index>; rel=shorturl
    [13] => Expires: Fri, 23 Feb 2024 20:33:07 GMT
    [14] => Cache-Control: max-age=0
)

2 · format

<?

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

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

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] => Fri, 23 Feb 2024 20:33:07 GMT
    [server] => LiteSpeed
    [strict-transport-security] => max-age=31536000; includeSubDomains; preload
    [x-content-type-options] => nosniff
)

3 · 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
    [Connection] => close
    [cache-control] => no-cache,no-store,no-transform
    [x-robots-tag] => noarchive,noimageindex
    [content-type] => text/html; charset=UTF-8
    [date] => Fri, 23 Feb 2024 20:33:07 GMT
    [server] => LiteSpeed
    [strict-transport-security] => max-age=31536000; includeSubDomains; preload
    [x-content-type-options] => nosniff
)
HomeMenu