• View our reviews on Hot Scripts
  • JS Tutorial

Recent Comments

Powered by Disqus

Back to articles

Multiple curl requests

Here is function that performs multiple curl requests which is much faster then file_get_contents or single curl request. For more information

It takes array of URLs to request as a parameter and returns array of contents or errors for each url:

//$urls - array of urls to request
function multi_curl($urls){
    // for curl handlers
    $curl_handlers = array();

    //for storing contents
    $content = array();
    //setting curl handlers
    foreach ($urls as $url) 
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $curl_handlers[] = $curl;
    //initiating multi handler
    $multi_curl_handler = curl_multi_init();

    // adding all the single handler to a multi handler
    foreach($curl_handlers as $key => $curl)
    // executing the multi handler
        $multi_curl = curl_multi_exec($multi_curl_handler, $active);
    while ($multi_curl == CURLM_CALL_MULTI_PERFORM  || $active);
    foreach($curl_handlers as $curl)
        //checking for errors
        if(curl_errno($curl) == CURLE_OK)
            //if no error then getting content
            $content[] = curl_multi_getcontent($curl);
            //parsing content
            //storing error
            $content[] = curl_error($curl);
    return $content;

$arr = multi_curl(array("", 
echo "<pre>";
echo "</pre>";

You may also be interested in:

Powered by

blog comments powered by Disqus