I am do some curl process, some site must set CURLOPT_HEADER, true, so that could get the html code.
$ch2 = curl_init();
curl_setopt($ch2, CURLOPT_URL, $url);
curl_setopt($ch2, CURLOPT_HEADER, true);
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1);
$html = curl_exec($ch2);
curl_close($ch2);
echo $html;
the return data like:
HTTP/1.0 200 OK Date: Wed, 14 Nov 2012 17:58:26 GMT Expires: Wed, 14 Nov 2012 18:08:26 GMT Cache-Control: max-age=600...
<html...
So how to remove some data before <html>(The CURLOPT_HEADER return data: HTTP/1.0 200 OK...)?
CURLOPT_HEADER does not affect what the site returns to you. You can remove it and if you get empty content back - then something else is wrong.
CURLOPT_HEADER is just for your convenience so you can see what the server said back to your script. Some web API's pass data in the headers and this allows you to access it.
You can split the content from the header like this
list($header, $body) = explode("\r\n\r\n", $content, 2); // Notice the "2" limit!
The solution in the accepted answer is nice and easy, but it won't work as expected if the response is redirected.
Here is another solution that works with redirects, but requires some more lines:
$header_size=curl_getinfo($ch,CURLINFO_HEADER_SIZE);
$header=trim(substr($content,0,$header_size)); //Get the header and trim it to remove \r\n
$body=substr($content,$header_size); //Get the body
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