In PHP, the curl_multi_* functions provide an efficient way to send multiple HTTP requests concurrently. Specifically, the curl_multi_info_read function helps manage and process the return data of these requests. This article will explore in detail how to manage and handle the return data from multiple cURL requests using curl_multi_info_read, ensuring efficiency and accuracy.
In PHP, the curl extension provides a rich API for making HTTP requests. When you need to make multiple HTTP requests, you typically use the curl_multi_* functions to execute these requests in parallel. The curl_multi_exec function is used to perform the multiple requests, while curl_multi_info_read is used to obtain the execution information and return data for each request.
First, use curl_multi_init() to initialize a multi cURL handle. Then, create individual cURL handles for each request and add them to the multi cURL handle. Here’s a simple example:
<?php
// Initialize a multi cURL handle
$multiCurl = curl_multi_init();
// Create individual cURL request handles
$curlHandles = [];
$urls = ['https://gitbox.net/api/data1', 'https://gitbox.net/api/data2', 'https://gitbox.net/api/data3'];
foreach ($urls as $url) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_multi_add_handle($multiCurl, $ch);
$curlHandles[] = $ch;
}
// Execute requests
do {
$status = curl_multi_exec($multiCurl, $active);
if ($active) {
// Wait for activity to finish
curl_multi_select($multiCurl);
}
} while ($active && $status == CURLM_OK);
// Handle each request's result
foreach ($curlHandles as $ch) {
$response = curl_multi_getcontent($ch);
// Process the returned data
echo $response . PHP_EOL;
// Close the handle
curl_multi_remove_handle($multiCurl, $ch);
curl_close($ch);
}
// Close the multi cURL handle
curl_multi_close($multiCurl);
?>
In the code above, we add each cURL request handle to the multi cURL handle $multiCurl using curl_multi_add_handle, and then execute these requests concurrently using curl_multi_exec.
The curl_multi_info_read function is used to retrieve the return information for each request from the multi cURL handle. Through this function, we can obtain the execution status, error information, and return content of each request. Here’s how to use it to manage and handle the return data from multiple requests:
<?php
// Assume multiple cURL requests have already been initialized and executed (as shown above)
// Retrieve the execution information for each request
while ($done = curl_multi_info_read($multiCurl)) {
// Get the request status
$status = $done['result'];
$ch = $done['handle'];
if ($status === CURLE_OK) {
// Request succeeded
$response = curl_multi_getcontent($ch);
echo "Request succeeded, returned data: " . $response . PHP_EOL;
} else {
// Request failed
echo "Request failed, error message: " . curl_error($ch) . PHP_EOL;
}
// Remove the completed handle
curl_multi_remove_handle($multiCurl, $ch);
curl_close($ch);
}
// Close the multi cURL handle
curl_multi_close($multiCurl);
?>
In this example, we use curl_multi_info_read to retrieve the return information for each request. If the request is successful, we retrieve the returned data using curl_multi_getcontent. Otherwise, we print the error message.
When multiple requests are executed in parallel, the return data for each request may have different structures or content. Therefore, when handling the return data, we need to consider the following aspects:
Consistency of Data Format: Ensure that the data format for all returned data is consistent to facilitate further processing. For example, consider converting the returned data into JSON format for uniform parsing.
Error Handling: Each request must handle failure cases. Through curl_multi_info_read, you can obtain error information to help with debugging and problem analysis.
Asynchronous Data Processing: In some cases, the returned data may need to be processed asynchronously, such as saving it to a database or making other API calls. In these cases, a queue system or storing the data in a cache for later processing can be used.
Here’s an example that combines data format handling and error handling:
<?php
// Assume multiple cURL requests have been executed and return data has been obtained
while ($done = curl_multi_info_read($multiCurl)) {
$ch = $done['handle'];
$status = $done['result'];
// Check if the request was successful
if ($status === CURLE_OK) {
$response = curl_multi_getcontent($ch);
// Assume the return data is in JSON format
$data = json_decode($response, true);
if ($data === null) {
echo "Failed to parse JSON data: " . json_last_error_msg() . PHP_EOL;
} else {
// Process the data
echo "Successfully retrieved data: " . print_r($data, true) . PHP_EOL;
}
} else {
// Request failed, output error information
echo "Request failed, error message: " . curl_error($ch) . PHP_EOL;
}
// Remove and close the handle
curl_multi_remove_handle($multiCurl, $ch);
curl_close($ch);
}
curl_multi_close($multiCurl);
?>
In this code, we use json_decode to parse the returned JSON data and handle any parsing errors.
Although the curl_multi_* functions allow parallel requests, in high-concurrency environments, we still need to consider the following optimization measures:
Batch Requests: Send requests in batches instead of sending all requests at once to avoid resource exhaustion or performance degradation caused by excessive concurrent requests.
Request Timeout Settings: Set appropriate timeout limits to prevent a single request from blocking others. Use CURLOPT_TIMEOUT or CURLOPT_TIMEOUT_MS to set the timeout for requests.
Connection Pooling: Use cURL’s connection reuse mechanism to reduce the overhead of repeatedly establishing connections. This can be optimized by setting CURLOPT_FORBID_REUSE and CURLOPT_FRESH_CONNECT.
Through the curl_multi_info_read and curl_multi_* functions, PHP can efficiently manage and handle multiple parallel HTTP requests. In practical applications, we should not only focus on parallel execution but also effectively handle the returned data to ensure reliability and performance.
We hope this article helps you better understand how to use curl_multi_info_read in PHP to manage multiple requests and handle their returned data.
Related Tags:
cURL