简体   繁体   中英

curl_multi_exec returning empty arrays

I have been using the following function to make asynchronous curl posts:

function curl_post_multi($urls){
    $curl_arr = array();
    $num_urls = count($urls);
    $mh = curl_multi_init();
    for($i= 0; $i < $num_urls; $i++){
        $curl_arr[$i] = curl_init();
        curl_setopt($curl_arr[$i], CURLOPT_URL, $urls[$i]);
        curl_setopt($curl_arr[$i], CURLOPT_HEADER, 0);
        curl_setopt($curl_arr[$i], CURLOPT_POST, true);
        curl_setopt($curl_arr[$i], CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl_arr[$i], CURLOPT_RETURNTRANSFER, true);
        curl_multi_add_handle($mh, $curl_arr[$i]);
    }

    $running = null;
    do{
        curl_multi_exec($mh, $running);
    }while($running > 0);
    $results = array();
    for($i= 0; $i < $num_urls; $i++){
        $results[] = curl_multi_getcontent($curl_arr[$i]);
        curl_multi_remove_handle($mh, $curl_arr[$i]);
    }
    curl_multi_close($mh);
    return $results;
}

I've just now noticed that it is often only returning results from the first URL, or no results at all. I've played around with about 100 configurations of this function. The results are empty arrays, and curl_error($curl_arr[$i]) is empty, so I'm not sure where to start looking for what's wrong. I know the URLS are correct because if I do back to back regular curls it works fine.

Your do-while loop never runs. $running is never > 1, as it's set to null on the previous line.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM