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: Wed, 20 Oct 2021 07:30:06 GMT
    [3] => Content-Type: text/html; charset=utf-8
    [4] => Transfer-Encoding: chunked
    [5] => Connection: close
    [6] => Last-Modified: Wed, 20 Oct 2021 07:20:12 GMT
    [7] => Content-language: en
    [8] => Permissions-Policy: interest-cohort=()
    [9] => X-Frame-Options: SAMEORIGIN
    [10] => Set-Cookie: COUNTRY=NA%2C45.112.85.242; expires=Wed, 27-Oct-2021 07:30:06 GMT; Max-Age=604800; path=/; domain=.php.net
    [11] => Set-Cookie: LAST_NEWS=1634715006; expires=Thu, 20-Oct-2022 07:30:06 GMT; Max-Age=31536000; path=/; domain=.php.net
    [12] => Link: <https://www.php.net/index>; rel=shorturl
    [13] => Expires: Wed, 20 Oct 2021 07:30:06 GMT
    [14] => 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
    [Connection] => close
    [cache-control] => must-revalidate, no-cache, no-store, no-transform
    [x-robots-tag] => noarchive, noimageindex
    [content-type] => text/html; charset=UTF-8
    [transfer-encoding] => chunked
    [date] => Wed, 20 Oct 2021 07:30:07 GMT
    [server] => LiteSpeed
    [x-content-type-options] => nosniff
)

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] => must-revalidate, no-cache, no-store, no-transform
    [x-robots-tag] => noarchive, noimageindex
    [content-type] => text/html; charset=UTF-8
    [date] => Wed, 20 Oct 2021 07:30:07 GMT
    [server] => LiteSpeed
    [x-content-type-options] => nosniff
)

PHP

Home Menu