curl_multi_exec如何在多个IP地址上执行它并获取响应标头

发布于 2024-11-30 01:27:33 字数 1084 浏览 0 评论 0原文

你好,我已经在一个IP上做了一个curl测试,我可以获得响应信息(时间,响应代码,...) 这次我想让它同时在多个IP上运行。我发现这可以通过curl_multi_exec实现,并且我发现了这段代码:

         // create both cURL resources
        $ch1 = curl_init();
        $ch2 = curl_init();

// set URL and other appropriate options
        curl_setopt($ch1, CURLOPT_URL, "http://www.example.com/");
        curl_setopt($ch1, CURLOPT_HEADER, 0);
        curl_setopt($ch2, CURLOPT_URL, "http://www.php.net/");
        curl_setopt($ch2, CURLOPT_HEADER, 0);

//create the multiple cURL handle
        $mh = curl_multi_init();

//add the two handles
        curl_multi_add_handle($mh, $ch1);
        curl_multi_add_handle($mh, $ch2);

        $running = null;
//execute the handles
        do {
            curl_multi_exec($mh, $running);
        } while ($running > 0);


//close the handles
        curl_multi_remove_handle($mh, $ch1);
        curl_multi_remove_handle($mh, $ch2);
        curl_multi_close($mh);

但我只得到最后输入的url的洞内容(CURLOPT_URL,“http://www.php.net/”) 此外,我想要有关请求和响应的信息(在curl中我使用了 ($infos = curl_getinfo($curl);)

Hello i have made a curl test on an ip and i could get the response infos( times, response code,...)
this time i want to make it work on several ip's in the same time. I found that could be possible with curl_multi_exec, and i found this code:

         // create both cURL resources
        $ch1 = curl_init();
        $ch2 = curl_init();

// set URL and other appropriate options
        curl_setopt($ch1, CURLOPT_URL, "http://www.example.com/");
        curl_setopt($ch1, CURLOPT_HEADER, 0);
        curl_setopt($ch2, CURLOPT_URL, "http://www.php.net/");
        curl_setopt($ch2, CURLOPT_HEADER, 0);

//create the multiple cURL handle
        $mh = curl_multi_init();

//add the two handles
        curl_multi_add_handle($mh, $ch1);
        curl_multi_add_handle($mh, $ch2);

        $running = null;
//execute the handles
        do {
            curl_multi_exec($mh, $running);
        } while ($running > 0);


//close the handles
        curl_multi_remove_handle($mh, $ch1);
        curl_multi_remove_handle($mh, $ch2);
        curl_multi_close($mh);

But i only get the hole content of the last url entered (CURLOPT_URL, "http://www.php.net/")
and besides, i want the informations abouts the request and response( in curl i used ($infos = curl_getinfo($curl);)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只是在用心讲痛 2024-12-07 01:27:33

您可能需要 具有多线程的 PHP cURL 类
在这里获取它 http://pastebin.com/vBgYDzVu 或在这里 http://semlabs.co.uk/assets/files/curl.zip

例如:

    include "curl.php";

    $curl = new CURL();
    $optsA = array( CURLOPT_RETURNTRANSFER => true, CURLOPT_FOLLOWLOCATION => true,
 CURLOPT_HEADER => 0 );
    $optsB = array( CURLOPT_RETURNTRANSFER => true, CURLOPT_HEADER => 0 );
    $curl->addSession( "http://siteA.com/scriptA.php", $optsA );
    $curl->addSession( "http://siteB.com/scriptB.php", $optsB );
    $curl->addSession( "http://siteC.com/scriptC.php", $opts );
    $curl->addSession( "http://siteD.com/scriptD.php", $opts );
    $curlresult = $curl->exec();
    $curl->clear();

    $siteA = $curlresult[0]
    $siteB = $curlresult[1]
    $siteC = $curlresult[2]
    $siteD = $curlresult[3]

You probably need PHP cURL Class With Multi-Threading
Get it here http://pastebin.com/vBgYDzVu or here http://semlabs.co.uk/assets/files/curl.zip

Ex:

    include "curl.php";

    $curl = new CURL();
    $optsA = array( CURLOPT_RETURNTRANSFER => true, CURLOPT_FOLLOWLOCATION => true,
 CURLOPT_HEADER => 0 );
    $optsB = array( CURLOPT_RETURNTRANSFER => true, CURLOPT_HEADER => 0 );
    $curl->addSession( "http://siteA.com/scriptA.php", $optsA );
    $curl->addSession( "http://siteB.com/scriptB.php", $optsB );
    $curl->addSession( "http://siteC.com/scriptC.php", $opts );
    $curl->addSession( "http://siteD.com/scriptD.php", $opts );
    $curlresult = $curl->exec();
    $curl->clear();

    $siteA = $curlresult[0]
    $siteB = $curlresult[1]
    $siteC = $curlresult[2]
    $siteD = $curlresult[3]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文