【问题标题】:Cancel operation after 5 seconds of not receiving any response [duplicate]5秒未收到任何响应后取消操作[重复]
【发布时间】:2015-07-02 14:37:59
【问题描述】:

我正在使用脚本从我通过表单上传的文件中检测哪些域仍在工作。

文件结构为domain:description每行(约20行)

我正在使用以下代码遍历文件的每一行并测试每个域

$file = file($_FILES['products_file']['tmp_name']);
$count = count($file);
if($count > 0)
{
    $good = '';
    $bad = '';
    foreach($file as $line)
    {
        $line = trim(preg_replace('/\s+/', ' ', $line));
        $data = explode(":", $line);

        $test = testURL("https://".$data[1]."/");
        if ($test !== 404 && !empty($test)) {
            $good[] = $line;
        } else {
            $bad[] = $line;
        }
    }
}

我的问题是我在使用 CURL 函数 curl_exec($ch) 时收到此错误 Maximum execution time of 30 seconds exceeded

这如果我的testURL 函数有问题

function testURL($url) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $data = curl_exec($ch);
    curl_close($ch);

    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    if($httpCode == 404) {
        return 404;
    }

    if (empty($data)) {
        return "";
    }

    return $data;
}

为了解决这个问题,我想知道如果 URL 在 5 秒内没有响应并返回一个空字符串,是否有办法让 CURL 函数取消它的操作。

【问题讨论】:

  • 我尝试将 CURLOPT_TIMEOUT 设置为 5 秒,但我仍然收到错误 Maximum execution time of 30 seconds exceeded
  • 您的错误消息是 PHP 错误,而不是 CURL 错误。增加PHP的执行时间:stackoverflow.com/a/5164954/1682509
  • @Reeno 但是如果我增加 PHP 的执行时间,我将不得不等待大约 10 分钟才能从我的文件中提取 20 行。我想快速通过它们。
  • 然后要么将 CURL 的超时设置为非常低的值,要么多次调用 PHP,每次只处理一些 URI。你不能加速 CURL,只能减少超时,但是你不会得到所有请求的返回状态

标签: php curl


【解决方案1】:

看看CURLOPT_TIMEOUT:https://php.net/curl-setopt

【讨论】:

    猜你喜欢
    • 2016-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多