在请求HTTP代码时出现cURL错误

时间:2015-01-08 22:16:57

标签: php curl

我现在有了这个代码(在从这个问题中修改了补丁:Check if website is available in fastest possible way

foreach($links as $link_content)
{
                $handle = curl_init(LINK_BASE.$link_content);
                curl_setopt( $c, CURLOPT_RETURNTRANSFER, true );
                curl_setopt( $c, CURLOPT_CUSTOMREQUEST, 'HEAD' );
                curl_setopt( $c, CURLOPT_HEADER, 1 );
                curl_setopt( $c, CURLOPT_NOBODY, true );
                $content = curl_exec ($handle);
                curl_close ($handle); //warning there!!!
                $httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
                if($httpCode != 200)
                    continue; //if not, go to next link

}

错误日志:

Warning: curl_getinfo(): 17 is not a valid cURL handle resource in C:\xampp\htdocs\index.php on line 82

Warning: curl_getinfo(): 18 is not a valid cURL handle resource in C:\xampp\htdocs\index.php on line 82

Warning: curl_getinfo(): 19 is not a valid cURL handle resource in C:\xampp\htdocs\index.php on line 82

我不确定是什么引起了这个警告。此外,代码不能按预期方式工作。我会在每种情况下继续循环,当网站可用并返回代码200时。你能给我任何提示吗?

1 个答案:

答案 0 :(得分:1)

您正在调用您的cURL句柄$handle

$handle = curl_init(LINK_BASE.$link_content);

但是您尝试在$c来电中使用curl_setopt

curl_setopt( $c, CURLOPT_RETURNTRANSFER, true );