🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
[TOC] ## **Request** ``` // $connection TcpConnection对象 // $session Session 实例 // $properties // ::$maxFileUploads 上传文件最大size,默认1024 // __construct($buffer) // get($name = null, $default = null) // post($name = null, $default = null) // header($name = null, $default = null) // cookie($name = null, $default = null) // file($name = null) // method() // protocolVersion() // host($without_port = false) // uri() // path() // queryString() // session() // sessionId($session_id = null) // rawHead() // rawBody() // rawBuffer() // ::enableCache($value) 开启关闭缓存 // __set($name, $value) // __get($name) // __isset($name) // __unset($name) // __toString() // __wakeup() // __destruct() ``` ``` //use Workerman\Worker; use Workerman\Connection\TcpConnection; use Workerman\Protocols\Http\Request; $worker = new Worker('http://0.0.0.0:8080'); $worker->onMessage = function(TcpConnection $connection, Request $request) { $get = $request->get(); $get_user = $request->get("user",'tom'); // $request为请求对象 $connection->send("hello " . $get_user); }; // 运行worker Worker::runAll(); ``` ## **get()** ``` $get_params = $request->get(); $get_pwd = $request->get("pwd"); //可以给get参数一个默认值 $get_pwd = $request->get("pwd",'admin123456'); ``` ## **post()** ``` $post_params = $request->post(); $post_pwd = $request->post("pwd"); //可以给post参数一个默认值 $post_pwd = $request->post("pwd",'admin123456'); ``` ## **rawBody()** 获得原始请求post包体 这个功能类似与`php-fpm`里的`file_get_contents("php://input");`操作。用于获得http原始请求包体。这在获取非`application/x-www-form-urlencoded`格式的post请求数据时很有用 ``` $post = $request->rawBody(); ``` 示例: ``` 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 ``` // 获取整个header数组 $header_params= $request->header(); // 获取header数组的某一个值 $header= $request->header('host'); //可以给header参数一个默认值 $header= $request->header('host', 'localhost'); ``` ## **cookie()** 获取cookie ``` // 获取整个cookie数组 $cookie_params= $request->cookie(); // 获取cookie数组的某一个值 $cookie= $request->cookie('name'); //可以给cookiet参数一个默认值 $cookie= $request->cookie('name', 'tom'); ``` ## **file()** 获取上传文件 返回的文件格式类似: ~~~php array ( 'avatar' => array ( 'name' => '123.jpg',// name为文件名字 'tmp_name' => '/tmp/workerman.upload.9hjR4w',// tmp_name为磁盘临时文件位置 'size' => 1196127,// size为文件大小 'error' => 0, // error为错误码 'type' => 'application/octet-stream', // type为文件mine类型 ), 'anotherfile' => array ( 'name' => '456.txt', 'tmp_name' => '/tmp/workerman.upload.9sirSws', 'size' => 490, 'error' => 0, 'type' => 'text/plain', ) ) ~~~ error[错误码](https://www.php.net/manual/zh/features.file-upload.errors.php) >[danger]**注意:** >* 上传文件大小受到[defaultMaxPackageSize](https://www.workerman.net/doc/workerman/tcp-connection/default-max-package-size.html)限制,默认10M,可修改。 >* 请求结束后文件将被自动清除。 >* 如果请求没有上传文件则返回一个空的数组。 ### 获取特定上传文件 ~~~php $avatar_file = $request->file('avatar'); ~~~ 返回类似 ~~~php array ( 'name' => '123.jpg', 'tmp_name' => '/tmp/workerman.upload.9hjR4w', 'size' => 1196127, 'error' => 0, 'type' => 'application/octet-stream', ) ~~~ 如果上传文件不存在则返回null。 **例子** ~~~php 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信息 $host = $request->host(); //不返回host信息携带端口 $host = $request->host(true); ``` ## **method()** 获取请求方法 返回值可能是GET、POST、PUT、DELETE、OPTIONS、HEAD中的一个。 ``` $method = $request->method(); ``` ## **uri()** 获取请求uri (如:/user/get.php?uid=10&type=2) 返回请求的uri,包括path和queryString部分。 ``` //当浏览器访问`http://127.0.0.1:8080/user/get.php?uid=10&type=2`时将返回`/user/get.php?uid=10&type=2` $uri = $request->uri(); ``` ## **path()** 获取请求路径(如:/user/get.php) 返回请求的path部分 ``` //当浏览器访问`http://127.0.0.1:8080/user/get.php?uid=10&type=2`时将返回`/user/get.php` $path = $request->path(); ``` ## **queryString()** 获取请求queryString(如:uid=10&type=2) ``` //当浏览器访问`http://127.0.0.1:8080/user/get.php?uid=10&type=2`时将返回`uid=10&type=2` $query_string = $request->queryString(); ``` ## **protocolVersion()** 获取请求HTTP版本(返回字符串 1.1 或者1.0) ``` $version = $request->protocolVersion(); ``` ## **sessionId()** 获取请求sessionId 返回字符串,由字母和数字组成 ``` $sid = $request->sessionId(); ``` ## **session()** 返回\Workerman\Protocols\Http\Session对象 ## **rawHead()** 获取http原始头 ## **rawBuffer()** 获取http原始缓冲区 ## **::enableCache($value) 开启关闭请求缓存 默认true** 传入的参数最终回强制转换为bool,如:static::$_enableCache = (bool)$value; ``` Request::enableCache(1) Request::enableCache(true) ``` ## **::$maxFileUploads** 上传文件最大size,默认1024