search for in the  
<base64_encodeget_meta_tags>
Last updated: Thu, 19 May 2005

get_headers

(PHP 5)

get_headers --  Fetches all the headers sent by the server in response to a HTTP request

Description

array get_headers ( string url [, int format] )

get_headers() returns an array with the headers sent by the server in response to a HTTP request. Returns FALSE on failure.

If the optional format parameter is set to 1, get_headers() parses the response and sets the array's keys.

Example 1. get_headers() example

<?php
$url
= 'http://www.example.com';

print_r(get_headers($url));

print_r(get_headers($url, 1));
?>

The above example will output something similar to:

Array
(
    [0] => HTTP/1.1 200 OK
    [1] => Date: Sat, 29 May 2004 12:28:13 GMT
    [2] => Server: Apache/1.3.27 (Unix)  (Red-Hat/Linux)
    [3] => Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT
    [4] => ETag: "3f80f-1b6-3e1cb03b"
    [5] => Accept-Ranges: bytes
    [6] => Content-Length: 438
    [7] => Connection: close
    [8] => Content-Type: text/html
)

Array
(
    [0] => HTTP/1.1 200 OK
    [Date] => Sat, 29 May 2004 12:28:14 GMT
    [Server] => Apache/1.3.27 (Unix)  (Red-Hat/Linux)
    [Last-Modified] => Wed, 08 Jan 2003 23:11:55 GMT
    [ETag] => "3f80f-1b6-3e1cb03b"
    [Accept-Ranges] => bytes
    [Content-Length] => 438
    [Connection] => close
    [Content-Type] => text/html
)



User Contributed Notes
get_headers
david at nothisbit dot futuresbright dot com
05-Apr-2005 06:03
After discovering that some webservers reply with "Content-Type" and others with "Content-type" I modified the function below to use strtolower($key) to make for easy checking against these case differences.
aeontech at gmail dot com
23-Dec-2004 07:20
In response to dotpointer's modification of Jamaz' solution...

Here is a small modification of your function, this adds the emulation of the optional $format parameter.

<?php
if(!function_exists('get_headers')) {
  
  
/**
   * @return array
   * @param string $url
   * @param int $format
   * @desc Fetches all the headers
   * @author cpurruc fh-landshut de
   * @modified by dotpointer
   * @modified by aeontech
   */
  
function get_headers($url,$format=0)
   {
      
$url_info=parse_url($url);
      
$port = isset($url_info['port']) ? $url_info['port'] : 80;
      
$fp=fsockopen($url_info['host'], $port, $errno, $errstr, 30);
      
       if(
$fp)
       {
          
$head = "HEAD ".@$url_info['path']."?".@$url_info['query']." HTTP/1.0\r\nHost: ".@$url_info['host']."\r\n\r\n";     
          
fputs($fp, $head);     
           while(!
feof($fp))
           {
               if(
$header=trim(fgets($fp, 1024)))
               {
                   if(
$format == 1)
                   {
                      
$key = array_shift(explode(':',$header));
                      
// the first element is the http header type, such as HTTP 200 OK,
                       // it doesn't have a separate name, so we have to check for it.
                      
if($key == $header)
                       {
                          
$headers[] = $header;
                       }
                       else
                       {
                          
$headers[$key]=substr($header,strlen($key)+2);
                       }
                       unset(
$key);
                   }
                   else
                   {
                      
$headers[] = $header;
                   }
               }
           }
           return
$headers;
       }
       else
       {
           return
false;
       }
   }
}
?>

<base64_encodeget_meta_tags>
 Last updated: Thu, 19 May 2005
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: The Server Pages
Last updated: Thu May 19 17:35:34 2005 CDT