PHP:http GET / POST以真正的异步方式

Basically, I'm looking for a way to do the following in PHP:

http_get_or_post('an.url', 'or.two');

// Do some work here, not worrying about the http going on in the background.

$r = wait_for_and_get_the_results_of_the_http_requests()

And maybe someone with more curl experience can confirm that curl_multi is what I'm looking for.

From what I gather from http://php.net/manual/en/function.curl-multi-init.php, the sample there might give me what I need:

$ch1 = curl_init();
curl_setopt($ch1, CURLOPT_URL, "http://www.php.net/");
curl_setopt($ch1, CURLOPT_HEADER, 0);
$mh = curl_multi_init();
curl_multi_add_handle($mh,$ch1);

$active = null;
do {
    $mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);

// Now, am I free to do some time consuming work here and not worry about
// calling curl_multi_exec every now and then to facilitate the background
// http / socket processes?

while ($active && $mrc == CURLM_OK) {
    if (curl_multi_select($mh) != -1) {
        do {
            $mrc = curl_multi_exec($mh, $active);
        } while ($mrc == CURLM_CALL_MULTI_PERFORM);
    }
}

curl_multi_remove_handle($mh, $ch1);
curl_multi_close($mh);

Now, the main question is, is my understanding of what this would do correct?

1) The first loop will only take care of sending out / writing the requests to the sockets.

2) All of the http / socket stuff will happen in the background after the requests have been sent, leaving me free to do other stuff without having to periodically call curl_multi_exec to make sure that some buffer is not full somewhere and needs a kick to keep going.

3) The second loop will wait for any outstanding response data to arrive and finish reading and processing the responses.

Now, this would still not be fully async - I might become blocked on writing the requests should the socket write buffers fill up, but in my scenario that's not a problem, I'm only worried about having to call curl_multi_exec while I'm doing the other stuff in the middle, just so the whole thing would not freeze until the next chance I get to call curl_multi_exec.

I'm also fine with this being the case for the general scenario of 2k-4k responses, with bigger responses getting stuck doing nothing in the background until I get to the second loop.

Is this how curl_multi works? If not, what can you suggest that will get this done in PHP?

2个回答



curl_multi一次只能做很小的工作。 对curl_multi_exec的每次调用都会对传输操作进行一些分段,一旦你调用了足够多次数据并且已经发送和接收了数据,就完成了。</ p>
</ div>

展开原文

原文

curl_multi does its work a very small piece at a time. Every call to curl_multi_exec does a little fragment of the transfer operation and once you've called it enough number of times and the data has been sent and received, it is done.



刚做了一些测试,简而言之 - 不,这不是curl_multi的工作原理 - 你需要继续调用curl_multi_exec, 没有它,第一个循环只会设法在连接上进行TCP握手,甚至不发送请求。</ p>

很遗憾,使用mysqlnd的mysqli可以做异步请求,为什么可以 不卷曲?</ p>
</ div>

展开原文

原文

Just did a few tests, and in short - no, this is not how it works with curl_multi - you need to keep calling curl_multi_exec, without that, the first loop will only manage to do a TCP handshake on the connection, not even send out the request.

It's a shame, mysqli using mysqlnd can do async requests, why can't curl?

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐