gremo gremo - 1 year ago 772
PHP Question

PHP cURL retrieving response headers AND body in a single request?

Is there any way to get both headers and body for a cURL request using PHP? I found that this option:

curl_setopt($ch, CURLOPT_HEADER, true);

is going to return body plus headers, but then i need to parse it to get the body. Any way to get both in a more usable (and secure) way?

EDIT: for "single request" i mean avoind issuing a HEAD request prior of GET/POST.

Answer Source

Yes. For details, please take a look at this post in the PHP documentation:

Code example:

$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
// ...

$response = curl_exec($ch);

// Then, after your curl_exec call:
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($response, 0, $header_size);
$body = substr($response, $header_size);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download