12

Ive got a code

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, 1);        
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
    $ret = curl_exec($ch);
    $responseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

Now i want to extract the body from $ret without headers. Any idea how to do it?

Michal
  • 608
  • 1
  • 5
  • 12

3 Answers3

43

raina77ow's response is good. From the comments it looks like there are a few conditions that would cause the "explode" to not work. I found a way that works better for me and thought I'd share.

By calling curl_getinfo($ch, CURLINFO_HEADER_SIZE) you can get the length of the response header. So I did the following:

// Original Code
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);        
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
$ret = curl_exec($ch);
$responseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

// Added Code
$header_len = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($ret, 0, $header_len);
$body = substr($ret, $header_len);

// Back to Original Code
curl_close($ch);

This is working great for me. I hope it helps someone else.

natiupiru
  • 431
  • 1
  • 4
  • 2
  • It seems `CURLOPT_BINARYTRANSFER` doesn't do anything anymore. [see this SO question](http://stackoverflow.com/questions/6007363/what-does-curlopt-binarytranfer-exactly-mean) – jxmallett Oct 27 '16 at 01:01
  • Perfect answer... at last I'm saved from those bad scripts that just split by '\n\n' – leedch May 18 '17 at 14:30
22

How about this:

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

... which should give you the body of your request in the $content variable?

raina77ow
  • 103,633
  • 15
  • 192
  • 229
2

Drawing on @Geoffrey excellent answer in this thread...

Some of the other solutions offered this thread are not doing this correctly.

  • Splitting on \r\n\r\n is not reliable when CURLOPT_FOLLOWLOCATION is on or when the server responds with a 100 code.
  • Detecting the size of the headers via CURLINFO_HEADER_SIZE is also not always reliable, especially when proxies are used or in some of the same redirection scenarios.

The most reliable method of obtaining headers is using CURLOPT_HEADERFUNCTION. Then you can remove curl_setopt($ch, CURLOPT_HEADER, 1); and your body will be clean with no headers.

Here is a very clean method of performing this using PHP closures. It also converts all headers to lowercase for consistent handling across servers and HTTP versions.

$ch = curl_init();
$headers = [];
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

// Run the following function for each header received
// Note: Does NOT handle multiple instances of the same header. Later one will overwrite.
//       Consider handling that scenario if you need it.
curl_setopt($ch, CURLOPT_HEADERFUNCTION, function($curl, $header) use (&$headers) {
    $len = strlen($header);
    $header = explode(':', $header, 2);
    if (count($header) >= 2)
        $headers[strtolower(trim($header[0]))] = trim($header[1]);
    return $len;
});

$data = curl_exec($ch);
print_r($headers);
Simon East
  • 55,742
  • 17
  • 139
  • 133