Getting content body from http post using php CURL

Getting content body from http post using php CURL

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

Stefan Bogdanescu

Founder & Senior Architect

Getting Content Body from HTTP POST Using PHP cURL

When debugging an HTTP POST request, it is common to focus on the headers. However, understanding what actually lands in the body of the request is equally critical, especially when dealing with file uploads or complex payloads. You mentioned being able to see headers using CURLOPT_STDERR and CURLOPT_VERBOSE, but finding the actual content body sent by the client remains elusive. This discrepancy often stems from a misunderstanding of how cURL handles output streams versus response buffers.

To inspect the request body in PHP, you must configure cURL to capture the raw HTTP stream rather than just the server's response. The standard approach involves setting CURLOPT_VERBOSE to 1. This flag instructs cURL to include debugging information in the output stream. However, simply enabling verbose mode is not enough if you are only reading from the default STDOUT or a file pointer that isn't capturing the full debug string correctly.

In your specific scenario, you are uploading an image using application/x-www-form-urlencoded. Your code snippet shows you have already enabled CURLOPT_HEADER to FALSE, which is excellent practice for isolating response data, but it does not automatically dump the request body to a file or variable you can inspect.

The most reliable way to view the request body while maintaining your existing setup is to redirect the verbose output to a specific file handle or stream that you control. In your code, you are using CURLOPT_STDERR. This is correct for capturing debug logs. The issue might be that the content of $fp isn't being displayed as expected in your Lisp application's logging mechanism, or the verbose output format needs adjustment.

To ensure the request body is visible, verify that CURLOPT_VERBOSE is set to 1 and that CURLOPT_STDERR points to a resource you can read immediately after execution. Here is how you can refine your setup:

$fp = fopen("debug.log", "w"); // Open a specific file for debug output
curl_setopt($curl, CURLOPT_VERBOSE, 1);
curl_setopt($curl, CURLOPT_STDERR, $fp);
// ... other options ...
$response = curl_exec($curl);
fclose($fp);

// Read the log file immediately to see the full request
$debugContent = file_get_contents("debug.log");
echo $debugContent;

When CURLOPT_VERBOSE is active, cURL prepends a line starting with > POST followed by the URL. The actual body data follows on subsequent lines, typically prefixed with > or indented spaces. If you are using a proxy for debugging tools like Wireshark or Fiddler, remember that these tools often capture the TCP stream before HTTP parsing occurs. Your PHP cURL call sends raw bytes; ensure your proxy is configured to display the decoded HTTP request line and headers followed by the body, rather than just the raw binary dump which might look like garbage without context.

A common pitfall in your current implementation involves CURLOPT_POSTFIELDS. You are correctly using http_build_query to serialize your file metadata into a string. If the body is not appearing in your logs, double-check that you haven't accidentally disabled the output stream elsewhere or if the proxy tool you are using requires specific settings to decode the multipart or urlencoded payload.

For high-fidelity debugging, especially when interfacing with Drupal 7 REST APIs, consider using CURLOPT_DEBUGFUNCTION. This allows cURL to invoke a user-defined function for each debug event (headers, data, etc.). While this adds complexity, it provides granular control over exactly what is sent and when.

$callback = function($ch, $debug_data) {
    // $debug_data contains the raw output string including headers and body
    file_put_contents("debug_callback.txt", $debug_data . PHP_EOL, FILE_APPEND);
};
curl_setopt($curl, CURLOPT_DEBUGFUNCTION, $callback);

This approach ensures that every byte sent to the server is logged in debug_callback.txt, making it trivial to compare what your Lisp application expects against what Drupal 7 actually receives. Always remember that when sending binary data or large files, ensure the Content-Length header matches the actual size of the body, as mismatches can cause silent failures even if headers look correct.

Our network