Kritim Yantra
Apr 25, 2025
A new helper in PHP 8.5 called curl_multi_get_handles
makes it easy to see all of the individual requests you’ve added to a multi-cURL session in one go. Instead of keeping track of each handle yourself, you simply ask PHP to give you the full list. Below, we’ll walk through why this matters, how it works, and then show you a beginner-friendly example fetching three different API endpoints at once.
curl_init
) per request. curl_multi_init
), which bundles multiple requests together and runs them in parallel.curl_multi_get_handles
DoesBefore PHP 8.5, if you added three handles to your multi handle, you’d have to remember each one yourself if you ever wanted to inspect or loop through them. Now, you call:
$allHandles = curl_multi_get_handles($multiHandle);
and PHP hands you back an array of every handle currently in that multi handle.
Let’s say you want to grab the current weather for three cities in one go:
// 1. Create the multi handle
$multiHandle = curl_multi_init();
// 2. Prepare three individual cURL handles
$chNY = curl_init('https://api.example.com/weather?city=NewYork');
$chLDN = curl_init('https://api.example.com/weather?city=London');
$chTKY = curl_init('https://api.example.com/weather?city=Tokyo');
// 3. Add them to the multi handle
curl_multi_add_handle($multiHandle, $chNY);
curl_multi_add_handle($multiHandle, $chLDN);
curl_multi_add_handle($multiHandle, $chTKY);
// 4. Run them simultaneously
$running = null;
do {
curl_multi_exec($multiHandle, $running);
curl_multi_select($multiHandle);
} while ($running > 0);
// 5. Now use the new function to get your handles back
$handles = curl_multi_get_handles($multiHandle);
// 6. Loop through each to collect the results
foreach ($handles as $ch) {
$response = curl_multi_getcontent($ch);
echo "Response for handle " . spl_object_hash($ch) . ": $response\n";
curl_multi_remove_handle($multiHandle, $ch);
}
// 7. Clean up
curl_multi_close($multiHandle);
What’s happening here?
curl_multi_get_handles
, which returns all three handles in an array. With PHP 8.5’s curl_multi_get_handles
, managing lots of simultaneous HTTP requests becomes almost effortless. For simple projects or big ones, you’ll spend less time juggling handles and more time building the features you care about.
Happy coding!
No comments yet. Be the first to comment!
Please log in to post a comment:
Sign in with Google