Backend Development 7 min read

Introduction to Swoole and Its Integration with ThinkPHP5

This article introduces Swoole as a high‑performance asynchronous PHP engine, explains its differences from traditional PHP execution, and provides step‑by‑step guidance on integrating Swoole with ThinkPHP5, including a complete code example and key implementation notes.

360 Quality & Efficiency
360 Quality & Efficiency
360 Quality & Efficiency
Introduction to Swoole and Its Integration with ThinkPHP5

Swoole is a production‑ready asynchronous network communication engine for PHP that enables developers to write high‑performance concurrent TCP, UDP, Unix Socket, HTTP and WebSocket services.

The Swoole extension follows the standard PHP extension build process (phpize, ./configure, make, make install) and integrates low‑level socket handling directly into PHP, allowing asynchronous processing, long‑living connections and task concurrency.

Compared with traditional PHP execution modes (Fast‑CGI, PHP‑FPM, CLI), Swoole scripts run as persistent server processes; they are compiled once and remain in memory, eliminating per‑request recompilation and enabling custom handling of sessions and global variables.

Integration with ThinkPHP5 is achieved via the think‑swoole package (versions 2.0 for ThinkPHP5.0 and 3.0 for ThinkPHP6.0). The package adapts ThinkPHP’s request lifecycle to Swoole’s event‑driven model, supporting static file handling, worker processes and WebSocket.

The following example shows a minimal Swoole HTTP server that boots ThinkPHP5, maps Swoole request data to the usual $_SERVER, $_GET and $_POST superglobals, runs the application and returns the response:

set([
    'enable_static_handler' => true,
    'document_root' => "/var/www/thinkphp_5.0.11/public/static",
    'worker_num' => 5, // number of worker processes
]);
$http->on('WorkerStart', function (swoole_server $server, $worker_id) {
    define('APP_PATH', __DIR__ . '/../application/');
    // load base.php instead of start.php to avoid immediate controller execution
    require __DIR__ . '/../thinkphp/base.php';
});
$http->on('request', function ($request, $response) use ($http) {
    $_SERVER = [];
    if (isset($request->header)) {
        foreach ($request->header as $k => $v) {
            $_SERVER[strtoupper($k)] = $v;
        }
    }
    if (isset($request->server)) {
        foreach ($request->server as $k => $v) {
            $_SERVER[strtoupper($k)] = $v;
        }
    }
    $_GET = [];
    if (isset($request->get)) {
        foreach ($request->get as $k => $v) {
            $_GET[$k] = $v;
        }
    }
    $_POST = [];
    if (isset($request->post)) {
        foreach ($request->post as $k => $v) {
            $_POST[$k] = $v;
        }
    }
    // start output buffering
    ob_start();
    try {
        think\App::run()->send();
    } catch (Exception $e) {
    }
    $res = ob_get_contents();
    ob_end_clean();
    $response->header('Content-Type', 'text/html;charset=utf-8', false);
    $response->end($res);
});
$http->start();

Key points of the code: the WorkerStart event loads the ThinkPHP base file without executing the framework; the request event translates Swoole request data into PHP superglobals so ThinkPHP can operate normally; after running the application the output is captured and sent back to the client.

Because Swoole processes persist, global variables and request data must be cleared between requests; otherwise data from previous requests may leak. Restarting the Swoole server resets the state.

backendWebSocketphpAsyncSwooleThinkPHP5
360 Quality & Efficiency
Written by

360 Quality & Efficiency

360 Quality & Efficiency focuses on seamlessly integrating quality and efficiency in R&D, sharing 360’s internal best practices with industry peers to foster collaboration among Chinese enterprises and drive greater efficiency value.

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.