PHP cURL多句柄改变选择

时间:2011-08-31 16:50:09

标签: php curl-multi

更改一些选项并删除其他资源不能使用相同的多句柄:


    $ch1 = curl_init();
    $ch2 = curl_init();
    $ch3 = curl_init();

    curl_setopt($ch1, CURLOPT_URL, "http://mytestsite.com/test.curl.php?test=a");
    curl_setopt($ch1, CURLOPT_HEADER, 0);
    curl_setopt($ch1, CURLOPT_RETURNTRANSFER, TRUE);

    curl_setopt($ch2, CURLOPT_URL, "http://mytestsite.com/test.curl.php?test=b");
    curl_setopt($ch2, CURLOPT_HEADER, 0);
    curl_setopt($ch2, CURLOPT_RETURNTRANSFER, TRUE);

    curl_setopt($ch3, CURLOPT_URL, "http://mytestsite.com/test.curl.php?test=b");
    curl_setopt($ch3, CURLOPT_HEADER, 0);
    curl_setopt($ch3, CURLOPT_RETURNTRANSFER, TRUE);

    $mh = curl_multi_init();

    curl_multi_add_handle($mh,$ch1);
    curl_multi_add_handle($mh,$ch2);
    curl_multi_add_handle($mh,$ch3);

    $active = null;

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

    $test1 = curl_multi_getcontent( $ch1 );
    $test2 = curl_multi_getcontent( $ch2 );
    $test3 = curl_multi_getcontent( $ch3 );
    error_log($test1);  // a
    error_log($test2);  // b
    error_log($test3);  // b

    curl_multi_remove_handle($mh, $ch1);
    // curl_multi_remove_handle($mh, $ch2);
    // curl_multi_remove_handle($mh, $ch3);
    curl_setopt($ch2, CURLOPT_URL, "http://mytestsite.com/test.curl.php?test=c");
    curl_setopt($ch3, CURLOPT_URL, "http://mytestsite.com/test.curl.php?test=c");

    $active = null;

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

    $test1 = curl_multi_getcontent( $ch1 );
    $test2 = curl_multi_getcontent( $ch2 );
    $test3 = curl_multi_getcontent( $ch3 );
    error_log($test1);  // a
    error_log($test2);  // b
    error_log($test3);  // b 

    curl_multi_close($mh);

我明白了:

a
b
b

a
b
b

我想看:

a
b
b

a
c
c

in accroding with this f.a.q.

  

转移后,您只需在句柄中设置新选项并进行另一次转移。如果可以,这将使libcurl重用相同的连接。

可以帮助我吗?

感谢

1 个答案:

答案 0 :(得分:1)

您需要从多把手中正确移除通道。中间的正确代码应该看起来像

// [...]
echo curl_multi_getcontent( $ch1 );
echo curl_multi_getcontent( $ch2 );
echo curl_multi_getcontent( $ch3 );

// Removing the channels 
curl_multi_remove_handle($mh, $ch1);
curl_multi_remove_handle($mh, $ch2);
curl_multi_remove_handle($mh, $ch3);

// and of course we need to re-add them
curl_setopt($ch2, CURLOPT_URL, "http://example.com/test.curl.php?test=c");
curl_setopt($ch3, CURLOPT_URL, "http://example.com/test.curl.php?test=c");
curl_multi_add_handle($mh,$ch2);
curl_multi_add_handle($mh,$ch3);

$active = null;
// [...]

现在您获得了abbacc的理想结果。您可能已经注意到,我们没有重新添加$ch1,但仍会得到结果。这是由于问题,$ch1不再在多个exec中,但仍然是状态为“已完成”的有效cURL资源,因此仍然具有与之前相同的结果 - {{1 }}。您实际上可以在服务器日志中检查这一点,其中根本不执行对a的第二次调用:

$ch1