Home » Php » PHP Parallel curl requests

PHP Parallel curl requests

Posted by: admin November 29, 2017 Leave a comment


I am doing a simple app that reads json data from 15 different URLs. I have a special need that I need to do this serverly. I am using file_get_contents($url).

Since I am using file_get_contents($url). I wrote a simple script, is it:

$websites = array(

foreach ($websites as $website) {
    $data[] = file_get_contents($website);

and it was proven to be very slow, because it waits for the first request and then do the next one.


If you mean multi-curl then, something like this might help:

$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 {
} while($running > 0);

for($i = 0; $i < $node_count; $i++)
    $results[] = curl_multi_getcontent  ( $curl_arr[$i]  );

Hope it helps in some way