Antti Rytsölä
Antti Rytsölä

Reputation: 1553

An example of PHP curl_multi_exec() without curl_multi_select()?

Does anyone have an example of using curl_multi_exec() without curl_multi_select() ? One of our servers still uses Centos5 with PHP5.1.x.

Also the example should allow processing the handles before everyone has finished.

My implementation has problems with sites without Content-Length parameter and request body over buffer size (16k). Without the Content-Length from server the curl_getinfo()['download_content_length'] is always -1.

Upvotes: 1

Views: 797

Answers (1)

Antti Rytsölä
Antti Rytsölä

Reputation: 1553

something like this.

$curlMulti = curl_multi_init();
$handles = array();

for ($i = 0; $i < 10; $i++) {
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, "https://example.com/request/$i");
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_multi_add_handle($curlMulti, $ch);
  $handles[] = $ch;
}

do {
  $curlMultiStatus = curl_multi_exec($curlMulti, $runningHandles);

  // Process completed handles
  while ($completedHandle = curl_multi_info_read($curlMulti)) {
     $handle = $completedHandle['handle'];
     $response = curl_multi_getcontent($handle);
     $httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
     echo "Handle completed: $httpCode\n";
     curl_multi_remove_handle($curlMulti, $handle);
     curl_close($handle);
  }
    
} while ($curlMultiStatus == CURLM_CALL_MULTI_PERFORM || $runningHandles);

curl_multi_close($curlMulti);

Upvotes: 0

Related Questions