1

I'm looking at the sample code at php.net which explains how to open connections to multiple URLs simultaneously with cURL. But I don't understand what the 2 loops do and why is there 2 of them?

//execute the handles
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);
    }
}

I found a resource online which shows this example, but there's no clear explanation as to what each of these loops does and how are they connected.

They mention that first loop has a non-blocking function and the second loop has blocking function. What does that mean?

Can someone please explain why are there 2 loops and what do they do?

Also what is CURLM_CALL_MULTI_PERFORM, php.net shows it inside the code, but there's no reference as to what it is.

Sorry spent the whole day researching the answer and this is my last resort. Hoping someone can help.

Robert Sinclair
  • 4,550
  • 2
  • 44
  • 46
  • 1
    Generally speaking: "Blocking" (in context of sockets) means that operations on that socket "pause" further execution of code (eg. a read waits until it has read enough bytes or another end-condition is met, specific bytes for example (think newline or EOF)) while functions on "non-blocking" sockets immediately return and later code gets executed even if there wasn't a response on the socket. – ccKep Dec 19 '15 at 02:25
  • 1
    See [this comment](http://php.net/manual/en/function.curl-multi-exec.php#80977) in the documentation: **If it returns CURLM_CALL_MULTI_PERFORM you better call it again soon, as that is a signal that it still has local data to send or remote data to receive.** – Barmar Dec 19 '15 at 02:27
  • ^ thank you, it makes more sense now – Robert Sinclair Jan 13 '16 at 04:23

1 Answers1

1

So I googled the exact line of code in quotes and found the original guy who wrote that snippet that got on php.net to explain what it does.. Even he confirms that it's a bit confusing

http://technosophos.com/2012/10/26/php-and-curlmultiexec.html

another explanation discovered by googling the code in quotes:

why curl_multi_exec in two loops

Explanation for the 2 loops, from the other Stack overflow answer:

As presented, the first loop is intended to initialize the HTTP clients. Normally it only executes once. Then in the second loop the HTTP requests are sent and the responses reaped.

Community
  • 1
  • 1
Robert Sinclair
  • 4,550
  • 2
  • 44
  • 46