문제

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?

도움이 되었습니까?

해결책

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.

다른 팁

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?

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top