How to print all information from an HTTP request to the screen, in PHP

How to print all information from an HTTP request to the screen, in PHP

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Debugging the Invisible: Printing Full HTTP Request Data in PHP

When debugging a web application, you often encounter scenarios where the standard var_dump($_POST) or $_GET arrays feel insufficient. You need to see the raw HTTP envelope—the headers, the method, and potentially the request body—to understand exactly what the client sent versus what your server received. This is particularly useful when diagnosing issues with Content-Type mismatches, missing query parameters, or malformed payloads that aren't automatically parsed into superglobals.

To achieve a comprehensive dump of an incoming HTTP request in PHP, you need to access both the global $_SERVER array and the raw input stream. The $_SERVER array contains most of the header information, but it is often incomplete or case-sensitive depending on how the web server (Apache or Nginx) forwarded the data. For a truly complete picture, you must also handle the request body directly.

Accessing Server Headers

The most straightforward way to inspect incoming headers is via the $_SERVER superglobal. This array maps HTTP header names to lowercase strings with a 'HTTP_' prefix (e.g., HTTP_ACCEPT_LANGUAGE). However, this approach has limitations. Not all headers are guaranteed to be present in $_SERVER, and some web servers strip sensitive information like the actual request body content from this array.

A robust diagnostic tool should iterate through $_SERVER and filter for keys that start with 'HTTP_'. It is also prudent to include the CONTENT_LENGTH and CONTENT_TYPE headers directly, as they are critical for understanding how to parse the incoming data. You can format these values for readability by converting underscores to spaces and capitalizing the first letter of each word.

Handling the Request Body

Printing headers alone leaves a gap when dealing with POST or PUT requests. The raw body content is not stored in $_SERVER. To dump this, you must read from the input stream using the PHP function file_get_contents('php://input'). This reads the raw string sent by the client without parsing it into $_POST or $_GET.

There is a significant trade-off here regarding request size. If a client sends a large payload (for example, a file upload or a JSON blob exceeding memory limits), reading the entire input stream into a variable can cause an Out of Memory error. In production environments, you should always validate CONTENT_LENGTH before attempting to read the body. For diagnostic purposes where safety is less critical than completeness, reading the full content is acceptable provided you are aware of the risk with large requests.

Implementation Example

The following script combines these approaches into a single diagnostic output. It first prints the method and key header information, then iterates through $_SERVER to show all available metadata, and finally attempts to dump the raw body if one exists.

<?php

// Display the HTTP Method
echo "Method: " . $_SERVER['REQUEST_METHOD'] . "\n\n";

// Display Content Metadata
if (isset($_SERVER['CONTENT_LENGTH'])) {
    echo "Content-Length: " . $_SERVER['CONTENT_LENGTH'] . "\n";
}
if (isset($_SERVER['CONTENT_TYPE'])) {
    echo "Content-Type: " . $_SERVER['CONTENT_TYPE'] . "\n";
}

echo "\n--- Server Headers ---\n";

// Iterate through _SERVER to find HTTP headers
foreach ($_SERVER as $key => $value) {
    if (strpos($key, 'HTTP_') === 0 || strpos($key, 'CONTENT_') === 0 || strpos($key, 'REMOTE_') === 0) {
        // Clean up the key for display: HTTP_X_Custom_Header -> X-Custom-Header
        $cleanKey = str_replace('_', ' ', $key);
        $cleanKey = ucwords(strtolower($cleanKey));
        
        echo "$cleanKey: " . htmlspecialchars($value) . "\n";
    }
}

echo "\n--- Raw Request Body ---\n";

// Read raw input from php://input
$rawBody = file_get_contents('php://input');

if ($rawBody !== false && $rawBody !== '') {
    // Output the raw body. Use htmlspecialchars to prevent breaking the output stream if JSON/XML is sent.
    echo "Raw Body:\n" . htmlspecialchars($rawBody);
} else {
    echo "(No body or unable to read)\n";
}

?>

Considerations for Production Use

While this diagnostic approach is perfect for local development or temporary debugging endpoints, it should not be used in production without safeguards. Exposing raw request bodies can reveal sensitive information, such as internal API keys sent by a client, or potentially malicious payloads if the input contains unexpected characters that break the output formatting.

If you are building a framework where this feature is needed regularly, consider wrapping this logic into a dedicated class or trait that handles encoding and limits output size. Additionally, be mindful of performance; logging massive request bodies to disk or sending them to a logging service can quickly exhaust I/O bandwidth. For critical production systems, it is often better to rely on structured logging libraries that support log levels for debug-only data, ensuring this verbose output is gated behind a specific environment flag.

For those working within the Laravel ecosystem, similar functionality exists via dd($request->all()) combined with $request->headers->all(), but understanding the underlying PHP mechanisms ensures you can troubleshoot issues even when the framework's helpers obscure the raw details.

Our network