Php 卷曲停止工作

Php 卷曲停止工作,php,curl,Php,Curl,我想从列表中搜索一些关于单词的链接。 因此,我正在制作一个脚本: //html code here. <? if (array_key_exists('form_action', $_POST)){ $pel=$_POST['url']; $toplist=file_get_contents($pel); $listgrabbing=explode("\r\n",$toplist); foreach($listgrabbing as $item) { $useragent="Mo

我想从列表中搜索一些关于单词的链接。 因此,我正在制作一个脚本:

//html code here.
<?
if (array_key_exists('form_action', $_POST)){
$pel=$_POST['url'];
$toplist=file_get_contents($pel);
$listgrabbing=explode("\r\n",$toplist);
foreach($listgrabbing as $item)
{    

$useragent="Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 5.1; .NET CLR 1.1.4322; Alexa Toolbar; .NET CLR 2.0.50727)";
$urlto=$item;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $urlto);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLOPT_COOKIEJAR, "COOKIE.txt"); 
curl_setopt($ch, CURLOPT_COOKIEFILE, "COOKIE.txt"); 
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,10); 
$buffer = curl_exec($ch);
$po = strpos($buffer,"article");
if ($po===false)
{
echo ($item."---Word didn't found!");
echo "<br>";
}
else {
echo ($item."---Word Found!");
echo "<br>";
}
}
}
?>
//这里是html代码。

尝试选项
CURLOPT\u LOW\u SPEED\u TIME
CURLOPT\u LOW\u SPEED\u LIMIT

// the download speed must be at least 1 byte per second
curl_setopt(CURLOPT_LOW_SPEED_LIMIT, 1);
// if the download speed is below 1 byte per second for
// more than 30 seconds curl will give up
curl_setopt(CURLOPT_LOW_SPEED_TIME, 30);
如果给定的超时下载速率低于给定的阈值,这将防止curl在慢速或死机连接上“挂起”。达到超时时,您可以重试或跳过url:

// skips the url if errors on download
$buffer = curl_exec($ch);
if ($buffer === FALSE) { 
    echo curl_error($ch);
    continue;
}
“停止工作”可能有几个原因。最简单的是,远程服务器在响应过程中崩溃,而没有发送aTCP FIN。(我在野外见过这种情况)。因此底层TCP连接不会被关闭,curl将永远等待剩余的字节

另一个原因可能是防火墙规则在建立连接后的传输期间阻止端口。不太可能,但也可以在野外看到

我可以想象的另一个原因是,远程服务器计算错误的“内容长度”HTTP头。再加上HTTP/1.1的“连接:保持活动”,这可能会使curl在等待重新生成永远不会发送的字节时“挂起”。要防止出现这种情况,应明确使用标题“Connection:close”。这可以通过以下方式完成:

curl_setopt(CURLOPT_HTTPHEADER, array('Connection: close'));

不过,我的建议只是为了避免脚本挂起而采取的变通办法。如果您想了解为什么卷曲挂起,您必须跟踪网络流量。您可以使用Wireshark。

如果($buffer==false){echo curl_error($ch)}
现在仍然停止工作,请尝试
。但是根据你的建议,很快就会有结果。想知道是什么问题吗?(如果你能简而言之)事实上我不能理解这个错误。我是说为什么停止执行。但问题是它突然停止工作了。