curl_multi_exec returning empty arrays

1.1k Views Asked by At

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.

1

There are 1 best solutions below

1
On

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