Explain curl in PHP_ Implementation of multi concurrency

Time:2020-7-30

Curl in PHP_ Multi series functions can request multiple URLs at the same time to achieve concurrency, instead of blocking the request until the result returns. Therefore, when you request URLs in bulk, you can use curl_ Multi series functions improve the efficiency of the program.

Curl normal request

$startTime = microtime(true);
$chArr = [];
$optArr = [
  CURLOPT_URL => 'http://www.httpbin.org/ip',
  CURLOPT_HEADER => 0,
  CURLOPT_RETURNTRANSFER => 1,
];
$result = [];

//Create multiple curl resources and execute
for ($i=0; $i<10; $i++) {
  $chArr[$i] = curl_init();
  curl_setopt_array($chArr[$i], $optArr);
  $result[$i] = curl_exec($chArr[$i]);
  curl_close($chArr[$i]);
}

$endTime = microtime(true);
echo sprintf("use time: %.3f s".PHP_EOL, $endTime - $startTime);

use time: 6.080 s

curl_ Multi concurrent requests

$startTime = microtime(true);
$chArr = [];
$optArr = [
  CURLOPT_URL => 'http://www.httpbin.org/ip',
  CURLOPT_HEADER => 0,
  CURLOPT_RETURNTRANSFER => 1,
];
$result = [];

//Create multiple curl resources
for ($i=0; $i<10; $i++) {
  $chArr[$i] = curl_init();
  curl_setopt_array($chArr[$i], $optArr);
}
//Create batch curl handle
$mh = curl_multi_init();
//Add a single curl handle to a batch curl handle
foreach ($chArr as $ch) {
  curl_multi_add_handle($mh, $ch);
}
//A reference to the identity that determines whether the operation is still in progress
$active = null;
/**
 *This loop processes the $ch handle in the $MH batch for the first time, and writes the execution status of the $MH batch to $active,
 *When the status value is equal to curlm_ CALL_ MULTI_ When performing, it indicates that the data is still being written or read, and the loop is executed,
 *When the first time the data of the $ch handle is successfully written or read, the status value changes to curlm_ OK, jump out of this loop and enter the next big loop.
 */
do {
  //Handle every handle in the batch stack
  $mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
/**
 *In the above code, you can directly use $active > 0 as the condition of while, as follows:
 * do {
 *  $mrc = curl_multi_exec($mh, $active);
 * } while ($active > 0);
 *At this time, if the whole batch handle is not executed completely, the system will execute curl continuously_ multi_ Exec function, resulting in high CPU consumption of the system,
 *Therefore, this scheme is generally not adopted, and curl can be used_ multi_ Select function to achieve no need to read the program on the purpose of blocking.
 */

/**
 *When $active is true, that is, there is a $ch handle waiting to be processed in the $MH batch,
 * $mrc == CURLM_ OK, that is, the last read or write of the $ch handle has completed.
 */
while ($active && $mrc == CURLM_OK) {
  /** 
   *The program enters a blocked state until there is an active connection in the batch (that is, there is an executable $ch handle in the $MH batch),
   *The benefit of doing this is that the $ch handle in the $MH batch will end after reading or writing data ($MRC = = curlm)_ OK) into the blocking phase,
   *Instead, curl is not executed continuously during the entire $MH batch execution_ multi_ Exec function, a waste of CPU resources.
   */
   if (curl_multi_select($mh) != -1) {
    //The program exits the blocked state and continues to execute the $ch handle that needs to be processed
    do {
      $mrc = curl_multi_exec($mh, $active);
    } while ($mrc == CURLM_CALL_MULTI_PERFORM);
  }
}

foreach ($chArr as $i=>$ch) {
  //Gets the return value of a curl handle
  $result[$i] = curl_multi_getcontent($ch);
  //Remove handle from a batch of resources
  curl_multi_remove_handle($mh, $ch);
}
//Close a set of curl handles
curl_multi_close($mh);
$endTime = microtime(true);
echo sprintf("use time: %.3f s".PHP_EOL, $endTime - $startTime);

use time: 0.599 s

By comparing the running time of the above program, we can know that curl is used_ Multi series function concurrent request is much more efficient than ordinary curl function.

This article is about explaining curl in PHP in detail_ Multi concurrent implementation of the article introduced here, more related PHP curl_ Multi concurrent content, please search the previous articles of developeppaer or continue to browse the related articles below. I hope you can support developeppaer more in the future!