PHP | get_headers() Function

The get_headers() function in PHP is used to fetch all the headers sent by the server in the response of an HTTP request.

Syntax:

get_headers( $url, $format, $context )

Parameters: This function accepts three parameters as mentioned above and described below:



  • $url: It is a mandatory parameter of type string. It defines the target URL.
  • $format: It is an optional parameter of type int. If its value is set to non-zero it will return an associative array otherwise indexed array.
  • $context: It holds the valid resource context created by stream_context_create() function.

Example 1: In this example, value of optional parameter $format is not assigned.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Target URL
  
// Fetching headers
$headers = get_headers($url);
  
// Printing headers
print_r($headers);
?>

chevron_right


Output:

Array (
     [0] => HTTP/1.1 200 OK 
     [1] => Content-Type: text/html; charset=UTF-8 
     [2] => Connection: close 
     [3] => Date: Sun, 19 May 2019 08:31:29 GMT 
     [4] => Server: Apache 
     [5] => Strict-Transport-Security: max-age=3600; includeSubDomains 
     [6] => Cache-Control: s-maxage=21600, max-age=3, must-revalidate 
     [7] => Access-Control-Allow-Credentials: true 
     [8] => X-Frame-Options: DENY 
     [9] => X-Content-Type-Options: nosniff 
     [10] => Vary: Accept-Encoding, Cookie 
     [11] => X-Cache: Miss from cloudfront 
     [12] => Via: 1.1 aa0bb866c09b4e243eb9a97bcdb7fe32.cloudfront.net (CloudFront) 
     [13] => X-Amz-Cf-Id: QAOIIj4eBsrX0hyZ-UHjOtqA2dQePcLbEUZJ3KRohjsSPfcrcAFaiQ== 
) 

Example 2: In this example, value of optional parameter $format is set to non-zero.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Target URL
  
// Fetching headers
$headers = get_headers($url, 1);
  
// Printing headers
print_r($headers);
?>

chevron_right


Output:

Array ( 
        [0] => HTTP/1.1 200 OK 
        [Content-Type] => text/html; charset=UTF-8 
        [Connection] => close 
        [Date] => Sun, 19 May 2019 08:35:47 GMT 
        [Server] => Apache 
        [Strict-Transport-Security] => max-age=3600; includeSubDomains 
        [Cache-Control] => s-maxage=21600, max-age=3, must-revalidate 
        [Access-Control-Allow-Credentials] => true 
        [X-Frame-Options] => DENY 
        [X-Content-Type-Options] => nosniff 
        [Vary] => Accept-Encoding, Cookie 
        [X-Cache] => Miss from cloudfront 
        [Via] => 1.1 95d17b4d563934eb90636ad03f8f524e.cloudfront.net (CloudFront) 
        [X-Amz-Cf-Id] => se3QRyaWDeuHI3GrisMzAr4FJBamqMtbUNzhTPqAJhBoQZbWvy3UPw== 
) 


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.