说明

workerman从4.x版本开始加强了HTTP服务的支持。引入了请求类、响应类、session类以及SSE。如果你想使用workerman的HTTP服务,强烈推荐使用workerman4.x或者以后的更高版本。

注意以下都是workerman4.x版本的用法,不兼容workerman3.x。

获得请求对象

请求对象一律在onMessage回调函数中获取,框架会自动将Request对象通过回调函数第二个参数传递进来。

例子

  1. use Workerman\Worker;
  2. use Workerman\Connection\TcpConnection;
  3. use Workerman\Protocols\Http\Request;
  4. require_once __DIR__ . '/vendor/autoload.php';
  5. $worker = new Worker('http://0.0.0.0:8080');
  6. $worker->onMessage = function(TcpConnection $connection, Request $request)
  7. {
  8. // $request为请求对象,这里没有对请求对象执行任何操作直接返回hello给浏览器
  9. $connection->send("hello");
  10. };
  11. // 运行worker
  12. Worker::runAll();

当浏览器访问http://127.0.0.1:8080时将返回hello

获得请求参数get

获取整个get数组

  1. $get = $request->get();

如果请求没有get参数则返回一个空的数组。

获取get数组的某一个值

  1. $name = $request->get('name');

如果get数组中不包含这个值则返回null。

你也可以给get方法第二个参数传递一个默认值,如果get数组中没找到对应值则返回默认值。例如:

  1. $name = $request->get('name', 'tom');

例子

  1. use Workerman\Worker;
  2. use Workerman\Connection\TcpConnection;
  3. use Workerman\Protocols\Http\Request;
  4. require_once __DIR__ . '/vendor/autoload.php';
  5. $worker = new Worker('http://0.0.0.0:8080');
  6. $worker->onMessage = function(TcpConnection $connection, Request $request)
  7. {
  8. $connection->send($request->get('name'));
  9. };
  10. // 运行worker
  11. Worker::runAll();

当浏览器访问http://127.0.0.1:8080?name=jerry&age=12时将返回jerry

获得请求参数post

获取整个post数组

  1. $post = $request->post();

如果请求没有post参数则返回一个空的数组。

获取post数组的某一个值

  1. $name = $request->post('name');

如果post数组中不包含这个值则返回null。

与get方法一样,你也可以给post方法第二个参数传递一个默认值,如果post数组中没找到对应值则返回默认值。例如:

  1. $name = $request->post('name', 'tom');

例子

  1. use Workerman\Worker;
  2. use Workerman\Connection\TcpConnection;
  3. use Workerman\Protocols\Http\Request;
  4. require_once __DIR__ . '/vendor/autoload.php';
  5. $worker = new Worker('http://0.0.0.0:8080');
  6. $worker->onMessage = function(TcpConnection $connection, Request $request)
  7. {
  8. $post = $request->post();
  9. $connection->send(var_export($post, true));
  10. };
  11. // 运行worker
  12. Worker::runAll();

获得原始请求post包体

  1. $post = $request->rawBody();

这个功能类似与 php-fpm里的 file_get_contents("php://input");操作。用于获得http原始请求包体。这在获取非application/x-www-form-urlencoded格式的post请求数据时很有用。

例子

  1. use Workerman\Worker;
  2. use Workerman\Connection\TcpConnection;
  3. use Workerman\Protocols\Http\Request;
  4. require_once __DIR__ . '/vendor/autoload.php';
  5. $worker = new Worker('http://0.0.0.0:8080');
  6. $worker->onMessage = function(TcpConnection $connection, Request $request)
  7. {
  8. $post = json_decode($request->rawBody());
  9. $connection->send('hello');
  10. };
  11. // 运行worker
  12. Worker::runAll();

获取header

获取整个header数组

  1. $headers = $request->header();

如果请求没有header参数则返回一个空的数组。注意所有key均为小写。

获取header数组的某一个值

  1. $host = $request->header('host');

如果header数组中不包含这个值则返回null。注意所有key均为小写。

与get方法一样,你也可以给header方法第二个参数传递一个默认值,如果header数组中没找到对应值则返回默认值。例如:

  1. $host = $request->header('host', 'localhost');

例子

  1. use Workerman\Worker;
  2. use Workerman\Connection\TcpConnection;
  3. use Workerman\Protocols\Http\Request;
  4. require_once __DIR__ . '/vendor/autoload.php';
  5. $worker = new Worker('http://0.0.0.0:8080');
  6. $worker->onMessage = function(TcpConnection $connection, Request $request)
  7. {
  8. if ($request->header('connection') === 'keep-alive') {
  9. $connection->send('hello');
  10. } else {
  11. $connection->close('hello');
  12. }
  13. };
  14. // 运行worker
  15. Worker::runAll();

获取cookie

获取整个cookie数组

  1. $cookies = $request->cookie();

如果请求没有cookie参数则返回一个空的数组。

获取cookie数组的某一个值

  1. $name = $request->cookie('name');

如果cookie数组中不包含这个值则返回null。

与get方法一样,你也可以给cookie方法第二个参数传递一个默认值,如果cookie数组中没找到对应值则返回默认值。例如:

  1. $name = $request->cookie('name', 'tom');

例子

  1. use Workerman\Worker;
  2. use Workerman\Connection\TcpConnection;
  3. use Workerman\Protocols\Http\Request;
  4. require_once __DIR__ . '/vendor/autoload.php';
  5. $worker = new Worker('http://0.0.0.0:8080');
  6. $worker->onMessage = function(TcpConnection $connection, Request $request)
  7. {
  8. $cookie = $request->cookie();
  9. $connection->send(var_export($cookie, true));
  10. };
  11. // 运行worker
  12. Worker::runAll();

获取上传文件

获取整个上传文件数组

  1. $files = $request->file();

返回的文件格式类似:

  1. array (
  2. 'avatar' => array (
  3. 'name' => '123.jpg',
  4. 'tmp_name' => '/tmp/workerman.upload.9hjR4w',
  5. 'size' => 1196127,
  6. 'error' => 0,
  7. 'type' => 'application/octet-stream',
  8. ),
  9. 'anotherfile' => array (
  10. 'name' => '456.txt',
  11. 'tmp_name' => '/tmp/workerman.upload.9sirSws',
  12. 'size' => 490,
  13. 'error' => 0,
  14. 'type' => 'text/plain',
  15. )
  16. )

其中:

  • name为文件名字
  • tmp_name为磁盘临时文件位置
  • size为文件大小
  • error为错误码
  • type为文件mine类型。

注意:

  • 上传文件大小受到defaultMaxPackageSize限制,默认10M,可修改。

  • 请求结束后文件将被自动清除。

  • 如果请求没有上传文件则返回一个空的数组。

获取特定上传文件

  1. $avatar_file = $request->file('avatar');

返回类似

  1. array (
  2. 'name' => '123.jpg',
  3. 'tmp_name' => '/tmp/workerman.upload.9hjR4w',
  4. 'size' => 1196127,
  5. 'error' => 0,
  6. 'type' => 'application/octet-stream',
  7. )

如果上传文件不存在则返回null。

例子

  1. use Workerman\Worker;
  2. use Workerman\Connection\TcpConnection;
  3. use Workerman\Protocols\Http\Request;
  4. require_once __DIR__ . '/vendor/autoload.php';
  5. $worker = new Worker('http://0.0.0.0:8080');
  6. $worker->onMessage = function(TcpConnection $connection, Request $request)
  7. {
  8. $file = $request->file('avatar');
  9. if ($file && $file['error'] === UPLOAD_ERR_OK) {
  10. rename($file['tmp_name'], '/home/www/web/public/123.jpg');
  11. $connection->send('ok');
  12. return;
  13. }
  14. $connection->send('upload fail');
  15. };
  16. // 运行worker
  17. Worker::runAll();

获取host

获取请求的host信息。

  1. $host = $request->host();

如果请求的地址是非标准的80或者443端口,host信息可能会携带端口,例如example.com:8080。如果不需要端口第一个参数可以传入true

  1. $host = $request->host(true);

例子

  1. use Workerman\Worker;
  2. use Workerman\Connection\TcpConnection;
  3. use Workerman\Protocols\Http\Request;
  4. require_once __DIR__ . '/vendor/autoload.php';
  5. $worker = new Worker('http://0.0.0.0:8080');
  6. $worker->onMessage = function(TcpConnection $connection, Request $request)
  7. {
  8. $connection->send($request->host());
  9. };
  10. // 运行worker
  11. Worker::runAll();

当浏览器访问http://127.0.0.1:8080?name=tom时将返回127.0.0.1:8080

获取请求方法

  1. $method = $request->method();

返回值可能是GETPOSTPUTDELETEOPTIONSHEAD中的一个。

例子

  1. use Workerman\Worker;
  2. use Workerman\Connection\TcpConnection;
  3. use Workerman\Protocols\Http\Request;
  4. require_once __DIR__ . '/vendor/autoload.php';
  5. $worker = new Worker('http://0.0.0.0:8080');
  6. $worker->onMessage = function(TcpConnection $connection, Request $request)
  7. {
  8. $connection->send($request->method());
  9. };
  10. // 运行worker
  11. Worker::runAll();

获取请求uri

  1. $uri = $request->uri();

返回请求的uri,包括path和queryString部分。

例子

  1. use Workerman\Worker;
  2. use Workerman\Connection\TcpConnection;
  3. use Workerman\Protocols\Http\Request;
  4. require_once __DIR__ . '/vendor/autoload.php';
  5. $worker = new Worker('http://0.0.0.0:8080');
  6. $worker->onMessage = function(TcpConnection $connection, Request $request)
  7. {
  8. $connection->send($request->uri());
  9. };
  10. // 运行worker
  11. Worker::runAll();

当浏览器访问http://127.0.0.1:8080/user/get.php?uid=10&type=2时将返回/user/get.php?uid=10&type=2

获取请求路径

  1. $path = $request->path();

返回请求的path部分。

例子

  1. use Workerman\Worker;
  2. use Workerman\Connection\TcpConnection;
  3. use Workerman\Protocols\Http\Request;
  4. require_once __DIR__ . '/vendor/autoload.php';
  5. $worker = new Worker('http://0.0.0.0:8080');
  6. $worker->onMessage = function(TcpConnection $connection, Request $request)
  7. {
  8. $connection->send($request->path());
  9. };
  10. // 运行worker
  11. Worker::runAll();

当浏览器访问http://127.0.0.1:8080/user/get.php?uid=10&type=2时将返回/user/get.php

获取请求queryString

  1. $query_string = $request->queryString();

返回请求的queryString部分。

例子

  1. use Workerman\Worker;
  2. use Workerman\Connection\TcpConnection;
  3. use Workerman\Protocols\Http\Request;
  4. require_once __DIR__ . '/vendor/autoload.php';
  5. $worker = new Worker('http://0.0.0.0:8080');
  6. $worker->onMessage = function(TcpConnection $connection, Request $request)
  7. {
  8. $connection->send($request->queryString());
  9. };
  10. // 运行worker
  11. Worker::runAll();

当浏览器访问http://127.0.0.1:8080/user/get.php?uid=10&type=2时将返回uid=10&type=2

获取请求HTTP版本

  1. $version = $request->protocolVersion();

返回字符串 1.1 或者1.0

例子

  1. use Workerman\Worker;
  2. use Workerman\Connection\TcpConnection;
  3. use Workerman\Protocols\Http\Request;
  4. require_once __DIR__ . '/vendor/autoload.php';
  5. $worker = new Worker('http://0.0.0.0:8080');
  6. $worker->onMessage = function(TcpConnection $connection, Request $request)
  7. {
  8. $connection->send($request->protocolVersion());
  9. };
  10. // 运行worker
  11. Worker::runAll();

获取请求sessionId

  1. $sid = $request->sessionId();

返回字符串,由字母和数字组成

例子

  1. use Workerman\Worker;
  2. use Workerman\Connection\TcpConnection;
  3. use Workerman\Protocols\Http\Request;
  4. require_once __DIR__ . '/vendor/autoload.php';
  5. $worker = new Worker('http://0.0.0.0:8080');
  6. $worker->onMessage = function(TcpConnection $connection, Request $request)
  7. {
  8. $connection->send($request->sessionId());
  9. };
  10. // 运行worker
  11. Worker::runAll();