Backend Development 10 min read

Implementing Asynchronous Processing in PHP Using fsockopen, popen, curl and AJAX

This article explains how to achieve asynchronous processing in PHP by using non‑blocking socket connections, ajax calls, popen, curl, and fsockopen, providing code examples and discussing their advantages and limitations for backend development.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Implementing Asynchronous Processing in PHP Using fsockopen, popen, curl and AJAX

PHP is widely used as a backend server, but many scenarios require asynchronous processing when the client does not need to wait for long‑running tasks such as logging, email sending, or heavy computations.

The basic idea is to open a non‑blocking socket to the local machine, let the server handle the time‑consuming work, and immediately return a response to the client, effectively achieving asynchronous behavior.

One simple implementation uses fsockopen to create a socket, sets it to non‑blocking mode with stream_set_blocking($fp, false) , sends a request, and closes the connection:

<code>$fp = fsockopen($php_Path, 80);
if (!$fp) {
    LMLog::error("fsockopen:err");
} else {
    $out = "GET /album/action/album_write_friends_thread_record.php?key=&u= HTTP/1.1\r\n";
    $out .= "Host: " . $php_Path . "\r\n";
    $out .= "Connection: Close\r\n\r\n";
    stream_set_blocking($fp, true);
    stream_set_timeout($fp, 1);
    fwrite($fp, $out);
    usleep(1000);
    fclose($fp);
}</code>

The target script ( album_write_friends_thread_record.php ) may contain long‑running code such as sleep(20); , but because the socket request is sent asynchronously, the client receives a response immediately while the server continues processing in the background.

Four common ways to achieve PHP asynchronous execution are presented:

AJAX request : embed a jQuery $.get() call or an &lt;img src="..."&gt; tag that triggers a separate PHP script. Simple but may not fire if the page is closed before the request completes.

popen() : open a process pipe with popen('php /path/doRequest.php &', 'r') and immediately close it. Fast, but limited to local scripts and can create many processes under high load.

cURL extension : use curl_init() , set CURLOPT_TIMEOUT (minimum 1 s), and execute the request. Provides full HTTP features but still forces the client to wait at least one second.

fsockopen() (or fscokopen typo in the original): manually build HTTP headers and send the request via a socket. Most flexible, but requires manual header construction and can spawn many child processes under heavy traffic.

Typical usage includes ignoring client aborts and removing execution time limits:

<code>ignore_user_abort(true); // continue even if client disconnects
set_time_limit(0);      // prevent script timeout</code>

Example of a lightweight asynchronous call:

<code>$fp = fsockopen('localhost', 80, $errno, $errstr, 5);
if (!$fp) {
    echo "$errstr ($errno)<br>\n";
}
fputs($fp, "GET another_page.php?flag=1\r\n");
fclose($fp);</code>

In a real‑world scenario, a blog post can trigger an email notification asynchronously: the post is saved, the client receives a success message instantly, and a separate background request sends the emails, optionally logging the result.

Testing with two scripts ( write.php and sendmail.php ) shows that the asynchronous request returns immediately while the email script runs for 10 seconds in the background, confirming the non‑blocking behavior.

backendasynchronouscurlfsockopenpopen
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.