PHP Multi-cURL to Run Parallel cURL Requests (Example)

Posted on May 25, 2019 at 11:00 pm

Simple PHP function to run multiple cURL requests in parallel:

function multicurl_urls($nodes)
{
    $node_count = count($nodes);
 
    $curl_arr = array();
    $master = curl_multi_init();
 
    for($i = 0; $i < $node_count; $i++)
    {
        $url =$nodes[$i];
        $curl_arr[$i] = curl_init($url);
        curl_setopt($curl_arr[$i], CURLOPT_RETURNTRANSFER, true);
        curl_multi_add_handle($master, $curl_arr[$i]);
    }
 
    do 
    {
        curl_multi_exec($master,$running);
    } 
    while($running > 0);
 
    $results = array();
 
    for($i = 0; $i < $node_count; $i++)
    {
        $results[] = curl_multi_getcontent  ( $curl_arr[$i]  );
    }
 
    return $results;
}

You just need to pass an array of URLs, ie:

$urls = array(
"http://www.website1.com/path",
"http://www.website2.com/path",
"http://www.website3.com/path",
"http://www.website4.com/path",
);
 
print_r(multicurl_urls($urls));

The non-function code is this:

$nodes = array(url1, url2, url3);
 
$node_count = count($nodes);
 
$curl_arr = array();
$master = curl_multi_init();
 
for($i = 0; $i < $node_count; $i++)
{
    $url =$nodes[$i];
    $curl_arr[$i] = curl_init($url);
    curl_setopt($curl_arr[$i], CURLOPT_RETURNTRANSFER, true);
    curl_multi_add_handle($master, $curl_arr[$i]);
}
 
do 
{
    curl_multi_exec($master,$running);
} 
while($running > 0);
 
 
for($i = 0; $i < $node_count; $i++)
{
    $results[] = curl_multi_getcontent  ( $curl_arr[$i]  );
}
 
print_r($results);

Source:

https://stackoverflow.com/a/9311112

Receive updates via email

Other Posts

Updated Posts