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?
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.
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With