我发现了为什么会发生这种情况,请查看我的回答
这是唯一发生这种情况的域名,我在一堆网址上运行curl_multi,这个网址带有404 http_code http://www.breakingnews.com
但是当我在浏览器中访问它时,它是200OK(需要一段时间来加载)并且看起来不像是重定向。
任何人都知道了什么?这是一个常见的问题
这是一个var_dump:
["info"]=> array(22) { ["url"]=> string(27) "http://www.breakingnews.com" ["content_type"]=> string(24) "text/html; charset=utf-8" ["http_code"]=> int(404) ["header_size"]=> int(337) ["request_size"]=> int(128) ["filetime"]=> int(-1) ["ssl_verify_result"]=> int(0) ["redirect_count"]=> int(0) ["total_time"]=> float(1.152229) ["namelookup_time"]=> float(0.001261) ["connect_time"]=> float(0.020121) ["pretransfer_time"]=> float(0.020179) ["size_upload"]=> float(0) ["size_download"]=> float(9755) ["speed_download"]=> float(8466) ["speed_upload"]=> float(0) ["download_content_length"]=> float(-1) ["upload_content_length"]=> float(0) ["starttransfer_time"]=> float(1.133522) ["redirect_time"]=> float(0) ["certinfo"]=> array(0) { } ["redirect_url"]=> string(0) "" } ["error"]=> string(0) ""
<击>更新: 这实际上看起来像curl_setopt的一个php bug($ ch,CURLOPT_NOBODY,true); https://bugs.php.net/bug.php?id=39611 击>
编辑:这不是一个错误。答案 0 :(得分:10)
我在评论中找到了答案http://w-shadow.com/blog/2007/08/02/how-to-check-if-page-exists-with-curl/comment-page-1/#comment-12186 通过将CURLOPT_NOBODY设置为true,CURL将使用HEAD作为请求,某些服务器不喜欢该请求(例如,forbes)并将返回“来自服务器的Emply reply”。要解决此问题,您还需要设置CURLOPT_HTTPGET以重置为GET请求。
/* don’t download the page, just the header (much faster in this case) */
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPGET, true); //this is needed to fix the issue
答案 1 :(得分:1)
我不确定你的代码是怎样的,但这样可以正常使用
$url = "http://www.breakingnews.com";
$ch = curl_init ( $url );
curl_setopt ( $ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; pl; rv:1.9) Gecko/2008052906 Firefox/3.0" );
curl_setopt ( $ch, CURLOPT_AUTOREFERER, true );
curl_setopt ( $ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_exec ( $ch );
var_dump ( curl_getinfo ( $ch ) );
if (curl_errno ( $ch )) {
print curl_error ( $ch );
} else {
curl_close ( $ch );
}
输出
array
'url' => string 'http://www.breakingnews.com' (length=27)
'content_type' => string 'text/html; charset=utf-8' (length=24)
'http_code' => int 200
'header_size' => int 330
'request_size' => int 154
'filetime' => int -1
'ssl_verify_result' => int 0
'redirect_count' => int 0
'total_time' => float 4.243
'namelookup_time' => float 0.171
'connect_time' => float 0.374
'pretransfer_time' => float 0.374
'size_upload' => float 0
'size_download' => float 68638
'speed_download' => float 16176
'speed_upload' => float 0
'download_content_length' => float -1
'upload_content_length' => float 0
'starttransfer_time' => float 3.681
'redirect_time' => float 0
'certinfo' =>
array
empty
'redirect_url' => string '' (length=0)