【问题标题】:curl_multi_exec shows different runscurl_multi_exec 显示不同的运行
【发布时间】:2018-10-12 02:15:17
【问题描述】:

我将 curl_multi_exec() 用于仅 5 个 url。 现在我有这个奇怪的问题。当我在 xampp 上运行我的代码时,它运行良好。我可以看到用 5 初始化的 $running 值,然后不断减小。 . 但是,当我在其他 localhost(在 arm 架构上)上尝试它时,$running 被初始化为 0。 所以我的 curl_multi_exec() 永远不会返回任何响应。

这里是sn-p的代码:

do {
curl_multi_exec($master,$running);
echo "<pre>";
var_dump($running );
echo "</pre>";
} while($running > 0);

这是我的完整代码:

    $nodes = array( 'https://www.example.com',
            'https://www.example2.com',
            'https://www.example3.com',
            'https://www.example4.com',
            'https://www.example5.com'
            );
    $node_count = count($nodes);

    $curl_arr = array();
    $master = curl_multi_init();
    for($i = 0; $i < $node_count; $i++)
    {
    $url =$nodes[$i];
    $curl_arr[$i] = curl_init($url);
    curl_setopt($curl_arr[$i], CURLOPT_RETURNTRANSFER, true);
    curl_multi_add_handle($master, $curl_arr[$i]);  
    }

    do {
   curl_multi_exec($master,$running);
   echo "<pre>";
   var_dump($running );
   echo "</pre>";
   } while($running > 0);


   for($i = 0; $i < $node_count; $i++)
   {
    $results = curl_multi_getcontent($curl_arr[$i]);
    var_dump($results);
    }

我用谷歌搜索了一些东西,并知道 curl ssl 可能是一个问题。因此,我安装了另一个启用了 openssl 和 curl ssl 的 localhost(在 ARM 上)。 现在我有两个启用 SSL 的不同 localhost(均用于 ARM),这个 sn-p 在一个 localhost 上工作正常,在另一个 localhost 上不起作用。

不知何故,我需要那个“另一个”,因为它有更多的功能。

请高人指导一下这个 $running 初始化可能有什么问题?

任何帮助表示赞赏:)

也试过了,没成功

                                    <?php

            // echo "<meta http-equiv='refresh' content='3'/>" ;

                include_once ("simple_html_dom.php");
                libxml_use_internal_errors(true);

            function get_string_between($string, $start, $end){
                $string = ' ' . $string;
                $ini = strpos($string, $start);
                if ($ini == 0) return '';
                $ini += strlen($start);
                $len = strpos($string, $end, $ini) - $ini;
                return substr($string, $ini, $len);
            }

            function multi_thread_curl($url_array, $number_threads) {
             $curl_array = array_chunk($url_array, $number_threads, $preserve_keys = true);
            //Iterate through each batch of urls.
            foreach($curl_array as $threads) {
                //Create your cURL resources.
                foreach($threads as $key=>$value) {
                ${'ch' . $key} = curl_init();
                curl_setopt(${'ch' . $key}, CURLOPT_URL, $value);
                curl_setopt(${'ch' . $key}, CURLOPT_SSL_VERIFYPEER, false);
                curl_setopt(${'ch' . $key}, CURLOPT_RETURNTRANSFER, true);
                curl_setopt(${'ch' . $key}, CURLOPT_TIMEOUT, 10);

                }
                //Create the multiple cURL handler.
                $mh = curl_multi_init();

                //Add the handles.
                foreach($threads as $key=>$value) {

                curl_multi_add_handle($mh, ${'ch' . $key});

                }

                $active = null;

                //execute the handles.
                do {

                $mrc = curl_multi_exec($mh, $active);

                } while ($mrc == CURLM_CALL_MULTI_PERFORM);

                while ($active && $mrc == CURLM_OK) {
                    echo $active;

                    if (curl_multi_select($mh) != -1) {
                        do {

                            $mrc = curl_multi_exec($mh, $active);

                        } while ($mrc == CURLM_CALL_MULTI_PERFORM);
                    }
                }
                //Get your data and close the handles.
                foreach($threads as $key=>$value) {
                $results[$key] = curl_multi_getcontent(${'ch' . $key});
                curl_multi_remove_handle($mh, ${'ch' . $key});
                }
                //Close the multi handle exec.
                curl_multi_close($mh);
            }
            return $results;
            }
            $nodes = array( 'https://www.example1.com',
                            'https://www.example2.com',
                            'https://www.example3.com',
                            'https://www.example4.com',
                            'https://www.example5.com',
                            );
            $node_count = count($nodes);
            echo "results: ";
            $number_threads = 5;
            $results = multi_thread_curl($nodes, $number_threads);
            print_r($results);
            echo 'done';


            ?>

问题在这里:$active is always 5。永远循环:(

【问题讨论】:

  • 请发布您的 curl 代码。将 multi exec curl 函数放在 while 循环中可能不是一个好习惯
  • @Joseph_J curl_multi_exec 始终在内部工作,同时跟踪当前正在运行的句柄。您可以访问:curl_multi_exec php.net official 看看 :)
  • 我很抱歉,你是对的。出于某种原因,我认为这是您编写的包含 curl_multi_exec 的函数。
  • @Joseph_J 没关系 :) 。任何帮助或评论表示赞赏。 :)

标签: php curl php-curl curl-multi


【解决方案1】:

这是一个多线程 curl 函数,我使用 PHP.net 中的示例组合在一起。我已经使用这个函数来获取大量的 URL。它能够真正加快速度。我已经取得了巨大的成功。

您甚至可以扩展它并为您的 curl 选项添加一个参数。

function multi_thread_curl($url_array, $number_threads) {


$curl_array = array_chunk($url_array, $number_threads, $preserve_keys = true);

    //Iterate through each batch of urls.
    foreach($curl_array as $threads) {

        //Create your cURL resources.
        foreach($threads as $key=>$value) {

        ${'ch' . $key} = curl_init();

        curl_setopt(${'ch' . $key}, CURLOPT_URL, $value);
        curl_setopt(${'ch' . $key}, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt(${'ch' . $key}, CURLOPT_RETURNTRANSFER, true);
        curl_setopt(${'ch' . $key}, CURLOPT_TIMEOUT, 10);

        }


        //Create the multiple cURL handler.
        $mh = curl_multi_init();

        //Add the handles.
        foreach($threads as $key=>$value) {

        curl_multi_add_handle($mh, ${'ch' . $key});

        }

        $active = null;

        //execute the handles.
        do {

        $mrc = curl_multi_exec($mh, $active);

        } while ($mrc == CURLM_CALL_MULTI_PERFORM);

        while ($active && $mrc == CURLM_OK) {

            if (curl_multi_select($mh) != -1) {
                do {

                    $mrc = curl_multi_exec($mh, $active);

                } while ($mrc == CURLM_CALL_MULTI_PERFORM);
            }

        }

        //Get your data and close the handles.
        foreach($threads as $key=>$value) {

        $results[$key] = curl_multi_getcontent(${'ch' . $key});

        curl_multi_remove_handle($mh, ${'ch' . $key});

        }

        //Close the multi handle exec.
        curl_multi_close($mh);

    //Limits to one group of threads.
    //break;

    }


    return $results;



}

$urls = array(

  'https://en.wikipedia.org/wiki/Wiki'

);


$results = multi_thread_curl($urls, 1);

print_r($results);

【讨论】:

    猜你喜欢
    • 2011-07-02
    • 1970-01-01
    • 2016-12-28
    • 1970-01-01
    • 2016-07-10
    • 2021-08-12
    • 2020-12-14
    • 2020-12-16
    相关资源
    最近更新 更多