CURLM_CALL_MULTI_PERFORM deprecated

前端 未结 3 1767
情歌与酒
情歌与酒 2021-01-03 01:21

CURLM_CALL_MULTI_PERFORM was deprecated.

    do {
        $mrc = curl_multi_exec($mc, $active);
    } while ($mrc == CURLM_CALL_MULTI_PERFORM);
相关标签:
3条回答
  • 2021-01-03 01:49

    It's not really clear, if CURLM_CALL_MULTI_PERFORM is deprecated or not.

    The symbol exists. A removal was not mentioned in the 7.27.0 Change Notes.

    Like @tne pointed out in his comment: it seems reasonable to ignore it.

    0 讨论(0)
  • 2021-01-03 02:04

    try like:

    do {
        $mrc = curl_multi_exec($mc, $active);
    } while ($active > 0);
    
    0 讨论(0)
  • 2021-01-03 02:04

    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, it's done in such a way that your 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, but there still may be more tasks it wants to do immediately. To ensure all these got done, you had to do:

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

    CURLM_CALL_MULTI_PERFORM was only returned when curl wanted you to call it again immediately because it still had other tasks it wanted to do. It wasn't returned when the request still active but Curl was still waiting on data.

    From 7.20.0, Curl realized that this was silly, and that if curl_multi_exec (which is actually curl_multi_perform in curl itself) 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, returns true if there are any requests in progress on the curl multi handle at all. This is not the same as CURLM_CALL_MULTI_PERFORM which indicates the function needs to be called again immediately. In fact, if curl_multi_exec returns and doesn't return CURLM_CALL_MULTI_PERFORM, you can be sure that there is no work that needs to be done immediately. You can go on with the other tasks you wanted to do while you waited - the whole reason you were motivated to use the curl_multi interface in the first place. $active should be ignored except as an indicator that you still have ongoing requests, which in many cases you'll know anyway if you keep track of that yourself.

    0 讨论(0)
提交回复
热议问题