How to remove HTTP headers from CURL response?
When you make a request using curl_exec, the raw output often includes the full HTTP response, starting with the status line and followed by header fields before the actual body content begins. In your specific case, you are seeing metadata like Date, Server, X-Powered-By, and Content-Length mixed in with your plain text payload (6.8320).
To isolate just the body, you must configure cURL to exclude these headers from the returned string entirely. The most direct approach is setting the CURLOPT_HEADER option to false.
Disabling Header Transfer
By default, curl_exec returns both headers and body if you enable CURLOPT_RETURNTRANSFER. To strip the headers at the source:
$ch = curl_init( $url );
// Set options
curl_setopt( $ch, CURLOPT_POST, true ); // Only if using POST
curl_setopt( $ch, CURLOPT_POSTFIELDS, $_POST ); // Only if using POST
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt( $ch, CURLOPT_HEADER, false ); // This is the key setting
curl_setopt( $ch, CURLOPT_USERAGENT, $_GET['user_agent'] ? $_GET['user_agent'] : $_SERVER['HTTP_USER_AGENT'] );
$response = curl_exec( $ch );
$httpCode = curl_getinfo( $ch, CURLINFO_HTTP_CODE );
curl_close( $ch );
When CURLOPT_HEADER is set to false, the string returned in $response contains only the body. You no longer need to use preg_split or regex patterns to separate the header block from the content. The variable $response will immediately hold just 6.8320.
If you still require the HTTP status code for your logic (which is stored separately via curl_getinfo), this configuration remains valid. The status line (HTTP/1.1 200 OK) is also excluded from the string, but its numeric or textual representation remains accessible through CURLINFO_HTTP_CODE.
Handling Errors and Edge Cases
It is important to remember that disabling headers does not disable error reporting. If the remote server returns an HTTP error status (like 404 or 500), $response will still contain the HTML error message provided by the server, even if you don't want to display it in your UI. You should always check curl_getinfo($ch, CURLINFO_HTTP_CODE) before processing the body to ensure the request succeeded.
Additionally, be aware that some servers might include headers inside the response body itself (a practice known as "chunked encoding" or specific framing), but this is rare for standard text responses. In your scenario, where the payload is simple text, CURLOPT_HEADER => false provides a clean separation.
Why This Works
The HTTP protocol dictates that headers precede the body. When cURL executes a request, it constructs a string containing the entire response stream. The CURLOPT_HEADER flag tells the underlying libcurl library whether to prepend that header section to the data returned in the output variable. By setting it to false, you instruct the library to skip the header parsing step for the return value, effectively giving you a "headless" response object that contains only the payload bytes.
This approach is cleaner and more performant than using regular expressions to split the string, as it avoids potential pitfalls with malformed headers or unexpected line endings in the response stream. It relies on the library's internal buffer management rather than manual string manipulation.