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) 2003-2019, HerongYang.com, All Rights Reserved.
# 
   $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:

Last update: 2019.

Table of Contents

 About This Book

 Introduction and Installation of PHP 7.3

 PHP Script File Syntax

 PHP Data Types and Data Literals

 Variables, References, and Constants

 Expressions, Operations and Type Conversions

 Conditional Statements - "if" and "switch"

 Loop Statements - "while", "for", and "do ... while"

 Function Declaration, Arguments, and Return Values

 Arrays - Ordered Maps

 Introduction of Class and Object

 Integrating PHP with Apache Web Server

 Retrieving Information from HTTP Requests

 Creating and Managing Sessions in PHP Scripts

 Sending and Receiving Cookies in PHP Scripts

Controlling HTTP Response Header Lines in PHP Scripts

 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

 MySQL Server Connection and Access Functions

 Functions to Manage Directories, Files and Images

 SOAP Extension Function and Calling Web Services

 SOAP Server Functions and Examples

 Localization Overview of Web Applications

 Using Non-ASCII Characters in HTML Documents

 Using Non-ASCII Characters as PHP Script String Literals

 Receiving Non-ASCII Characters from Input Forms

 "mbstring" Extension and Non-ASCII Encoding Management

 Managing Non-ASCII Character Strings with MySQL Servers

 Configuring and Sending out Emails

 Outdated Tutorials

 References

 Full Version in PDF/EPUB