说明
workerman 从 4.x 版本开始加强了 HTTP 服务的支持。引入了请求类、响应类、session 类以及 SSE。如果你想使用 workerman 的 HTTP 服务,强烈推荐使用 workerman4.x 或者以后的更高版本。
注意以下都是 workerman4.x 版本的用法,不兼容 workerman3.x。
获得请求对象
请求对象一律在 onMessage 回调函数中获取,框架会自动将 Request 对象通过回调函数第二个参数传递进来。
例子
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为请求对象,这里没有对请求对象执行任何操作直接返回hello给浏览器
$connection->send("hello");
};
// 运行worker
Worker::runAll();
当浏览器访问 http://127.0.0.1:8080
时将返回 hello
。
获得请求参数 get
获取整个 get 数组
$get = $request->get();
如果请求没有 get 参数则返回一个空的数组。
获取 get 数组的某一个值
$name = $request->get('name');
如果 get 数组中不包含这个值则返回 null。
你也可以给 get 方法第二个参数传递一个默认值,如果 get 数组中没找到对应值则返回默认值。例如:
$name = $request->get('name', 'tom');
例子
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'));
};
// 运行worker
Worker::runAll();
当浏览器访问 http://127.0.0.1:8080?name=jerry&age=12
时将返回 jerry
。
获得请求参数 post
获取整个 post 数组
$post = $request->post();
如果请求没有 post 参数则返回一个空的数组。
获取 post 数组的某一个值
$name = $request->post('name');
如果 post 数组中不包含这个值则返回 null。
与 get 方法一样,你也可以给 post 方法第二个参数传递一个默认值,如果 post 数组中没找到对应值则返回默认值。例如:
$name = $request->post('name', 'tom');
例子
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));
};
// 运行worker
Worker::runAll();
获得原始请求 post 包体
$post = $request->rawBody();
这个功能类似与 php-fpm
里的 file_get_contents("php://input");
操作。用于获得 http 原始请求包体。这在获取非 application/x-www-form-urlencoded
格式的 post 请求数据时很有用。
例子
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');
};
// 运行worker
Worker::runAll();
获取 header
获取整个 header 数组
$headers = $request->header();
如果请求没有 header 参数则返回一个空的数组。注意所有 key 均为小写。
获取 header 数组的某一个值
$host = $request->header('host');
如果 header 数组中不包含这个值则返回 null。注意所有 key 均为小写。
与 get 方法一样,你也可以给 header 方法第二个参数传递一个默认值,如果 header 数组中没找到对应值则返回默认值。例如:
$host = $request->header('host', 'localhost');
例子
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');
}
};
// 运行worker
Worker::runAll();
获取 cookie
获取整个 cookie 数组
$cookies = $request->cookie();
如果请求没有 cookie 参数则返回一个空的数组。
获取 cookie 数组的某一个值
$name = $request->cookie('name');
如果 cookie 数组中不包含这个值则返回 null。
与 get 方法一样,你也可以给 cookie 方法第二个参数传递一个默认值,如果 cookie 数组中没找到对应值则返回默认值。例如:
$name = $request->cookie('name', 'tom');
例子
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));
};
// 运行worker
Worker::runAll();
获取上传文件
获取整个上传文件数组
$files = $request->file();
返回的文件格式类似:
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',
)
)
其中:
- name 为文件名字
- tmp_name 为磁盘临时文件位置
- size 为文件大小
- error 为错误码
- type 为文件 mine 类型。
注意:
上传文件大小受到 defaultMaxPackageSize 限制,默认 10M,可修改。
请求结束后文件将被自动清除。
如果请求没有上传文件则返回一个空的数组。
获取特定上传文件
$avatar_file = $request->file('avatar');
返回类似
array (
'name' => '123.jpg',
'tmp_name' => '/tmp/workerman.upload.9hjR4w',
'size' => 1196127,
'error' => 0,
'type' => 'application/octet-stream',
)
如果上传文件不存在则返回 null。
例子
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');
};
// 运行worker
Worker::runAll();
获取 host
获取请求的 host 信息。
$host = $request->host();
如果请求的地址是非标准的 80 或者 443 端口,host 信息可能会携带端口,例如 example.com:8080
。如果不需要端口第一个参数可以传入 true
。
$host = $request->host(true);
例子
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());
};
// 运行worker
Worker::runAll();
当浏览器访问 http://127.0.0.1:8080?name=tom
时将返回 127.0.0.1:8080
。
获取请求方法
$method = $request->method();
返回值可能是 GET
、 POST
、 PUT
、 DELETE
、 OPTIONS
、 HEAD
中的一个。
例子
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());
};
// 运行worker
Worker::runAll();
获取请求 uri
$uri = $request->uri();
返回请求的 uri,包括 path 和 queryString 部分。
例子
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());
};
// 运行worker
Worker::runAll();
当浏览器访问 http://127.0.0.1:8080/user/get.php?uid=10&type=2
时将返回 /user/get.php?uid=10&type=2
。
获取请求路径
$path = $request->path();
返回请求的 path 部分。
例子
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());
};
// 运行worker
Worker::runAll();
当浏览器访问 http://127.0.0.1:8080/user/get.php?uid=10&type=2
时将返回 /user/get.php
。
获取请求 queryString
$query_string = $request->queryString();
返回请求的 queryString 部分。
例子
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());
};
// 运行worker
Worker::runAll();
当浏览器访问 http://127.0.0.1:8080/user/get.php?uid=10&type=2
时将返回 uid=10&type=2
。
获取请求 HTTP 版本
$version = $request->protocolVersion();
返回字符串 1.1
或者 1.0
。
例子
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());
};
// 运行worker
Worker::runAll();
获取请求 sessionId
$sid = $request->sessionId();
返回字符串,由字母和数字组成
例子
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());
};
// 运行worker
Worker::runAll();