Mastering curl_multi_close(): Properly Release Multi‑cURL Resources in PHP
This article explains how the PHP curl_multi_close() function releases resources allocated by curl_multi_init(), demonstrates its proper usage with a complete code example, and highlights why closing multi‑cURL handles is essential for efficient network request handling.
When making network requests in PHP, the cURL library is commonly used, and the curl_multi_close() function is used to close multiple cURL sessions.
The curl_multi_close() function releases resources allocated by curl_multi_init() after all requests have been processed, which is a good practice.
Example
<?php
// Create multiple cURL sessions
$multiHandle = curl_multi_init();
// Add first request
$ch1 = curl_init();
curl_setopt($ch1, CURLOPT_URL, 'https://example.com/api/1');
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
curl_multi_add_handle($multiHandle, $ch1);
// Add second request
$ch2 = curl_init();
curl_setopt($ch2, CURLOPT_URL, 'https://example.com/api/2');
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
curl_multi_add_handle($multiHandle, $ch2);
// Execute and wait for all requests to complete
// ...
// Close sessions
curl_multi_close($multiHandle);
?>The example first creates a multi‑handle with curl_multi_init(), then initializes two individual handles with curl_init(), sets URLs and options, adds them to the multi‑handle via curl_multi_add_handle(), and finally closes the multi‑handle with curl_multi_close() to free resources.
Summary
The curl_multi_close() function is essential for properly releasing resources created by curl_multi_init(), ensuring better performance and avoiding leaks when handling concurrent network requests in PHP.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
php Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
