4.x is outdated, please check the latest version 22.x

Latest version: pecl install openswoole-22.1.2


<?php Swoole\Coroutine\Http\Server->handle(string $pattern, callable $function): void



The URL path or pattern you want to respond to when a HTTP request comes in. Do not include the domain or protocol here, just the URL path.


A callback function to handle the HTTP request by the matched URL path. The callback acts simailar to the normal Swoole HTTP Request event where you are given a request and response object, see the example below.




Register the callback function which processes a specific URL path for HTTP requests.

Using the handle() function, you must register all the URL paths you want to react to, for example the following are potential HTTP pages:

  • /home/jobs/latest
  • /posts/programming/php
  • /blog/2021/08/how-to-grow-traffic

When a HTTP request is successfully accepted and matched to a URL pattern, the server will automatically create a coroutine context for you, so you don't need to call Co\run inside your handle callbacks.

With the URL examples above you could register multiple handle function callbacks but, for a more scalable solution you can register one handle() callback and set the URL path to /. This acts as the root path and every request will just fallback to this root path, allowing you to use $request->server['request_uri'] to perform routing yourself. You can then write your own routing system or use a pre-built one.

The built in pattern matching with this method is very basic and should not be relied on, for example, the URL path /test111 will also match to /test. This is because the pattern matching falls back to the most matchable URL path, that is why it is recommended to just register the root path of / and handle routing yourself. There is no support for wildcards, case-insensitive matching, matching algorithms or prefix matching, so it is best to get the request URI and use your own routing service.

This server also supports keep-alive and thus, will keep accepting new requests and won't exit. A sub-coroutine will process new requests - This only happens if the client supports keep-alive. If the client does not support keep-alive then, the connection will automatically be closed and the sub-coroutine will exit. This still means new requests can be accepted though.

You must register all handle callbacks before starting the server



function callback(Swoole\Http\Request $request, Swoole\Http\Response $response)
    $response->end("Hello World! - From Swoole.\n");

If you register a URL path multiple times, any patterns which are the same will just overwrite to what was previously set.

There is no need to create a coroutine context with Co\run inside your callback, Swoole does this automatically for you upon a new request.

If no pattern is matched, Swoole will return a HTTP 404 error to the client.

The request and response objects are the same as the ones used in the normal Swoole\HTTP\Server server so checkout its documentation on its methods etc.

Last updated on August 31, 2022