PHP cURL 多句柄更改选项

发布于 2024-12-02 11:20:30 字数 2707 浏览 0 评论 0原文

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

    $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

根据此常见问题解答

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

可以帮助我吗?

谢谢

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

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

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

发布评论

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

评论(1

甜是你 2024-12-09 11:20:30

您需要正确地从多手柄上卸下通道。中间的正确代码应该看起来像

// [...]
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 不再处于多重执行中,但仍然是状态为“完成”的有效 cURL 资源,因此仍然具有与之前相同的结果- 一个。您实际上可以在服务器日志中检查这一点,其中根本没有执行对 $ch1 的第二次调用:

[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 "-" "-"

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 "-" "-"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文