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.

3
  • 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. Commented Dec 19, 2015 at 2:25
  • 1
    See this comment 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. Commented Dec 19, 2015 at 2:27
  • ^ thank you, it makes more sense now Commented Jan 13, 2016 at 4:23

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.