Php 删除CULLOPT_标题返回数据
我正在做一些curl过程,一些站点必须设置Php 删除CULLOPT_标题返回数据,php,curl,Php,Curl,我正在做一些curl过程,一些站点必须设置CURLOPT_头,true,这样才能得到html代码 $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;
CURLOPT_头,true
,这样才能得到html代码
$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;
返回数据如下所示:
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...
HTTP/1.0 200确定日期:2012年11月14日星期三17:58:26 GMT过期时间:2012年11月14日星期三18:08:26 GMT缓存控制:最大年龄=600。。。
CURLOPT_标题
不会影响网站返回给您的内容。你可以删除它,如果你得到了空的内容回来-那么其他东西是错误的
CURLOPT_HEADER
只是为了方便您,这样您就可以看到服务器对您的脚本说了些什么。一些web API在标题中传递数据,这允许您访问它
您可以像这样从标题中拆分内容
list($header, $body) = explode("\r\n\r\n", $content, 2); // Notice the "2" limit!
被接受的答案中的解决方案是好的和简单的,但是如果响应被重定向,它将不会像预期的那样工作
下面是另一个使用重定向的解决方案,但需要更多的行:
$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