Swoole is released as a PHP extension (PECL) and runs as a PHP CLI application, just like php server.php
.
Please check the Try Swoole with Docker guide.
Please check the prerequisites and installation guide.
The following code is a Swoole HTTP Server returning a Hello World
response:
<?php
use Swoole\Http\Server;
use Swoole\Http\Request;
use Swoole\Http\Response;
$server = new Swoole\HTTP\Server("127.0.0.1", 9501);
$server->on("start", function (Server $server) {
echo "Swoole http server is started at http://127.0.0.1:9501\n";
});
$server->on("request", function (Request $request, Response $response) {
$response->header("Content-Type", "text/plain");
$response->end("Hello World\n");
});
$server->start();
To run the server, put the code into a file named server.php
.
Then execute on the command line:
// Start the HTTP server
$ php server.php
// On another terminal...
$ curl http://127.0.0.1:9501/
Interested with Swoole? Get Started with Swoole now!