Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CURLM_CALL_MULTI_PERFORM deprecated

CURLM_CALL_MULTI_PERFORM was deprecated.

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

Is there any alternative? Curl version 7.27.0

like image 347
Antonino Bonumore Avatar asked Oct 21 '13 09:10

Antonino Bonumore


2 Answers

You should leave your code as it is, as that's still the best way to call curl_multi_exec.

The constant itself still exists; it's simply not used in Curl 7.20.0 and later. But, the change was done in such a way that your previous code does not need to be modified at all, and will continue to work.

Prior to Curl 7.20.0, curl_multi_exec only processed a single task at a time, and after calling it there may still be other tasks ready to do immediately without blocking. To ensure all these tasks got done, you had to do:

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

All this was done without blocking, because CURLM_CALL_MULTI_PERFORM was only returned when other tasks were ready to be done and curl wanted you to call it again straight away.

From 7.20.0, Curl realized that this was silly, and that if curl_multi_exec (which is actually curl_multi_perform in upstream curl) needed to do more than one thing at once, it should just do them all itself, before returning.

So, there is no need for it to return CURLM_CALL_MULTI_PERFORM anymore.

Today, the equivalent to the above code is simply,

curl_multi_exec($mc, $active);

You do not need to check if the result is CURLM_CALL_MULTI_PERFORM and call the function again.

However, in order to support both older and newer versions of Curl, you need to still check the return code:

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

Which is precisely what you were doing before.

CURLM_CALL_MULTI_PERFORM still exists as a symbol, for this purpose. So, just leave your code as it is, and all will be fine with old and new Curl.

Why using $active isn't the same

The second parameter by reference, $active in this example, CANNOT be treated in the same way. $active will always return true until all HTTP requests in progress have terminated, regardless of whether there is anything ready for processing immediately or not. It therefore can't be used as an indicator that you should be calling curl_multi_exec again. All it indicates is that you are expecting further action on the connection in the future.

The usual reason for using the curl_multi interface is so that you can perform HTTP requests in a non-blocking manner so that you can do other tasks while you are waiting for data to be sent or received - or at least, so you can manage multiple requests in parallel and deal with each request as soon as it returns. To achieve this, you call curl_multi_exec as part of a loop. If there is some data that needs to be sent or received, curl_multi_exec will do that. If nothing is ready for processing and you are simply waiting, then it will do nothing. In the rest of the loop, you can achieve other tasks and/or react to curl_multi_info_read indicating one of the requests has completed.

But, if you were to do the following

// Do NOT do this
do {
    $mrc = curl_multi_exec($mc, $active);
} while ($active);

... then, this loop will never quit until all pending HTTP requests terminate, thereby acting in a blocking manner and never letting you attend to other tasks while waiting.

like image 126
thomasrutter Avatar answered Sep 28 '22 04:09

thomasrutter


try like:

do {
    $mrc = curl_multi_exec($mc, $active);
} while ($active > 0);
like image 38
Sudhir Bastakoti Avatar answered Sep 28 '22 06:09

Sudhir Bastakoti