Home >Backend Development >PHP Tutorial >Introduction to PHP functions—curl_multi_exec(): Execute cURL sessions with multiple handles
PHP function introduction—curl_multi_exec(): Execute cURL sessions with multiple handles
cURL is a powerful open source library for network communication in PHP. It supports various protocols such as HTTP, HTTPS, FTP, etc. and provides many feature-rich options, making it easy to send requests and get responses.
In many cases, we need to process multiple URL requests at the same time, then we need to use the curl_multi_exec()
function. This function allows us to execute multiple cURL handles at the same time, thereby improving the efficiency of requests. This article will introduce you to the use of the curl_multi_exec()
function in detail and provide corresponding code examples.
int curl_multi_exec(resource $multi_handle, int &$still_running)
$multi_handle
: Resource of multiple handles of cURL, created through the curl_multi_init()
function. $still_running
: A variable used to store the number of handles still running. If an error occurs, the curl_multi_exec()
function will return an error code; otherwise, it will return 0 to indicate success.
The following is a sample code that uses the curl_multi_exec()
function to send multiple requests concurrently:
<?php // 创建cURL多个句柄 $multi_handle = curl_multi_init(); // 创建多个cURL会话并添加到多句柄中 $handles = array(); $handles[] = create_curl_handle("https://example.com/api1"); $handles[] = create_curl_handle("https://example.com/api2"); $handles[] = create_curl_handle("https://example.com/api3"); foreach ($handles as $handle) { curl_multi_add_handle($multi_handle, $handle); } // 执行多个cURL会话 $running = null; do { curl_multi_exec($multi_handle, $running); curl_multi_select($multi_handle); // 等待I/O事件 } while ($running > 0); // 获取每个请求的响应 foreach ($handles as $handle) { $response = curl_multi_getcontent($handle); // 处理响应数据 echo $response; // 关闭cURL会话 curl_multi_remove_handle($multi_handle, $handle); curl_close($handle); } // 关闭多个句柄 curl_multi_close($multi_handle); // 创建一个cURL会话,并设置选项 function create_curl_handle($url) { $handle = curl_init(); curl_setopt($handle, CURLOPT_URL, $url); curl_setopt($handle, CURLOPT_RETURNTRANSFER, true); return $handle; } ?>
The above code is first created Create a cURL multi-handle resource and create multiple cURL sessions through a loop. Next, add these sessions to the multihandle and execute multiple cURL sessions. At each execution, the curl_multi_select()
function is called to wait for I/O events. Finally, obtain the response of each request through the curl_multi_getcontent()
function and process it accordingly.
curl_multi_exec()
function, be sure to call curl_multi_add_handle()
to add each session Add to multiple handles. curl_multi_getcontent()
function to obtain response data, be sure to ensure that the request has been completed, that is, the number of running handles is 0. curl_multi_remove_handle()
function to remove a session from a multi-handle, and use curl_close()
to close the session. curl_multi_exec()
The function is a very useful function that can execute multiple cURL sessions at the same time to improve request efficiency. Through the above code example, we can understand how to use this function to send multiple requests concurrently and obtain the response data for each request. In actual development, we can further optimize and expand this sample code according to our own needs.
The above is the detailed content of Introduction to PHP functions—curl_multi_exec(): Execute cURL sessions with multiple handles. For more information, please follow other related articles on the PHP Chinese website!