PHP - URL get_headers() Function



The PHP URL get_headers() function is used to fetch all headers sent by the server in response to an HTTP request. It return an array with headers sent by the server in response to an HTTP request.

Syntax

Below is the syntax of the PHP URL get_headers() function −

array get_headers(string $url, int $format = 0, ?resource $context = null)

Parameters

Below are the parameters of the get_headers() function −

  • $url − It is the URL to fetch the headers from.

  • $format − It specifies how the headers should be indexed. Its default value is 0.

  • $context − It is the valid context resource which is created using stream_context_create().

Return Value

The get_headers() function returns an array of headers. And FALSE on failure.

PHP Version

First introduced in core PHP 5, the get_headers() function continues to function easily in PHP 7, and PHP 8.

Example 1

First we will show you the basic example of the PHP URL get_headers() function to fetch headers from the given URL.

<?php
   // Define the url here
   $url = "http://www.example.com";
   $headers = get_headers($url);
   print_r($headers);
?>

Output

The above code will result something like this −

Array
(
    [0] => HTTP/1.1 200 OK
    [1] => Accept-Ranges: bytes
    [2] => Age: 482592
    [3] => Cache-Control: max-age=604800
    [4] => Content-Type: text/html; charset=UTF-8
    [5] => Date: Wed, 24 Jul 2024 08:45:42 GMT
    [6] => Etag: "3147526947"
    [7] => Expires: Wed, 31 Jul 2024 08:45:42 GMT
    [8] => Last-Modified: Thu, 17 Oct 2019 07:18:26 GMT
    [9] => Server: ECAcc (dcd/7D43)
    [10] => Vary: Accept-Encoding
    [11] => X-Cache: HIT
    [12] => Content-Length: 1256
    [13] => Connection: close
)

Example 2

Now we will try to use the get_headers() function and fetch headers and return the result as an associative array.

<?php
   /// Define url here
   $url = "http://www.tutorix.com";
   $headers = get_headers($url, 1);
   print_r($headers);
?> 

Output

This will generate the below output −

Array
(
    [0] => HTTP/1.1 302 Moved Temporarily
    [Content-Type] => Array
        (
            [0] => text/html; charset=iso-8859-1
            [1] => text/html; charset=UTF-8
        )

    [Content-Length] => 210
    [Connection] => Array
        (
            [0] => close
            [1] => close
        )

    [Date] => Array
        (
            [0] => Wed, 24 Jul 2024 08:46:34 GMT
            [1] => Wed, 24 Jul 2024 08:46:35 GMT
        )

    [Server] => Array
        (
            [0] => Apache
            [1] => Apache
        )
)

Example 3

Now the below code we are using get_headers() function and fetch headers. And then we will check if a specific header for example Content-Type is present or not and show the content accordingly.

<?php
   // Define url here
   $url = "http://www.tutorix.com";
   $headers = get_headers($url, 1);
   
   if (isset($headers['Content-Type'])) {
       echo"Content-Type: "; 
       print_r($headers['Content-Type']);
   } else {
       echo "Content-Type header not found.";
   }
?> 

Output

This will create the below output −

Content-Type: Array
(
    [0] => text/html; charset=iso-8859-1
    [1] => text/html; charset=UTF-8
)

Example 4

In the following example, we are using the get_headers() function to get headers using a custom context.

<?php
   // Define url here
   $url = "http://www.tutorix.com";
   $options = array(
      "http" => array(
          "method" => "GET",
          "header" => "User-Agent: PHP"
      )
   );
   $context = stream_context_create($options);
   $headers = get_headers($url, 0, $context);
   print_r($headers);
?> 

Output

Following is the output of the above code −

Array
(
    [0] => HTTP/1.1 302 Moved Temporarily
    [1] => Content-Type: text/html; charset=iso-8859-1
    [2] => Content-Length: 210
    [3] => Connection: close
    [4] => Date: Wed, 24 Jul 2024 08:46:34 GMT
    [5] => Server: Apache
    [6] => Location: https://origin.tutorix.com
    [7] => X-Cache: Hit from cloudfront
    [8] => Via: 1.1 6c978f94bb9ef67954e101caa67e6c38.cloudfront.net (CloudFront)
    [9] => X-Amz-Cf-Pop: DEL51-P1
    [10] => X-Amz-Cf-Id: UmSPYyvcQMuJqO3qmWr2GJvpyBRa738Sj3cufsVJ5PbiYtP_a5PZCw==
    [11] => Age: 315
    [12] => HTTP/1.1 200 OK
    [13] => Date: Wed, 24 Jul 2024 08:51:50 GMT
    [14] => Server: Apache
    [15] => X-Powered-By: PHP/7.2.22
    [16] => Set-Cookie: TUTORIXSESSIONID=ecso7cina18m2ine1os429hrr8; expires=Thu, 25-Jul-2024 08:51:50 GMT; Max-Age=86400; path=/
    [17] => Vary: Accept-Encoding
    [18] => Access-Control-Allow-Origin: *
    [19] => Cache-Control: max-age=3604800, public
    [20] => Connection: close
    [21] => Transfer-Encoding: chunked
    [22] => Content-Type: text/html; charset=UTF-8
)
php_function_reference.htm
Advertisements