gpt4 book ai didi

php - 胆怯的 PHP 脚本在遇到错误时就退出

转载 作者:行者123 更新时间:2023-12-02 18:05:49 25 4
gpt4 key购买 nike

我有一个 cURL 函数,可以抓取数组中指定的所有网页。该数组称为 $to_be_spidered,我的函数像这样执行:

$to_be_spidered = array('http://google.com', 'http://mysterysite.com', 'http://yahoo.com');

for ($i = 0; $i != count($to_be_spidered); $i++) {

$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_TIMEOUT, 0); // set cURL timeout
$html= curl_exec($ch);

// error handling
if (!$html) {
echo "<br />cURL error number:" .curl_errno($ch);
echo "<br />cURL error:" . curl_error($ch);
exit;
}

// etc. etc...

}

现在的问题是,如果网页返回 404 之类的错误,脚本就会被终止。例如,如果未找到 mymysite.com,则脚本不会尝试抓取 yahoo.com。它只是退出该链接以及之后的所有链接。

我希望它停止尝试抓取错误链接并转到队列中的下一个链接。我尝试将“退出”更改为“继续”,但没有成功。它仍然停止。我做错了什么还是这特定于使用 cURL?

最佳答案

您应该按照指示将exit更改为continue

您收到任何错误吗?是否启用错误报告? fatal error 将停止执行。

将其放在脚本的顶部

ini_set('display_errors', 'On');
error_reporting(E_ALL);

另外,您在哪里使用 $to_be_spidered 中的 URL?另一件事(也相关),使用 foreach

你的循环看起来会更好
foreach ($to_be_spidered as $target_url) {

关于php - 胆怯的 PHP 脚本在遇到错误时就退出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4870138/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com