PHP Tutorials - Herong's Tutorial Examples
Dr. Herong Yang, Version 3.00

HttpRequestGet.php - Viewing Header Lines

This section provides a tutorial example script, HttpRequestGet.php, on how to dump the entire HTTP response including all header lines from a Web server.

When a client program receives an HTTP response, it will look at header lines first. Based on the information contained in header lines, the client program will decide what to do with the actual response data in the entity body.

If you use a Web browser as an HTTP client program, it will process the data in the entity body differently depending on mainly the "Content-Type" entity header line: displaying the data as it is, rendering the data as an HTML document and displaying the resulting information, or passing the data to another registered program to handle it.

Once the Web browser finishes processing the entity body, you can get some limited information from the header lines. For example, you can click the right mouse button and select the properties command on Internet Explorer, it will display some general properties about this response in a pop up window. Properties displayed are not always identical to response header lines. The "Modified" property is probably identical to the "Last-Modified" entity header line. The "Type" property is sometime related to the "Content-Type" entity header line, and sometimes related to server side resource that generated the response.

How to view all header lines received in an HTTP response? I couldn't easily find any existing tools to do this. So I wrote the following program to dump the entire response including all header lines received from a Web server:

<?php # HttpRequestGet.php
# Copyright (c) 2005 by Dr. Herong Yang, http://www.herongyang.com/
# 
   $path = "/index.html";
   $port = 80;
   $host = "localhost";
   if (count($argv) > 1) $path = $argv[1];
   if (count($argv) > 2) $port = $argv[2];
   if (count($argv) > 3) $host = $argv[3];

   $address = gethostbyname($host);
   $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
   socket_connect($socket, $address, $port);
   $in = "GET $path HTTP/1.0\r\n\r\n";
   socket_write($socket, $in, strlen($in));
   while ($out = socket_read($socket, 2048)) {
      echo $out;
   }
   socket_close($socket);
?>

Note that:

  • $argv is built-in array that contains all command line arguments, with $argv[0] representing the PHP file name, if you run your PHP file as "php file_name.php ...".
  • You need to make sure that "extension=php_sockets.dll" is turned on in your php.ini configuration file to use socket related functions.

Last update: 2005.

Sections in This Chapter

What Is an HTTP Response?

HTTP Response Header Lines

header() - Inserting a Raw Header Lines

HttpRequestGet.php - Viewing Header Lines

Response Header Lines of Static Files

HttpHeaderLines.php - Examples of Inserting Header Lines

Location: - Forcing the Browser to Redirect to Another URL

Content-Type: - Generating Non-HTML Response Body

Content-Disposition: - Sending Files for Downloading

Dr. Herong Yang, updated in 2009
HttpRequestGet.php - Viewing Header Lines