Swoole HTTP server

<?php
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 , integer $port , string $socket_type )
    public boolean Swoole\Server::addProcess ( swoole_process $process )
    public ReturnType Swoole\Server::after ( integer $after_time_ms , callable $callback [, string $param ] )
    public boolean Swoole\Server::bind ( integer $fd , integer $uid )
    public void Swoole\Server::clearTimer ( integer $timer_id )
    public void swoole_timer_clear ( integer $timer_id )
    public boolean Swoole\Server::close ( integer $fd [, boolean $reset ] )
    public boolean Swoole\Server::confirm ( integer $fd )
    public array Swoole\Server::connection_info ( integer $fd [, integer $reactor_id ] )
    public array Swoole\Server::connection_list ( integer $start_fd [, integer $pagesize ] )
    public void Swoole\Server::defer ( callable $callback )
    public void Swoole\Server\Port::__destruct ( void )
    public ReturnType Swoole\Server\Port::on ( string $event_name , callable $callback )
    public void Swoole\Server\Port::set ( array $settings )
    public boolean Swoole\Server::exist ( integer $fd )
    public void Swoole\Server::finish ( string $data )
    public ReturnType Swoole\Server::getClientInfo ( integer $fd [, integer $reactor_id ] )
    public array Swoole\Server::getClientList ( integer $start_fd [, integer $pagesize ] )
    public integer Swoole\Server::getLastError ( void )
    public mixed Swoole\Server::heartbeat ( boolean $if_close_connection )
    public boolean Swoole\Server::listen ( string $host , integer $port , string $socket_type )
    public void Swoole\Server::on ( string $event_name , callable $callback )
    public void Swoole\Server::pause ( integer $fd )
    public void Swoole\Server::protect ( integer $fd [, boolean $is_protected ] )
    public boolean Swoole\Server::reload ( void )
    public void Swoole\Server::resume ( integer $fd )
    public boolean Swoole\Server::send ( integer $fd , string $data [, integer $reactor_id ] )
    public boolean Swoole\Server::sendfile ( integer $fd , string $filename [, integer $offset ] )
    public boolean Swoole\Server::sendMessage ( integer $worker_id , string $data )
    public boolean Swoole\Server::sendto ( string $ip , integer $port , string $data [, string $server_socket ] )
    public boolean Swoole\Server::sendwait ( integer $fd , string $data )
    public ReturnType 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 ([ integer $worker_id ] )
    public mixed Swoole\Server::task ( string $data [, integer $dst_worker_id [, callable $callback ]] )
    public void Swoole\Server::taskwait ( string $data [, float $timeout [, integer $worker_id ]] )
    public void Swoole\Server::taskWaitMulti ( array $tasks [, double $timeout_ms ] )
    public void Swoole\Server::tick ( integer $interval_ms , callable $callback )
}

Swoole\Http\Server

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.

Example

$http_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.

Swoole\Http\Server::start()

Alias: swoole_http_server->start()

<?php
swoole_http_server->start()

Start the swoole http server

Swoole\Http\Request

Alias: swoole_http_request

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

Swoole\Http\Request::header

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.

Example
<?php
echo $request->header['host'];
echo $request->header['accept-language'];

Swoole\Http\Request::server

Alias: swoole_http_request->server

Swoole\Http\Request::get

Alias: swoole_http_request->get

Swoole\Http\Request::post

Alias: swoole_http_request->post

Alias: swoole_http_request->cookie

Swoole\Http\Request::files

Alias: swoole_http_request->files

Swoole\Http\Request::rawContent

Alias: swoole_http_request->rawContent

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

Return the raw post data.

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

Swoole\Http\Response

Alias: swoole_http_response

Swoole\Http\Response::header

Alias: swoole_http_response->header

swoole_http_response->header(string $key, string $value)

Set the header of http response.

Parameter

Example

$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

Swoole\Http\Response::status

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

Swoole\Http\Response::gzip

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.

Parameter

This method must be called before the end method

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

Swoole\Http\Response::write

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.

Parameter

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.

Swoole\Http\Response::sendfile

Alias: swoole_http_response->sendfile

swoole_http_response->sendfile(string $filename, int $offset = 0, int $length = 0);

Response the file content to the client.

Parameter

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.

Example

$response->header('Content-Type', 'image/jpeg');
$response->sendfile(__DIR__.$request->server['request_uri']);

Swoole\Http\Response::end

Alias: swoole_http_response->end

swoole_http_response->end(string $html)

Send data for the HTTP request and end the response.

Parameter

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.