Skip to content

Writing Your First Swoole PHP

dhtmldhtml Posts: 27

**Create a file called sample1.php
**

<?php
$server = new Swoole\HTTP\Server("127.0.0.1", 9501);

$server->on("start", function (Swoole\Http\Server $server) {
    echo "Swoole http server is started at http://127.0.0.1:9501\n";
});

$server->on("request", function (Swoole\Http\Request $request, Swoole\Http\Response $response) {
    $response->header("Content-Type", "text/plain");
    $response->end("Hello World\n");
});

$server->start();

Executing it:
Locate the file in your CLI and do the following:

$ php sample1.php

This will spin up a server and you will see something like:

$ swoole % php sample1.php

Swoole http server is started at http://127.0.0.1:9501

You can then copy and run that in your browser, you should see:

Hello World

You can find out more about swoole php here - https://www.swoole.co.uk/

Sign In or Register to comment.