Current Location: Home> Latest Articles> curl_multi_info_read: How It Helps You Monitor the Execution Status of Multiple cURL Threads in Real Time

curl_multi_info_read: How It Helps You Monitor the Execution Status of Multiple cURL Threads in Real Time

gitbox 2025-08-13

When developing PHP programs that involve multiple HTTP requests, especially for large-scale API calls, cURL is a popular option. To enhance efficiency, PHP provides a multi-threaded cURL function set called curl_multi_*, which can send multiple requests concurrently. This allows the program to process multiple requests at the same time instead of waiting for each one to complete sequentially.

However, when using multi-threaded cURL, it’s not enough to just send requests; we also need to retrieve real-time execution statuses, error information, and returned content for each request. The curl_multi_info_read() function is one of the tools that help us obtain this information.

1. What is curl_multi_info_read()?

The curl_multi_info_read() function in PHP is used to get the execution status of multiple parallel cURL requests. Through it, developers can monitor the progress and status of each request in real time while multiple requests run concurrently. This function returns an array containing the current request’s status, including the HTTP status code, whether the request succeeded, and other details.

2. How to Use curl_multi_info_read()?

To use curl_multi_info_read(), you first initialize a multi-threaded cURL session with curl_multi_init(), then add multiple individual cURL handles to this session via curl_multi_add_handle(). Finally, you can execute the requests concurrently using curl_multi_exec() and retrieve each request’s execution status with curl_multi_info_read().

Here is a simple example:

<span><span><span class="hljs-meta">&lt;?php</span></span><span>
</span><span><span class="hljs-comment">// Initialize multi-threaded cURL session</span></span><span>
</span><span><span class="hljs-variable">$mh</span></span><span> = </span><span><span class="hljs-title function_ invoke__">curl_multi_init</span></span><span>();
<p></span>// Create multiple cURL handles<br>
$ch1 = curl_init("<a rel="noopener" target="_new" class="" href="https://www.example.com">https://www.example.com</a>");<br>
$ch2 = curl_init("<a rel="noopener" target="_new" class="" href="https://www.example.org">https://www.example.org</a>");</p>
<p>// Set cURL options<br>
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);<br>
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);</p>
<p>// Add cURL handles to the multi-threaded session<br>
curl_multi_add_handle($mh, $ch1);<br>
</span>curl_multi_add_handle($mh, $ch2);</p>
<p></span>// Execute the requests<br>
do {<br>
$status = curl_multi_exec($mh, $active);<br>
</span>if ($active) {<br>
// Wait for requests to complete<br>
curl_multi_select($mh);<br>
}<br>
} while ($active);</p>
<p>// Get the execution status of each request<br>
while ($info = curl_multi_info_read($mh)) {<br>
if ($info['result'] === CURLE_OK) {<br>
echo "Request to " . $info['handle'] . " succeeded.\n";<br>
} else {<br>
echo "Request to " . $info['handle'] . " failed with error: " . curl_error($info['handle']) . "\n";<br>
}<br>
}</p>
<p>// Cleanup<br>
curl_multi_remove_handle($mh, $ch1);<br>
curl_multi_remove_handle($mh, $ch2);<br>
curl_multi_close($mh);<br>
?><br>
</span>

3. Function Details

curl_multi_info_read() returns an array containing request information, including the following fields:

  • handle: The corresponding cURL handle.

  • result: Execution result. If the value is CURLE_OK, the request was successful. If it failed, the respective error code is returned.

  • msg: The message type returned (usually CURLMSG_DONE, indicating the request is completed).

The function returns an array; if there is no more request information, it returns null.

4. Real-Time Monitoring of Multiple Request Statuses

Performing multiple parallel requests significantly improves execution efficiency, especially when the requests are independent of each other. Using curl_multi_info_read(), we can monitor the status of each request in real time, ensuring that relevant information is promptly obtained upon completion, allowing for error handling or data processing.

With curl_multi_info_read(), we can:

  • Obtain the execution status of requests: Confirm whether each request succeeded.

  • Capture error information: If a request fails, immediately retrieve the error code for further processing.

  • Retrieve response data in real time: If the request succeeds, get the returned data for processing.

5. Practical Use Cases

In scenarios requiring batch HTTP requests, curl_multi_info_read() becomes especially valuable. Examples include:

  • Bulk data scraping: Requesting multiple websites simultaneously to fetch and parse data.

  • Parallel API calls: When multiple external APIs need to be called, initiating requests concurrently and retrieving results.

  • High-volume concurrent requests: Efficiently sending and handling multiple requests to avoid blocking.

6. Conclusion

curl_multi_info_read() offers a powerful and practical tool in PHP to obtain the execution status of multiple cURL requests. By using this function properly, we can easily monitor the progress of parallel requests, capture error details, and obtain results, greatly improving the efficiency of tasks involving multiple requests. Whether in web scraping, bulk data collection, or parallel API calling, curl_multi_info_read() is an indispensable asset.

  • Related Tags:

    cURL