Php cURL不处理所有请求-在110左右停止

Php cURL不处理所有请求-在110左右停止,php,curl,Php,Curl,所以我很难让这个代码正常工作。我从一个博客上下载了它,它基于wordpress链接检查器。我在一个数据库中有大约6000个URL,我需要检查http状态,所以这似乎是一个不错的选择。我已经稍微修改了代码以满足我的需要,它可以正常工作(有点) 我在整个代码中检查了url\u列表数组,它包含所有url。问题是,在大约第110行之后,它将基本上停止执行,这是一种随机的,但通常在该数字附近。不确定是否需要在某个地方设置超时,或者代码中是否存在错误。我注意到,如果我将$max\u connections设

所以我很难让这个代码正常工作。我从一个博客上下载了它,它基于wordpress链接检查器。我在一个数据库中有大约6000个URL,我需要检查http状态,所以这似乎是一个不错的选择。我已经稍微修改了代码以满足我的需要,它可以正常工作(有点)

我在整个代码中检查了
url\u列表
数组,它包含所有url。问题是,在大约第110行之后,它将基本上停止执行,这是一种随机的,但通常在该数字附近。不确定是否需要在某个地方设置超时,或者代码中是否存在错误。我注意到,如果我将
$max\u connections
设置为大于8,它将返回500错误。有什么建议吗

<?php
// CONFIG
$db_host = 'localhost';
$db_user = 'test';
$db_pass = 'yearight';
$db_name = 'URLS';
$excluded_domains = array();
$max_connections = 7;

$dbh = new PDO('mysql:host=localhost;dbname=URLS', $db_user, $db_pass);

$sth = $dbh->prepare("SELECT url FROM list");
$sth->execute();

$result = $sth->fetchAll(PDO::FETCH_COLUMN, 0);

// initialize some variables
$url_list = array();
$working_urls = array();
$dead_urls = array();
$not_found_urls = array();
$active = null;

foreach($result as $d) {
    // get all links via regex
    if (preg_match_all('@((http?://)?([-\w]+\.[-\w\.]+)+\w(:\d+)?(/([-\w/_\.]*(\?\S+)?)?)*)@', $d, $matches)) {

        foreach ($matches[1] as $url) {

            // store the url
            $url_list []= $url;
        }
    }
}

// 1. multi handle
$mh = curl_multi_init();

// 2. add multiple URLs to the multi handle
for ($i = 0; $i < $max_connections; $i++) {
    add_url_to_multi_handle($mh, $url_list);
}

// 3. initial execution
do {
    $mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);

// 4. main loop
while ($active && $mrc == CURLM_OK) {

    // 5. there is activity
    if (curl_multi_select($mh) != -1) {

        // 6. do work
        do {
            $mrc = curl_multi_exec($mh, $active);
        } while ($mrc == CURLM_CALL_MULTI_PERFORM);

        // 7. is there info?
        if ($mhinfo = curl_multi_info_read($mh)) {
            // this means one of the requests were finished

            // 8. get the info on the curl handle
            $chinfo = curl_getinfo($mhinfo['handle']);

            // 9. dead link?
            if (!$chinfo['http_code']) {
                $dead_urls []= $chinfo['url'];

            // 10. 404?
            } else if ($chinfo['http_code'] == 404) {
                $not_found_urls []= $chinfo['url'];

            // 11. working
            } else {
                $working_urls []= $chinfo['url'];
            }

            // 12. remove the handle
            curl_multi_remove_handle($mh, $mhinfo['handle']);
            curl_close($mhinfo['handle']);

            // 13. add a new url and do work
            if (add_url_to_multi_handle($mh, $url_list)) {

                do {
                    $mrc = curl_multi_exec($mh, $active);
                } while ($mrc == CURLM_CALL_MULTI_PERFORM);
            }
        }
    }
}

// 14. finished
curl_multi_close($mh);

echo "==Dead URLs==<br/>";
echo implode("<br/>",$dead_urls) . "<br/><br/>";

echo "==404 URLs==<br>";
echo implode("<br/>",$not_found_urls) . "<br/><br/>";

echo "==Working URLs==<br/>";
echo implode("<br/>",$working_urls);

echo "<pre>";
var_dump($url_list); 
echo "</pre>";
// 15. adds a url to the multi handle
function add_url_to_multi_handle($mh, $url_list) {
    static $index = 0;

    // if we have another url to get
    if ($url_list[$index]) {

        // new curl handle
        $ch = curl_init();

        // set the url
        curl_setopt($ch, CURLOPT_URL, $url_list[$index]);
        // to prevent the response from being outputted
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        // follow redirections
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        // do not need the body. this saves bandwidth and time
        curl_setopt($ch, CURLOPT_NOBODY, 1);

        // add it to the multi handle
        curl_multi_add_handle($mh, $ch);


        // increment so next url is used next time
        $index++;

        return true;
    } else {

        // we are done adding new URLs
        return false;
    }
}
?>

更新:


我已经用bash编写了一个脚本,它做了与此相同的事情。我注意到,当我查看信息输出到的文本文件时,当信息输出失败时,通常是在返回奇怪的http状态代码的链接周围,例如
000
522
,其中一些可能会执行长达5分钟!因此,我想知道当遇到这些状态代码时,cURL的PHP版本是否正在停止执行。这只是一个想法,可能会增加更多的价值来帮助解决这个问题。

1-执行时间问题

2-DECLAE位于代码最大执行时间的顶部,这肯定会有所帮助


bool set_time_limit(int$seconds)

脚本处理大约110行需要多少时间?@bad_boy大约10秒左右给或拿洗牌列表,使顶部的链接现在位于底部,然后再次检查问题是否通常在第110行左右出现。为了调试目的,请尝试
echo ini\u get('max_execution_time');
它说什么?@bad_boy最大执行时间是
120