Home Menu

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, 04 Aug 2021 00:30:49 GMT
    [3] => Content-Type: text/html; charset=utf-8
    [4] => Transfer-Encoding: chunked
    [5] => Connection: close
    [6] => Last-Modified: Wed, 04 Aug 2021 00:20:14 GMT
    [7] => Content-language: en
    [8] => Permissions-Policy: interest-cohort=()
    [9] => X-Frame-Options: SAMEORIGIN
    [10] => Set-Cookie: COUNTRY=NA%2C198.71.228.76; expires=Wed, 11-Aug-2021 00:30:49 GMT; Max-Age=604800; path=/; domain=.php.net
    [11] => Set-Cookie: LAST_NEWS=1628037049; expires=Thu, 04-Aug-2022 00:30:49 GMT; Max-Age=31536000; path=/; domain=.php.net
    [12] => Link: <https://www.php.net/index>; rel=shorturl
    [13] => Expires: Wed, 04 Aug 2021 00:30:49 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
    [Date] => Wed, 04 Aug 2021 00:30:49 GMT
    [Server] => Apache
    [content-security-policy] => frame-ancestors 'self'
    [cache-control] => must-revalidate, no-cache, no-store, no-transform
    [strict-transport-security] => max-age=86400
    [x-content-type-options] => nosniff
    [x-robots-tag] => noarchive, noimageindex
    [Upgrade] => h2,h2c
    [Connection] => Upgrade, close
    [Vary] => Accept-Encoding,User-Agent
    [Transfer-Encoding] => chunked
    [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] => Wed, 04 Aug 2021 00:30:49 GMT
    [Server] => Apache
    [content-security-policy] => frame-ancestors 'self'
    [cache-control] => must-revalidate, no-cache, no-store, no-transform
    [strict-transport-security] => max-age=86400
    [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

JesusBible
HTMLCSSJSPHPSVGMore
AboutTerms