Can PHP Curl Retrieve Response Headers and Body in a Single Request

Can PHP cURL retrieve response headers AND body in a single request?

One solution to this was posted in the PHP documentation comments: http://www.php.net/manual/en/function.curl-exec.php#80442

Code example:

$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 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);

Warning: As noted in the comments below, this may not be reliable when used with proxy servers or when handling certain types of redirects. @Geoffrey's answer may handle these more reliably.

Get Header from PHP cURL response

It converts all headers into an array

// create curl resource
$ch = curl_init();

// set url
curl_setopt($ch, CURLOPT_URL, "example.com");

//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//enable headers
curl_setopt($ch, CURLOPT_HEADER, 1);
//get only headers
curl_setopt($ch, CURLOPT_NOBODY, 1);
// $output contains the output string
$output = curl_exec($ch);

// close curl resource to free up system resources
curl_close($ch);

$headers = [];
$output = rtrim($output);
$data = explode("\n",$output);
$headers['status'] = $data[0];
array_shift($data);

foreach($data as $part){

//some headers will contain ":" character (Location for example), and the part after ":" will be lost, Thanks to @Emanuele
$middle = explode(":",$part,2);

//Supress warning message if $middle[1] does not exist, Thanks to @crayons
if ( !isset($middle[1]) ) { $middle[1] = null; }

$headers[trim($middle[0])] = trim($middle[1]);
}

// Print all headers as array
echo "<pre>";
print_r($headers);
echo "</pre>";

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

Maybe these links can help you:

  • Make curl follow redirects?
  • http://evertpot.com/curl-redirect-requestbody/

PHP CURLOPT_WRITEFUNCTION returns headers in body

https://curl.se/libcurl/c/CURLOPT_WRITEFUNCTION.html

If CURLOPT_HEADER is enabled, which makes header data get passed to the write callback, you can get up to CURL_MAX_HTTP_HEADER bytes of header data passed into it. This usually means 100K.

Either get rid of curl_setopt($ch, CURLOPT_HEADER, 1); or you'll have to parse out the headers from the callback data yourself.

You may also be able to use CURLOPT_HEADERFUNCTION as in this answer:

https://stackoverflow.com/a/41135574/1064767

Retrieve the response code from header cURL php

I think you need to pass $curl to the curl_getinfo method, not the $response

$response = curl_exec($curl);
$theInfo = curl_getinfo($curl);
$http_code = $theInfo['http_code'];

You can see the doco here.. https://www.php.net/manual/en/function.curl-getinfo.php

PHP cURL: Read a specific response header

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'http://www.google.com/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER,true);

$result = curl_exec($ch);

curl_close($ch);

list($headers, $content) = explode("\r\n\r\n",$result,2);

// Print header
foreach (explode("\r\n",$headers) as $hdr)
printf('<p>Header: %s</p>', $hdr);

// Print Content
echo $content;


Related Topics



Leave a reply



Submit