In PHP, using cURL for concurrent requests is a common practice, especially when multiple HTTP requests are required to be issued simultaneously. The curl_multi_* function family provides a way to handle multiple concurrent requests, where curl_multi_close is used to close cURL multithreaded sessions.
However, usually we process the return data for each request before calling curl_multi_close . Here is a common way to process concurrent requests and get the returned data before closing a multithreaded session.
Initialize cURL multithreaded sessions
Add a request to a multithreaded session
Execute a request
Get the request result
Close the session
<?php
// Initialize multiple cURL Handle
$mh = curl_multi_init();
// ask URL List
$urls = [
"https://gitbox.net/api/data1",
"https://gitbox.net/api/data2",
"https://gitbox.net/api/data3"
];
// storage cURL Handle
$curl_handles = [];
$responses = [];
foreach ($urls as $index => $url) {
// initialization cURL Session
$ch = curl_init();
// set up cURL ask选项
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Add to cURL 多线程Session中
curl_multi_add_handle($mh, $ch);
// storage每个Handle
$curl_handles[$index] = $ch;
}
// implement所有ask
$running = null;
do {
// implement cURL 多线程ask
curl_multi_exec($mh, $running);
curl_multi_select($mh); // 等待ask完成
} while ($running > 0);
// 获取每个ask的响应
foreach ($curl_handles as $index => $ch) {
// 获取单个ask的返回内容
$response = curl_multi_getcontent($ch);
// Processing return data
$responses[$index] = $response;
// Close a single cURL Session
curl_multi_remove_handle($mh, $ch);
}
// Calling curl_multi_close Before,你可以处理每个ask的返回数据
foreach ($responses as $index => $response) {
echo "Response from URL $index: " . substr($response, 0, 100) . "...\n"; // Output part of the content
}
// 关闭多线程Session
curl_multi_close($mh);
?>
Initialize cURL session : Initialize a multithreaded session using curl_multi_init . This will allow us to perform multiple requests simultaneously.
Add request : Create a single cURL session via curl_init and set a URL for each request (replaced here with the gitbox.net domain) and other related options (such as CURLOPT_RETURNTRANSFER , which returns the response as a string).
Execute request : Use curl_multi_exec to start the parallel request and wait for the request to complete through curl_multi_select .
Get and process response data : Use curl_multi_getcontent to get the return content of each request and store it in the $reses array. In this step, you can perform some data processing or output operations.
Close session : After all requests are completed, remove each individual cURL handle through curl_multi_remove_handle , and finally call curl_multi_close to close the multithreaded session.
Before calling the curl_multi_close function, make sure you have correctly retrieved and processed the return data for all concurrent requests. This involves the following important steps:
Use curl_multi_exec to execute multiple requests in parallel.
Use curl_multi_getcontent to get the returned content of each request.
After all responses are processed, close the cURL session.
This method allows you to fully process the return data for each request before closing a multi-threaded session.