Writing Your First Swoole PHP

In case you are yet to setup swoole php, I will recommend you check out my previous post on this - https://blog.africoders.com/archives/5-Setting-Up-SWOOLEPHP-on-OSX.html

If you have done that, then the next step is easy.


Create a file called sample1.php

$server = new Swoole\HTTP\Server("", 9501);

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

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


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:

[email protected] swoole % php sample1.php

Swoole http server is started at

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/

Categories: PHP

No comments

Add Comment

Enclosing asterisks marks text as bold (*word*), underscore are made via _word_.
Standard emoticons like :-) and ;-) are converted to images.

To prevent automated Bots from commentspamming, please enter the string you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.