Closes or releases a multi-transfer handle. This guide demonstrates its use in PHP code and explains when the result or side effect needs extra checks.
What It Does
Closes or releases a multi-transfer handle.
Syntax and Parameters
The examples below show a real call to curl_multi_close with suitable arguments. Read the function call in context: its connection, handle, input data or other prerequisites are created before use.
Requirements: cURL extension; HTTP client and transfer-handling tools.
Compatibility and security: Unlike the deprecated curl_close/curl_share_close functions, this closes a multi handle.
Example 1: Core Behavior
The first case demonstrates the precise handle or result type.
<?php
$mh = curl_multi_init();
$handles = [];
foreach (['health', 'catalog'] as $path) {
$ch = curl_init('https://api.example.test/' . $path);
curl_setopt_array($ch, [CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => 4]);
curl_multi_add_handle($mh, $ch);
$handles[] = $ch;
}
foreach ($handles as $ch) curl_multi_remove_handle($mh, $ch);
curl_multi_close($mh);
echo "Multi handle released";
Observe the returned value or changed resource state. A successful function call can still require additional checks before a higher-level operation is considered successful.
Example 2: Alternative or Failure Case
The second case checks behavior under a different configuration or request.
<?php
$mh = curl_multi_init();
$handles = [];
foreach (['health', 'catalog'] as $path) {
$ch = curl_init('https://api.example.test/' . $path);
curl_setopt_array($ch, [CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => 5]);
curl_multi_add_handle($mh, $ch);
$handles[] = $ch;
}
foreach ($handles as $ch) curl_multi_remove_handle($mh, $ch);
curl_multi_close($mh);
echo "Multi handle released";
echo "Remaining requests: 1";
Compare the different inputs, environment assumptions or result checks. Do not use a transport-level success value as proof that all business-level requirements were met.
Practical Example: Applying the Rule
The practical case shows how to inspect the operation outcome instead of assuming success.
<?php
$mh = curl_multi_init();
$handles = [];
foreach (['health', 'catalog'] as $path) {
$ch = curl_init('https://api.example.test/' . $path);
curl_setopt_array($ch, [CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => 6]);
curl_multi_add_handle($mh, $ch);
$handles[] = $ch;
}
foreach ($handles as $ch) curl_multi_remove_handle($mh, $ch);
curl_multi_close($mh);
echo "Multi handle released";
echo "Remaining requests: 2";
In an application, convert low-level failures into controlled responses and log only non-sensitive diagnostic context.
Important Behavior and Edge Cases
Remove easy handles when done; this API is distinct from the deprecated curl_close easy-handle operation.
TLS validation, bounded timeouts and a destination allowlist are essential for network requests involving potentially untrusted targets.
Comparison with Related Functions or Patterns
| Approach | When it differs |
|---|---|
| curl_multi_close | Closes or releases a multi-transfer handle. |
| Related API or pattern | curl_multi_remove_handle detaches an individual easy handle. |
Common Beginner Mistakes
| Mistake | Correct approach |
|---|---|
| Failing to advance or clean up handles in the multi-transfer event loop | Remove easy handles when done; this API is distinct from the deprecated curl_close easy-handle operation. |
| Substituting a related API without checking its semantics | curl_multi_remove_handle detaches an individual easy handle. |
Key Takeaways
- Closes or releases a multi-transfer handle.
- Remove easy handles when done; this API is distinct from the deprecated curl_close easy-handle operation.
- curl_multi_remove_handle detaches an individual easy handle.
Select this API or pattern based on its documented behavior and the trust boundaries in your own application, and test all external integrations against their actual deployments.