Parallelizing PHP cURL Requests to Avoid Race Conditions
The inconsistency you are experiencing stems from a fundamental misunderstanding of how sequential execution works in single-threaded environments like standard PHP scripts. When you chain curl_init, curl_exec, and curl_close for the pickup task before starting the dropoff task, you are creating a strict dependency. The second request literally cannot begin until the first one has fully completed, returned its data, and been closed.
If the Onfleet API is experiencing latency on the endpoint used for pickup tasksâperhaps due to rate limiting, server-side processing delays, or network jitterâthe script pauses execution there. If that delay pushes the total runtime past a specific threshold defined by an upstream system (like a web server timeout) or a concurrent process monitoring your script, the subsequent dropoff request may appear to "fail" or simply never run because the script terminated before reaching it. It is not a bug in cURL; it is the expected behavior of synchronous code.
To execute these requests simultaneously, you must decouple their execution flow. The standard approach in PHP is to spawn asynchronous processes using proc_open. This allows both cURL sessions to start, run independently, and complete without blocking each other. Since the two POST bodies differ based on the destination address (pickup vs. dropoff), you cannot reuse a single configuration object easily; instead, you pass distinct execution parameters to the spawned process or configure the internal options before launching them.
Here is how you can restructure the logic using proc_open to run both requests in parallel:
// Define shared credentials and base URL
$username = 'xxxxx';
$api_onfleet = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$url_onfleet = "https://onfleet.com/api/v2/tasks";
// Prepare the payload for the Pickup task
$pickup_payload = json_encode([
'destination' => [
'address' => ['unparsed' => $pickup_address],
'notes' => $comments
],
'recipients' => [
['name' => $name, 'phone' => '+61'.$phone, 'notes' => "Number of riders: {$riders}"]
],
'completeBefore' => $timestamp,
'pickupTask' => 'yes',
'autoAssign' => ['mode' => 'distance']
]);
// Prepare the payload for the Dropoff task
$dropoff_payload = json_encode([
'destination' => [
'address' => ['unparsed' => $dropoff_address],
'notes' => $comments
],
'recipients' => [
['name' => $name, 'phone' => '+61'.$phone, 'notes' => "Number of riders: {$riders}"]
],
'autoAssign' => ['mode' => 'distance']
]);
// Configuration for the child process
$descriptorspec = [
0 => ["pipe", "r"], // stdin
1 => ["pipe", "w"], // stdout
2 => ["pipe", "w"] // stderr
];
$process_handle = proc_open(
'php -r',
$descriptorspec,
$pipes
);
if (is_resource($process_handle)) {
// Write the first command to the pipe for Pickup
fwrite($pipes[0], "
\$ch = curl_init();
curl_setopt(\$ch, CURLOPT_URL, '$url_onfleet');
curl_setopt(\$ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt(\$ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt(\$ch, CURLOPT_USERPWD, '$username:$api_onfleet');
curl_setopt(\$ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt(\$ch, CURLOPT_POST, true);
curl_setopt(\$ch, CURLOPT_POSTFIELDS, '{$pickup_payload}');
echo curl_exec(\$ch);
curl_close(\$ch);
");
// Write the second command to the pipe for Dropoff immediately after
// In a real-world scenario, you might use a single cURL session with multiple handles
// or separate processes depending on complexity. Here we simulate parallelism via pipes.
// Note: For true parallelism in a single script without spawning PHP processes,
// consider using the CurlMultiHandle class which is built into PHP's curl extension.
fclose($pipes[0]);
fclose($pipes[1]);
fclose($pipes[2]);
$output = stream_get_contents($pipes[1]);
$error = stream_get_contents($pipes[2]);
proc_close($process_handle);
}
While spawning PHP processes via proc_open achieves parallelization, it introduces significant overhead regarding process creation and memory usage. A more efficient and idiomatic solution within the PHP ecosystem is to utilize the built-in CurlMultiHandle class. This allows you to manage multiple cURL handles in a single request loop without spawning external processes.
The CurlMultiHandle approach works by initializing multiple independent cURL sessions, adding them to a multi-handle queue, and then entering a polling loop. Inside this loop, you check for activity on any of the handles using curl_multi_select. This function blocks until at least one handle is ready for reading or writing, effectively waiting for network I/O without freezing your application thread indefinitely.
$mh = curl_multi_init();
// Handle 1: Pickup Task
$ch_pickup = curl_init($url_onfleet);
curl_setopt_array($ch_pickup, [
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $pickup_payload,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
CURLOPT_USERPWD => "$username:$api_onfleet",
CURLOPT_SSL_VERIFYPEER => false
]);
// Handle 2: Dropoff Task
$ch_dropoff = curl_init($url_onfleet);
curl_setopt_array($ch_dropoff, [
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $dropoff_payload,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
CURLOPT_USERPWD => "$username:$api_onfleet",
CURLOPT_SSL_VERIFYPEER => false
]);
// Add handles to the multi handle
curl_multi_add_handle($mh, $ch_pickup);
curl_multi_add_handle($mh, $ch_dropoff);
// Execute in a loop until all transfers are complete
do {
$running = null;
if (curl_multi_exec($mh, $running) === CURLM_OK) {
curl_multi_select($mh, 1); // Blocks up to 1 second
}
} while ($running > 0);
// Retrieve results
$ch_pickup_result = curl_multi_getcontent($ch_pickup);
$ch_dropoff_result = curl_multi_getcontent($ch_dropoff);
curl_multi_remove_handle($mh, $ch_pickup);
curl_multi_remove_handle($mh, $ch_dropoff);
curl_multi_close($mh);
This pattern ensures that both requests are initiated immediately. If the network is slow for one request, the other continues to run concurrently. The CurlMultiHandle method is generally preferred over proc_open because it keeps everything within the same memory space and avoids the complexity of managing file descriptors and child processes. It also allows you to handle timeouts gracefully on a per-request basis if needed.
