dongyishen5796 2011-08-31 16:50
浏览 67
已采纳

PHP cURL多句柄改变选择

changing a few options and deleting other resources can not I use the same multi handle:


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

i see:

a
b
b

a
b
b

i want to see:

a
b
b

a
c
c

in accroding with this f.a.q.

After a transfer, you just set new options in the handle and make another transfer. This will make libcurl to re-use the same connection if it can.

can help me?

thanks

  • 写回答

1条回答 默认 最新

  • dongxianghui3709 2011-08-31 21:45
    关注

    you need to properly remove the channels from the multi-handle. The correct code in the middle should look like

    // [...]
    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;
    // [...]
    

    now you get your desired result of abbacc. As you might notice, we did not re-add $ch1, but still get a result back. This is due to the problem, that $ch1 is not in the multi-exec anymore, but is still a valid cURL-resource with the state "finished", and thus still has the same result as before - a. You can actually check this in the server logs, where the second call to $ch1 is not performed at all:

    [31/Aug/2011:23:42:06 +0200] "GET /test.php?test=a HTTP/1.1" 200 1 "-" "-"
    [31/Aug/2011:23:42:06 +0200] "GET /test.php?test=b HTTP/1.1" 200 1 "-" "-"
    [31/Aug/2011:23:42:06 +0200] "GET /test.php?test=b HTTP/1.1" 200 1 "-" "-"
    [31/Aug/2011:23:42:06 +0200] "GET /test.php?test=c HTTP/1.1" 200 1 "-" "-"
    [31/Aug/2011:23:42:06 +0200] "GET /test.php?test=c HTTP/1.1" 200 1 "-" "-"
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料