curl_multi_exec显示不同的运行

时间:2018-05-02 03:25:25

标签: php curl php-curl curl-multi

我使用curl_multi_exec()只需5个网址。 现在我有这个奇怪的问题。当我在xampp上运行我的代码时,它完美无缺。我可以看到$运行值初始化为5然后继续下降。 。 但是,当我在其他localhost(在arm架构上)上尝试它时,$ running会被初始化为0。 所以我的curl_multi_exec()永远不会返回任何响应。

以下是代码段:

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);
    }

我用Google搜索了一些东西并且知道curl ssl可能是个问题。所以,我安装了另一个localhost(在ARM上),启用了openssl和curl ssl。 现在我有两个不同的localhost(都是ARM),启用了SSL,这个代码段可以在一个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。 Forever Loop :(

1 个答案:

答案 0 :(得分:1)

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

你甚至可以展开它并为你的卷曲选项添加一个参数。

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);