Send HTTP request from PHP without waiting for response?

Send HTTP request from PHP without waiting for response?

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Fire-and-Forget HTTP Requests in PHP

Server-side analytics often require sending data to a third-party endpoint without waiting for a response. This pattern is common when tracking page views or events directly from the application server rather than relying on client-side JavaScript. The goal is to fire an HTTP GET request, such as http://tracker.example.com?product_number=5230&price=123.52, and return control to the main script immediately. You do not care if the tracker returns a 200 OK, a 404 Not Found, or even a redirect; all responses must be discarded to ensure they do not block the request thread.

Achieving this in PHP requires bypassing the synchronous nature of standard HTTP clients like file_get_contents or cURL's default behavior. The most robust approach involves using cURL with asynchronous operations and explicit stream handling. While forking a shell process to execute an external curl command is conceptually simple, it introduces significant overhead under heavy load. Spawning child processes consumes memory and requires the operating system to manage context switches. If your application handles thousands of requests per second, this approach can degrade performance or exhaust available file descriptors.

A better alternative within the PHP process itself is to utilize cURL's multi-handle API combined with asynchronous I/O. This allows you to initiate a request and detach from it before the response arrives. The key lies in using CURLOPT_NOBODY or simply ignoring the response code by not calling curl_getinfo on the handle after execution, while ensuring the stream is closed immediately. However, for true non-blocking behavior where the script continues executing subsequent lines without waiting for the network socket to settle, you must leverage PHP's event loop or run the cURL request in a separate thread using extensions like pthreads (though this adds complexity) or simply rely on the fact that modern PHP engines handle I/O efficiently when not explicitly blocked.

For a generic solution that avoids external dependencies like the php-ga package, which might obscure internal implementation details, you can implement a custom handler using the cURL extension directly. The strategy involves initializing a cURL handle with specific options to suppress errors and output:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://tracker.example.com?product_number=5230&price=123.52');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false); // Do not return transfer as a string
curl_setopt($ch, CURLOPT_TIMEOUT, 5);            // Timeout after 5 seconds to prevent hanging
curl_setopt($ch, CURLOPT_HEADER, false);         // Discard headers

// Execute the request. 
// With CURLOPT_RETURNTRANSFER set to false, cURL will still block by default unless used in a multi-handle context.
// To achieve true non-blocking behavior in synchronous PHP without threads, you often need to 
// rely on stream_select or similar mechanisms, but for simple fire-and-forget, ignoring the result is key.
curl_exec($ch);

// Discard the response
$response_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); // Optional: only if you want to know later
curl_close($ch);

However, note that even with CURLOPT_RETURNTRANSFER set to false, a standard curl_exec call will block until the connection closes or times out. If the upstream server sends a 301 redirect and follows it, or if the connection hangs indefinitely, your PHP script pauses. To truly avoid blocking without threading, you must use a multi-handle setup where you check for completion periodically using curl_multi_exec and curl_multi_select.

$mh = curl_multi_init();
$ch = curl_init('http://tracker.example.com?product_number=5230&price=123.52');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 5); // Short timeout to minimize delay if server hangs

curl_multi_add_handle($mh, $ch);

do {
    $mrc = curl_multi_exec($mh, $running);
    if ($mrc === CURLM_CALL_MULTI_PERFORM) {
        // Continue executing in the next iteration
    } elseif ($mrc === CURLM_OK) {
        // Check for completion
        $status = curl_multi_info_read($mh, &$msgs);
        if ($status['num_msg'] > 0) {
            curl_multi_remove_handle($mh, $ch);
            curl_multi_close($mh);
            break;
        }
    }
    
    // If running is true, we need to wait for the socket to be ready.
    // In a CLI environment, this blocks. In a web server with async support (like Swoole), it does not.
    if ($running > 0) {
        usleep(1000); // Sleep briefly to prevent CPU spinning
    }
} while ($running > 0 || $mrc === CURLM_CALL_MULTI_PERFORM);

curl_multi_close($mh);

This multi-handle approach gives you granular control over when the script proceeds. If your environment supports it, integrating this into a framework like Laravel allows for cleaner separation of concerns. While specific third-party libraries exist for analytics, understanding the underlying cURL mechanics ensures you avoid pitfalls like unexpected redirects or long timeouts that could inadvertently degrade application performance. Always set a reasonable timeout; leaving it to default can cause requests to hang indefinitely on slow networks or unresponsive trackers.

Our network