In PHP, curl is a very powerful library for sending HTTP requests and processing responses. When making concurrent requests, we usually use the curl_multi_* function to perform multithreading. Debugging and troubleshooting errors becomes particularly important as the number of requests increases. This article will explore how to use the curl_multi_close function and curl_error to effectively troubleshoot errors in multithreaded requests.
The curl_multi_* series functions are functions in PHP that initiate multiple HTTP requests simultaneously. Unlike a single curl_exec , curl_multi_* can execute multiple requests at once and process their responses. Its core functions include:
curl_multi_init() : Initialize a cURL multiple handles
curl_multi_add_handle() : Add a single handle to multiple handles
curl_multi_exec() : executes all added cURL requests
curl_multi_getcontent() : Get the response content of each cURL request
curl_multi_remove_handle() : Removes a single handle from multiple handles
curl_multi_close() : Close all cURL requests
curl_multi_close is mainly used to close multi-handle resources after all requests are executed, while curl_error is used to obtain error information in cURL requests. In multi-threaded requests, multiple requests are usually processed at the same time, and errors may not appear immediately. By reasonably combining these two functions, we can help us catch and troubleshoot errors.
First, we need to initialize cURL multi-handles and create an independent cURL handle for each request.
$mh = curl_multi_init(); // Initialize multiple handles
$urls = [
"https://example.com/api/endpoint1",
"https://example.com/api/endpoint2",
"https://example.com/api/endpoint3",
];
$curlHandles = [];
foreach ($urls as $index => $url) {
$ch = curl_init($url); // Initialize a single cURL Handle
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$curlHandles[$index] = $ch;
curl_multi_add_handle($mh, $ch); // 将Handle添加到多Handle中
}
Use curl_multi_exec to execute all requests and monitor their execution status.
$running = null;
do {
curl_multi_exec($mh, $running); // Execute a request
curl_multi_select($mh); // Wait for the operation to complete
} while ($running > 0);
Once all requests are completed, we can get the response content of each request through curl_multi_getcontent and use curl_error to check whether any requests have failed.
foreach ($curlHandles as $index => $ch) {
$response = curl_multi_getcontent($ch); // Get the response content
if (curl_errno($ch)) {
// Capture error message
$error_msg = curl_error($ch);
echo "ask {$urls[$index]} An error occurred: $error_msg\n";
} else {
echo "ask {$urls[$index]} success,Response content: $response\n";
}
curl_multi_remove_handle($mh, $ch); // 从多Handle中移除
curl_close($ch); // 关闭单个Handle
}
Finally, use curl_multi_close to close multiple handles and free up resources.
curl_multi_close($mh); // 关闭多Handle
Suppose we have a list of API requests, and when processing concurrently, we may encounter certain request failures. Through the above method, we can catch the error and print the error message. Common errors include DNS resolution failure, connection timeout, etc. Usually curl_error can help us identify the specific cause of the error.
Using curl_multi_close and curl_error to cooperate with error checking for multi-threaded requests is a very effective debugging method. By reasonably capturing error information for each request, we can quickly locate and resolve issues that arise in concurrent requests. It should be noted that when handling concurrent requests, try to keep the code clear and concise, and avoid excessive nesting and duplicate code.