How to make HTTP requests in PHP and not wait on the response

How to make HTTP requests in PHP and not wait on the response

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

Stefan Bogdanescu

Founder & Senior Architect

Asynchronous HTTP Requests in PHP: Beyond file_get_contents

The standard approach to making an HTTP request in synchronous PHP is straightforward but inherently blocking. Functions like file_get_contents(), curl_exec(), or Guzzle's sync methods pause the execution of your script until the remote server responds or times out. If that external service is slow or unresponsive, your application logic sits idle, consuming resources without producing value. In high-traffic applications where you need to trigger "fire and forget" events or kick off long-running processes, this blocking behavior creates a bottleneck.

To decouple the initiation of an HTTP request from the receipt of its response, you must move away from synchronous execution models. The most robust native solution involves using cURL's asynchronous capabilities or leveraging a dedicated async framework like ReactPHP or Amphp. However, for many use cases, simply spawning a new process is the cleanest architectural decision without introducing complex dependency trees.

Spawning Background Processes

The simplest way to fire an HTTP request without waiting for the response is to execute it in a separate child process. You can wrap your synchronous logic inside a dedicated PHP script and launch it using proc_open or popen. This ensures the main thread continues immediately while the child process handles the network I/O.

For instance, you might create a helper script like send-event.php:

#!/usr/bin/env php
<?php

$payload = json_encode(['event' => 'user_signed_up', 'user_id' => 123]);
$url = 'https://api.external-service.com/webhook';

$context = [
    'http' => [
        'method'  => 'POST',
        'header'  => "Content-Type: application/json\r\n",
        'content' => $payload,
    ],
];

$options = stream_context_create($context);
$result = file_get_contents($url, false, $options);

// The result is discarded in a fire-and-forget scenario.
// We might log the fact that we tried to send it here.
echo "Request dispatched\n";

Then, from your main application, you spawn this script:

$process = proc_open('php send-event.php', [
    0 => ['pipe', 'r'],
    1 => ['pipe', 'w'],
    2 => ['pipe', 'w'],
], $pipes);

if (is_resource($process)) {
    // Main code continues immediately.
    echo "Event queued. Processing request in background.\n";
} else {
    die('Failed to spawn process');
}

This approach isolates the network latency from your primary request flow. If the external API takes five seconds to respond, your user sees an immediate success screen. The trade-off is increased resource overhead due to forked processes and potential complexity in error handling across process boundaries.

Native cURL Multi-Handle

If spawning processes feels too heavy or you need finer control within a single process context, PHP's cURL extension supports multi-handling. This allows you to queue multiple requests and handle them concurrently without blocking the main thread, though true non-blocking I/O in pure PHP requires an event loop.

Using curl_multi_init, you can add handles and then enter a loop that polls for activity. However, note that standard PHP CLI or web servers do not run inside an event loop by default. To achieve true "fire and forget" within the same process without spawning a child, you typically need to integrate with an async framework like ReactPHP, which manages the event loop explicitly.

When to Use Which Approach

Choosing between spawning a process and using multi-handling depends on your architecture. If you are building a web application where every request must remain responsive, offloading heavy or asynchronous tasks to a message queue (like RabbitMQ or Redis) is often superior to both methods above. You can enqueue the HTTP call as a job, letting a worker pick it up later. This scales horizontally and prevents your web server from ever waiting on external dependencies.

The https://laravelcompany.com platform offers robust examples of integrating such asynchronous patterns into modern PHP stacks, particularly when leveraging Laravel's queue system to manage these fire-and-forget tasks efficiently. Their documentation often highlights how to structure background jobs so that the HTTP client is never a blocking constraint for the user-facing API.

Ultimately, avoiding synchronous waits requires accepting that you lose immediate visibility into the request's success or failure unless you implement callbacks or polling mechanisms. For event-driven architectures, this lack of immediate feedback is often an acceptable trade-off for maintaining high throughput and low latency.

Our network