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

时间:2011-08-17 12:03:52

标签: php http curl libcurl

您好我已经对ip进行了卷曲测试,我可以获得响应信息(时间,响应代码,......) 这次我想让它在几个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);

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

1 个答案:

答案 0 :(得分:0)

您可能需要PHP cURL Class With Multi-Threading
http://pastebin.com/vBgYDzVuhttp://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]