Content · Web Development · PHP

PHP curl_multi_exec() Function: Usage and Examples

Drives transfers registered on a multi-handle until libcurl reports.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 admin

Drives transfers registered on a multi-handle until libcurl reports progress. This guide demonstrates its use in PHP code and explains when the result or side effect needs extra checks.

What It Does


Drives transfers registered on a multi-handle until libcurl reports progress.

Syntax and Parameters


The examples below show a real call to curl_multi_exec 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.

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;
}
do {
    $rc = curl_multi_exec($mh, $running);
    if ($running) { $ready = curl_multi_select($mh, 0.5); if ($ready === -1) usleep(10000); }
} while ($running && $rc === CURLM_OK);
foreach ($handles as $ch) { echo curl_getinfo($ch, CURLINFO_RESPONSE_CODE) . PHP_EOL; curl_multi_remove_handle($mh, $ch); }

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;
}
do {
    $rc = curl_multi_exec($mh, $running);
    if ($running) { $ready = curl_multi_select($mh, 0.5); if ($ready === -1) usleep(10000); }
} while ($running && $rc === CURLM_OK);
foreach ($handles as $ch) { echo curl_getinfo($ch, CURLINFO_RESPONSE_CODE) . PHP_EOL; curl_multi_remove_handle($mh, $ch); }

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;
}
do {
    $rc = curl_multi_exec($mh, $running);
    if ($running) { $ready = curl_multi_select($mh, 0.5); if ($ready === -1) usleep(10000); }
} while ($running && $rc === CURLM_OK);
foreach ($handles as $ch) { echo curl_getinfo($ch, CURLINFO_RESPONSE_CODE) . PHP_EOL; curl_multi_remove_handle($mh, $ch); }

In an application, convert low-level failures into controlled responses and log only non-sensitive diagnostic context.

Important Behavior and Edge Cases


Repeat while work remains, and use curl_multi_select to avoid tight CPU loops.

TLS validation, bounded timeouts and a destination allowlist are essential for network requests involving potentially untrusted targets.

Comparison with Related Functions or Patterns


ApproachWhen it differs
curl_multi_execDrives transfers registered on a multi-handle until libcurl reports progress.
Related API or patterncurl_exec transfers through a single easy handle.

Common Beginner Mistakes


MistakeCorrect approach
Treating transport completion as proof that the HTTP response succeededRepeat while work remains, and use curl_multi_select to avoid tight CPU loops.
Substituting a related API without checking its semanticscurl_exec transfers through a single easy handle.

Key Takeaways


  • Drives transfers registered on a multi-handle until libcurl reports progress.
  • Repeat while work remains, and use curl_multi_select to avoid tight CPU loops.
  • curl_exec transfers through a single 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.

Share this resourceSend this page to someone who may find it useful.