PHP保持请求直到完成

I have an API written in PHP that sends 10 requests with CURL. The problem is that when I send a HTTP request to the API, I get the response right away, although the server hasn't finished working( getting the response for all of the 10 requests).

I can't use ignore_user_abort() because I need to know exactly the time that the API finished.

How can I notify the connection "hey, wait for the script to finish working"?

Important note: if I use sleep() the connection holds.

Here's my code: gist

This is just a example to show how ob_start works.

 echo "hello";
 ob_start();    // output buffering starts here
 echo "hello1";
 //all curl requests
 if(all curl requests completed)
 {
    ob_end_flush() ;
 }

With no code to refer, I can only show implementation of ob_start. You have to change this code according to your requirement.

$handlers = [];
$mh = curl_multi_init();
ob_start();   // output buffering starts here
foreach($query->fetchAll() as $domain){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://'.$domain['name']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $DEFAULT_REQUEST_TIMEOUT);
curl_setopt($ch, CURLOPT_TIMEOUT, $DEFAULT_REQUEST_TIMEOUT);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 2);
curl_multi_add_handle($mh, $ch);
$handlers[] = ['ch'=>$ch, 'domain_id'=>$domain['domain_id']];
echo $domain['name'];
}

// Execute the handles
$active = null;
do {
  $mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);

while ($active && $mrc == CURLM_OK) {
   // Wait for activity on any curl-connection
   if (curl_multi_select($mh) == -1) {
    usleep(1);
}

   // Continue to exec until curl is ready to
   // give us more data
   do {
      $mrc = curl_multi_exec($mh, $active);
   } while ($mrc == CURLM_CALL_MULTI_PERFORM);
}

// Extract the content
$values = [];
foreach($handlers as $key => $handle){
// Check for errors
echo $key.'. result: ';
$curlError = curl_error($handle['ch']);
if($curlError == ""){
    $res = curl_multi_getcontent($handle['ch']);
    echo 'done';
}
else {
    echo "Curl error on handle $key: $curlError".' <br />';
}

  // Remove and close the handle
  curl_multi_remove_handle($mh, $handle['ch']);
  curl_close($handle['ch']);
}
// Clean up the curl_multi handle
curl_multi_close($mh);
ob_end_flush() ; // output flushed here

Source - http://php.net/manual/en/function.ob-start.php