Description
Workerman has strengthened its support for HTTP services starting from version 4.x. It introduces the Request class, Response class, Session class, and SSE. If you want to use Workerman's HTTP services, it is highly recommended to use Workerman 4.x or higher.
Note that the following are all usages for Workerman 4.x, and are not compatible with Workerman 3.x.
Getting the Request Object
The request object is obtained in the onMessage callback function, where the framework automatically passes the Request object as the second parameter of the callback function.
Example
use Workerman\Worker;
use Workerman\Connection\TcpConnection;
use Workerman\Protocols\Http\Request;
require_once __DIR__ . '/vendor/autoload.php';
$worker = new Worker('http://0.0.0.0:8080');
$worker->onMessage = function(TcpConnection $connection, Request $request)
{
// $request is the request object, here we directly return hello to the browser without any operations on the request object
$connection->send("hello");
};
// Run worker
Worker::runAll();
When the browser accesses http://127.0.0.1:8080, it will return hello.
Getting GET Request Parameters
Get the entire GET array
$get = $request->get();
If there are no GET parameters, it will return an empty array.
Get a specific value from the GET array
$name = $request->get('name');
If the value is not found in the GET array, it will return null.
You can also pass a default value as the second parameter to the get method. If a corresponding value is not found in the GET array, the default value will be returned. For example:
$name = $request->get('name', 'tom');
Example
use Workerman\Worker;
use Workerman\Connection\TcpConnection;
use Workerman\Protocols\Http\Request;
require_once __DIR__ . '/vendor/autoload.php';
$worker = new Worker('http://0.0.0.0:8080');
$worker->onMessage = function(TcpConnection $connection, Request $request)
{
$connection->send($request->get('name'));
};
// Run worker
Worker::runAll();
When the browser accesses http://127.0.0.1:8080?name=jerry&age=12, it will return jerry.
Getting POST Request Parameters
Get the entire POST array
$post = $request->post();
If there are no POST parameters, it will return an empty array.
Get a specific value from the POST array
$name = $request->post('name');
If the value is not found in the POST array, it will return null.
Similar to the get method, you can also pass a default value as the second parameter to the post method. If a corresponding value is not found in the POST array, the default value will be returned. For example:
$name = $request->post('name', 'tom');
Example
use Workerman\Worker;
use Workerman\Connection\TcpConnection;
use Workerman\Protocols\Http\Request;
require_once __DIR__ . '/vendor/autoload.php';
$worker = new Worker('http://0.0.0.0:8080');
$worker->onMessage = function(TcpConnection $connection, Request $request)
{
$post = $request->post();
$connection->send(var_export($post, true));
};
// Run worker
Worker::runAll();
Getting the Raw POST Body
$post = $request->rawBody();
This function is similar to the file_get_contents("php://input") operation in php-fpm. It is used to obtain the raw HTTP request body. This is very useful for obtaining POST request data in formats other than application/x-www-form-urlencoded.
Example
use Workerman\Worker;
use Workerman\Connection\TcpConnection;
use Workerman\Protocols\Http\Request;
require_once __DIR__ . '/vendor/autoload.php';
$worker = new Worker('http://0.0.0.0:8080');
$worker->onMessage = function(TcpConnection $connection, Request $request)
{
$post = json_decode($request->rawBody());
$connection->send('hello');
};
// Run worker
Worker::runAll();
Getting Headers
Get the entire header array
$headers = $request->header();
If there are no header parameters, it will return an empty array. Note that all keys are lowercase.
Get a specific value from the header array
$host = $request->header('host');
If the value is not found in the header array, it will return null. Note that all keys are lowercase.
Similar to the get method, you can also pass a default value as the second parameter to the header method. If a corresponding value is not found in the header array, the default value will be returned. For example:
$host = $request->header('host', 'localhost');
Example
use Workerman\Worker;
use Workerman\Connection\TcpConnection;
use Workerman\Protocols\Http\Request;
require_once __DIR__ . '/vendor/autoload.php';
$worker = new Worker('http://0.0.0.0:8080');
$worker->onMessage = function(TcpConnection $connection, Request $request)
{
if ($request->header('connection') === 'keep-alive') {
$connection->send('hello');
} else {
$connection->close('hello');
}
};
// Run worker
Worker::runAll();
Getting Cookies
Get the entire cookie array
$cookies = $request->cookie();
If there are no cookie parameters, it will return an empty array.
Get a specific value from the cookie array
$name = $request->cookie('name');
If the value is not found in the cookie array, it will return null.
Similar to the get method, you can also pass a default value as the second parameter to the cookie method. If a corresponding value is not found in the cookie array, the default value will be returned. For example:
$name = $request->cookie('name', 'tom');
Example
use Workerman\Worker;
use Workerman\Connection\TcpConnection;
use Workerman\Protocols\Http\Request;
require_once __DIR__ . '/vendor/autoload.php';
$worker = new Worker('http://0.0.0.0:8080');
$worker->onMessage = function(TcpConnection $connection, Request $request)
{
$cookie = $request->cookie();
$connection->send(var_export($cookie, true));
};
// Run worker
Worker::runAll();
Getting Uploaded Files
Get the entire uploaded files array
$files = $request->file();
The returned file format is similar to:
array (
'avatar' => array (
'name' => '123.jpg',
'tmp_name' => '/tmp/workerman.upload.9hjR4w',
'size' => 1196127,
'error' => 0,
'type' => 'application/octet-stream',
),
'anotherfile' => array (
'name' => '456.txt',
'tmp_name' => '/tmp/workerman.upload.9sirSws',
'size' => 490,
'error' => 0,
'type' => 'text/plain',
)
)
Where:
- name is the file name
- tmp_name is the temporary file location on disk
- size is the file size
- error is the error code
- type is the file mime type.
Note:
-
The size of uploaded files is limited by the defaultMaxPackageSize, which defaults to 10M but can be modified.
-
Files will be automatically cleared after the request is finished.
-
If there are no uploaded files in the request, an empty array will be returned.
Getting a Specific Uploaded File
$avatar_file = $request->file('avatar');
Returns something like
array (
'name' => '123.jpg',
'tmp_name' => '/tmp/workerman.upload.9hjR4w',
'size' => 1196127,
'error' => 0,
'type' => 'application/octet-stream',
)
If the uploaded file does not exist, it will return null.
Example
use Workerman\Worker;
use Workerman\Connection\TcpConnection;
use Workerman\Protocols\Http\Request;
require_once __DIR__ . '/vendor/autoload.php';
$worker = new Worker('http://0.0.0.0:8080');
$worker->onMessage = function(TcpConnection $connection, Request $request)
{
$file = $request->file('avatar');
if ($file && $file['error'] === UPLOAD_ERR_OK) {
rename($file['tmp_name'], '/home/www/web/public/123.jpg');
$connection->send('ok');
return;
}
$connection->send('upload fail');
};
// Run worker
Worker::runAll();
Getting the Host
Get the host information of the request.
$host = $request->host();
If the request's address is on a non-standard port of 80 or 443, the host information may include the port, for example, example.com:8080. If the port is not needed, you can pass true as the first parameter.
$host = $request->host(true);
Example
use Workerman\Worker;
use Workerman\Connection\TcpConnection;
use Workerman\Protocols\Http\Request;
require_once __DIR__ . '/vendor/autoload.php';
$worker = new Worker('http://0.0.0.0:8080');
$worker->onMessage = function(TcpConnection $connection, Request $request)
{
$connection->send($request->host());
};
// Run worker
Worker::runAll();
When the browser accesses http://127.0.0.1:8080?name=tom, it will return 127.0.0.1:8080.
Getting the Request Method
$method = $request->method();
The return value can be GET, POST, PUT, DELETE, OPTIONS, or HEAD.
Example
use Workerman\Worker;
use Workerman\Connection\TcpConnection;
use Workerman\Protocols\Http\Request;
require_once __DIR__ . '/vendor/autoload.php';
$worker = new Worker('http://0.0.0.0:8080');
$worker->onMessage = function(TcpConnection $connection, Request $request)
{
$connection->send($request->method());
};
// Run worker
Worker::runAll();
Getting the Request URI
$uri = $request->uri();
Returns the URI of the request, including the path and queryString parts.
Example
use Workerman\Worker;
use Workerman\Connection\TcpConnection;
use Workerman\Protocols\Http\Request;
require_once __DIR__ . '/vendor/autoload.php';
$worker = new Worker('http://0.0.0.0:8080');
$worker->onMessage = function(TcpConnection $connection, Request $request)
{
$connection->send($request->uri());
};
// Run worker
Worker::runAll();
When the browser accesses http://127.0.0.1:8080/user/get.php?uid=10&type=2, it will return /user/get.php?uid=10&type=2.
Getting the Request Path
$path = $request->path();
Returns the path part of the request.
Example
use Workerman\Worker;
use Workerman\Connection\TcpConnection;
use Workerman\Protocols\Http\Request;
require_once __DIR__ . '/vendor/autoload.php';
$worker = new Worker('http://0.0.0.0:8080');
$worker->onMessage = function(TcpConnection $connection, Request $request)
{
$connection->send($request->path());
};
// Run worker
Worker::runAll();
When the browser accesses http://127.0.0.1:8080/user/get.php?uid=10&type=2, it will return /user/get.php.
Getting the Request Query String
$query_string = $request->queryString();
Returns the query string part of the request.
Example
use Workerman\Worker;
use Workerman\Connection\TcpConnection;
use Workerman\Protocols\Http\Request;
require_once __DIR__ . '/vendor/autoload.php';
$worker = new Worker('http://0.0.0.0:8080');
$worker->onMessage = function(TcpConnection $connection, Request $request)
{
$connection->send($request->queryString());
};
// Run worker
Worker::runAll();
When the browser accesses http://127.0.0.1:8080/user/get.php?uid=10&type=2, it will return uid=10&type=2.
Getting the Request HTTP Version
$version = $request->protocolVersion();
Returns the string 1.1 or 1.0.
Example
use Workerman\Worker;
use Workerman\Connection\TcpConnection;
use Workerman\Protocols\Http\Request;
require_once __DIR__ . '/vendor/autoload.php';
$worker = new Worker('http://0.0.0.0:8080');
$worker->onMessage = function(TcpConnection $connection, Request $request)
{
$connection->send($request->protocolVersion());
};
// Run worker
Worker::runAll();
Getting the Request Session ID
$sid = $request->sessionId();
Returns a string consisting of letters and numbers.
Example
use Workerman\Worker;
use Workerman\Connection\TcpConnection;
use Workerman\Protocols\Http\Request;
require_once __DIR__ . '/vendor/autoload.php';
$worker = new Worker('http://0.0.0.0:8080');
$worker->onMessage = function(TcpConnection $connection, Request $request)
{
$connection->send($request->sessionId());
};
// Run worker
Worker::runAll();