PHP Quick Function For Asynchronous Multi Curl

PHP Quick Function For Asynchronous Multi Curl

If you are here, it probably means that you have a problem – you have a bunch of URLs that you need to cURL, but doing it linearly (one by one) is too slow. This is where curl_multi_init comes in. It is the asynchronous version of curl and it will help you to save lots of time (by that i mean like 80-90%)

So anyway, below is a quick function for you to use. Feel free to modify it according to your needs.

function get_content_of_urls($urls)
{
    // array of curl handles
    $multi_curl = array();
    // data to be returned
    $results = array();
    // init multi handle
    $mh = curl_multi_init();
    $i = 0;
    foreach ($urls as $url) 
    {	
        $multi_curl[$i] = curl_init();
        curl_setopt($multi_curl[$i], CURLOPT_URL,$url);	
        curl_setopt($multi_curl[$i], CURLOPT_HEADER, 0); 
        curl_setopt($multi_curl[$i], CURLOPT_RETURNTRANSFER,1);
        curl_multi_add_handle($mh, $multi_curl[$i]);
        $i++;
    }
    $index=null;
    do {
        curl_multi_exec($mh,$index);
    } while($index > 0);
    // get content and remove handles
    foreach($multi_curl as $k => $ch) {
        $results[$k] = curl_multi_getcontent($ch);
        curl_multi_remove_handle($mh, $ch);
    }
    return $results;
}

If you require any assistance, feel free to let me know in the comments below.

Enjoyed the content ? Share it with your friends !

Leave a Reply

Your email address will not be published. Required fields are marked *