How to get info on sent PHP curl request
When debugging a curl request in PHP, specifically when facing a persistent 401 Unauthorized error despite correct credentials, standard diagnostics often fall short. The core issue usually isn't just that the request failed, but that the tools available within the PHP curl extension hide critical details by default. You need to move beyond the high-level summary provided by curl_getinfo() and inspect the raw HTTP transaction itself.
The most immediate red flag in your scenario is the method being used: HEAD. Your code sets CURLOPT_NOBODY to 1, which instructs curl to perform a HEAD request rather than GET. This is a significant pitfall because many APIs, including authentication endpoints, do not handle HEAD requests correctly during the login phase. A server might reject a HEAD request for /export/auth simply because it expects a full payload or specific headers associated with a GET request. The fact that you are receiving a 401 suggests the server processed the request but rejected the credentials or the method combination, yet your current setup doesn't capture the full reason.
To see exactly what is being sent, including the raw URL path and all headers, you must disable the CURLOPT_NOBODY flag when retrieving the header information. The CURLINFO_HEADER_OUT option does capture the request line, but only if a body or full response isn't explicitly suppressed in a way that truncates the output. However, a more robust approach for deep inspection involves enabling verbose logging directly to a file handle.
Here is how you should restructure your initialization and execution block to ensure you capture every byte sent:
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_URL, $url); // Ensure URL is explicit
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HEADER, 1);
// CRITICAL: Do not set CURLOPT_NOBODY unless you intentionally want a HEAD request.
// For auth endpoints, almost always use GET or POST to avoid silent failures.
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
// Redirect stderr to a file for raw verbose output
$verbose = fopen('php://temp', 'rw+');
curl_setopt($ch, CURLOPT_STDERR, $verbose);
curl_exec($ch);
// Retrieve the request headers sent
$sent_request = curl_getinfo($ch, CURLINFO_HEADER_OUT);
echo "Request Headers Sent:\n";
echo $sent_request . "\n\n";
// Retrieve verbose log (includes connection details, DNS lookups, and full header dump)
rewind($verbose);
$verboseLog = stream_get_contents($verbose);
echo "Verbose Output:\n";
echo htmlspecialchars($verboseLog);
By removing CURLOPT_NOBODY, you ensure that curl treats the request as a standard GET (or POST, if applicable), which is how most authentication endpoints expect to be called. This change alone might resolve the 401 if the endpoint was rejecting HEAD requests silently.
Additionally, pay close attention to the "Verbose Output." When CURLOPT_VERBOSE is enabled and redirected to a stream, it prints more than just headers. It includes the TCP connection status, DNS resolution details, SSL handshake logs, and crucially, the exact Content-Length and Host headers being constructed by the client library. This level of detail allows you to verify if the Authorization header is actually being encoded correctly as Base64. If the credentials are correct but still rejected, the verbose log will often reveal if the server expects a specific header format or if there is an issue with the SSL certificate chain preventing the handshake before authentication logic even runs.
Remember that the URL path is part of the request line (GET /path HTTP/1.1), not just the Host header. The Host header tells the server which virtual host to use, but the request line determines the specific API endpoint being called. If your $url variable contains only the domain without the path, or if it includes query parameters that should be separate, the server will interpret the request differently than intended.
Finally, ensure you are not inadvertently stripping headers or modifying the request object before execution. In complex scenarios involving proxies or redirects, CURLOPT_FOLLOWLOCATION might interfere with debugging unless explicitly controlled. For authentication endpoints, it is often safer to disable automatic redirects (CURLOPT_FOLLOWLOCATION set to false) to isolate whether the initial 401 comes from the auth gateway or a redirect loop.
