Swoole HTTP Server

Swoole\Http\Server extends Swoole\Server {
    /* Methods */
    public void on ( string $event_name , callable $callback )
    public void start ( void )
    /* Inherited methods */
    public void Swoole\Server::addlistener ( string $host , int $port , string $socket_type )
    public boolean Swoole\Server::addProcess ( swoole_process $process )
    public mixed Swoole\Server::after ( int $after_time_ms , callable $callback [, string $param ] )
    public boolean Swoole\Server::bind ( int $fd , int $uid )
    public void Swoole\Server::clearTimer ( int $timer_id )
    public void swoole_timer_clear ( int $timer_id )
    public boolean Swoole\Server::close ( int $fd [, boolean $reset ] )
    public boolean Swoole\Server::confirm ( int $fd )
    public array Swoole\Server::connection_info ( int $fd [, int $reactor_id ] )
    public array Swoole\Server::connection_list ( int $start_fd [, int $pagesize ] )
    public void Swoole\Server::defer ( callable $callback )
    public void Swoole\Server\Port::__destruct ( void )
    public mixed Swoole\Server\Port::on ( string $event_name , callable $callback )
    public void Swoole\Server\Port::set ( array $settings )
    public boolean Swoole\Server::exist ( int $fd )
    public void Swoole\Server::finish ( string $data )
    public mixed Swoole\Server::getClientInfo ( int $fd [, int $reactor_id ] )
    public array Swoole\Server::getClientList ( int $start_fd [, int $pagesize ] )
    public int Swoole\Server::getLastError ( void )
    public mixed Swoole\Server::heartbeat ( boolean $if_close_connection )
    public boolean Swoole\Server::listen ( string $host , int $port , string $socket_type )
    public void Swoole\Server::on ( string $event_name , callable $callback )
    public void Swoole\Server::pause ( int $fd )
    public void Swoole\Server::protect ( int $fd [, boolean $is_protected ] )
    public boolean Swoole\Server::reload ( void )
    public void Swoole\Server::resume ( int $fd )
    public boolean Swoole\Server::send ( int $fd , string $data [, int $reactor_id ] )
    public boolean Swoole\Server::sendfile ( int $fd , string $filename [, int $offset ] )
    public boolean Swoole\Server::sendMessage ( int $worker_id , string $data )
    public boolean Swoole\Server::sendto ( string $ip , int $port , string $data [, string $server_socket ] )
    public boolean Swoole\Server::sendwait ( int $fd , string $data )
    public mixed Swoole\Server::set ( array $settings )
    public void Swoole\Server::shutdown ( void )
    public void Swoole\Server::start ( void )
    public array Swoole\Server::stats ( void )
    public boolean Swoole\Server::stop ([ int $worker_id ] )
    public mixed Swoole\Server::task ( string $data [, int $dst_worker_id [, callable $callback ]] )
    public void Swoole\Server::taskwait ( string $data [, float $timeout [, int $worker_id ]] )
    public void Swoole\Server::taskWaitMulti ( array $tasks [, double $timeout_ms ] )
    public void Swoole\Server::tick ( int $interval_ms , callable $callback )

Alias: swoole_http_server

The swoole_http_server class inherits from the class swoole_server. It is a complete implementation of http server. It supports asynchronous and synchronous mode.

The swoole http server can hold a mass of connections in asynchronous or synchronous mode.

Swoole\Http\Server::on(string $event, callable $callback)

Alias: swoole_http_server->on(string $event, callable $callback)

Swoole\Http\Server->on(string $event, callable $callback)

Register the callback function for the event.

The Swoole\Http\Server class inherits from the class swoole_server. The on method has something different from the on method of class swoole_server.

  • Don't support the event connect and receive

  • Add new event request. This event happens when the process receives a complete http request.

$server->on('request', function(Swoole\Http\Request $request, Swoole\Http\Response $response) {
     $response->end("<h1>hello Swoole</h1>");

When the process receives a complete http request, it will call the callback function registered for this event.

  • $request the object of class Swoole\Http\Request

  • $response the object of class Swoole\Http\Response

  • If the callback function hasn't called the $response->end function, the swoole will add the call of $response->end("") at the end automatically.


Alias: swoole_http_server->start()


Start the swoole http server


Alias: swoole_http_request

The object of swoole_http_request class contains the information of request, for example, GET, POST, etc.


Alias: swoole_http_request->header

The Swoole\Http\Request->header is an array which contains the information of http request.

All the keys of this array is lowercase.

echo $request->header['host'];
echo $request->header['accept-language'];

Alias: swoole_http_request->server


Alias: swoole_http_request->get


Alias: swoole_http_request->post

Alias: swoole_http_request->cookie


Alias: swoole_http_request->rawcookie


Alias: swoole_http_request->files


Alias: swoole_http_request->rawContent

string Swoole\Http\Request->getContent();

Alias: string Swoole\Http\Request->rawContent();

Get the raw POST body. This method is used for the POST data which isn't in form of application/x-www-form-urlencoded.


Return the raw post data.

You can set the configuration http_parse_post to control the analysis of POST data.

string Swoole\Http\Request->getData();

Get the raw HTTP request data.


Alias: swoole_http_response


Alias: swoole_http_response->header

Swoole\Http\Response->header(string $key, string $value)

Set the header of http response.

  • $key the key of header

  • $value the value of header

$responser->header('Content-Type', 'image/jpeg');

This method must be called before the end method

Alias: swoole_http_response->cookie

Swoole\Http\Response->cookie(string $key, string $value = '', int $expire = 0 , string $path = '/', string $domain  = '', bool $secure = false , bool $httponly = false);

Amount to setcookie of PHP.

This method must be called before the end method


Alias: swoole_http_response->status

Swoole\Http\Response->status(int $http_status_code);

Set the http response status.

This method must be called before the end method


Alias: swoole_http_response->gzip

Swoole\Http\Response->gzip(int $level = 1);

Enable the gzip of response content. The header about Content-Encoding will be added automatically.

  • $level the level of compression

This method must be called before the end method

This method needs zlib. Install zlib by sudo apt-get install libz-dev


Alias: swoole_http_response->write

bool Swoole\Http\Response->write(string $data);

Enable chunk feature of Http to send data to client. Check the reference material about chunk feature of Http.

  • $data the max length of data is 2M

After this method has been called, the call of $response->end can't be passed any parameter.

After the call of $response->end(), it would send a chunk of length 0 to end data transmission.


Alias: swoole_http_response->sendfile

Swoole\Http\Response->sendfile(string $filename, int $offset = 0, int $length = 0);

Response the file content to the client.

  • $filename the file path to send. If there is no this file, the sendfile will fail.

  • $offset the start offset of file to send.

  • $length the length of data to send. The default value is the whole length of file.

Before the call of this method, it has to set content type by $response->header().

Before the call of this method, it must not call $response->write

After the call of this method, it will call $response->end() automatically.

$response->sendfile doesn't support gzip compression.

$response->header('Content-Type', 'image/jpeg');

Alias: swoole_http_response->end

Swoole\Http\Response->end(string $html)

Send data for the HTTP request and end the response.

  • $html the data to send to the client

The call of $response->end can only be once.

If the client enables the configuration of keepalive, the connection between the server and the client will maintain.

If the client doesn't enable the configuration of keepalive, the connection between the server and the client will be closed.


Send the trailer header.

Swoole\Http\Response->trailer(string $key, string $value = '')


HTTP/1.1 200 OK 
Content-Type: text/plain 
Transfer-Encoding: chunked
Trailer: Expires

Expires: Wed, 21 Oct 2015 07:28:00 GMT\r\n

Detach the http response object with current context. Construct and send back the response within another context such as another Task Worker.


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

$server->set(['task_worker_num' => 1, 'worker_num' => 1]);

$server->on('request', function (Swoole\Http\Request $request, Swoole\Http\Response $response) use ($server) {

$server->on('finish', function () {
    echo "task finish";

$server->on('task', function ($serv, $task_id, $worker_id, $data) {
    $resp = Swoole\Http\Response::create($data);
    echo "async task\n";

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

$server->on('request', function ($req, Swoole\Http\Response $response) use ($server) {
    $server->send($response->fd, "HTTP/1.1 200 OK\r\nServer: SWOOLE\r\n\r\nHello World\n");