Swoole\HTTP\Server->on('request', fn)

Declaration

<?php
Swoole\HTTP\Server->on('request', callback $callback)

Parameters

event

The event name

callback

callback function

Return

success

if success, it returns TRUE, otherwise it returns FALSE.

Description

Executue the callback function when receiving HTTP request.

Example

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

$http->on('request', function ($request, $response) {
    $response->end("<h1>Hello World. #".rand(1000, 9999)."</h1>");
});

$http->start();