phpraptor / request
一个轻量级类,用于封装单个对象中的传入请求。
Requires
- php: >=5.6.4
Requires (Dev)
- phpunit/phpunit: ~5.0
This package is not auto-updated.
Last update: 2024-09-29 02:44:32 UTC
README
简介
Raptor Request 是一个使用 SOLID 原则构建的轻量级 PHP 库,用于将传入的 HTTP 请求封装在一个单独的对象中,其结构基于 [RFC7230]。
要安装库,只需运行以下 composer 命令。
composer require phpraptor/request v0.1-beta
要捕获请求,只需创建一个 Http
类的对象。
use Raptor\Request\Http; $request = new Http;
如 [RFC7230] 第 3 节所述,
所有 HTTP/1.1 消息都由一个起始行后跟一系列字节组成,其格式类似于 Internet 消息格式 [RFC5322]:零个或多个头部字段(统称为“头部”或“头部部分”),一个空行表示头部部分的结束,以及一个可选的消息主体。
HTTP-message = start-line *( header-field CRLF ) CRLF [ message-body ]
简单来说,请求消息有三个部分:一个 request-line
(根据 [RFC7230] 第 3.1 节),然后是 header-fields
和 message-body
。
您可以通过在请求对象上调用 line()
、header()
和 body()
方法来访问这三个组件。
$request->line(); // request-line $request->header(); // header fields $request->body(); // message body
请求行
RFC7230 第 3.1.1 节指出,
请求行以一个方法令牌开始,后跟一个空格(SP),请求目标,另一个空格(SP),协议版本,并以 CRLF 结束。
request-line = method SP request-target SP HTTP-version CRLF
这意味着请求行本身被进一步划分为三个子组件。
$request->line()->method(); // method token $request->line()->target(); // request-target $request->line()->version(); // HTTP-version
方法令牌
要获取方法令牌或请求方法,调用 method()
函数。
$request->line()->method(); $request->method(); // short approach // Example output: 'POST'
请求目标
RFC7230 第 5.3 节指出,
根据请求方法和请求是否为代理请求,请求目标的格式有四种。
request-target = origin-form / absolute-form / authority-form / asterisk-form
根据 RFC7230 第 3.1.1 节,
最常见的形式是 origin-form。
origin-form = absolute-path [ "?" query ]
origin-form 有两个子组件:absolute-path
和 query string parameters
。
要获取绝对路径或请求 URI,调用 path()
方法。
$request->line()->target()->path(); // Example output: '/company/about'
要获取所有查询字符串参数,调用 query()
方法。
$request->line()->target()->query(); $request->query(); // short approach // Example output: [ 'search' => 'john doe', 'page' => '2' ]
如果您想获取特定的查询字符串参数,请调用 query()
方法并传递所需的键作为参数。
$request->line()->target()->query('search'); $request->query('search'); // short approach // Example output: 'john doe'
您还可以定义一个默认值,当查询字符串参数中不存在该键时返回。
$request->line()->target()->query('search', 'default value'); $request->query('search', 'default value'); // short approach // Example output: 'default value'
HTTP版本
要获取HTTP版本,请调用 version()
方法。
$request->line()->version(); // Example output: 'HTTP/1.1'
头部字段
要获取所有头部字段或请求头部,请调用请求对象上的 header()
方法的 all()
方法。
$request->header()->all(); // Example output: [ 'HTTP_HOST' => 'localhost', 'HTTP_USER_AGENT' => 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Firefox/52.0', 'HTTP_ACCEPT' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'HTTP_ACCEPT_LANGUAGE' => 'en-US,en;q=0.5', 'HTTP_ACCEPT_ENCODING' => 'gzip, deflate', 'HTTP_COOKIE' => 'PHPSESSID=t8ih495a8fap4agrkk9ectn2r5', 'HTTP_CONNECTION' => 'keep-alive' ]
如果您想获取特定的头部字段,请调用 get()
方法并传递所需的键作为参数。
$request->header()->get('HTTP_HOST'); // Example output: 'localhost'
您还可以定义一个默认值,当头部字段中不存在该键时返回。
$request->header()->get('HTTP_EXAMPLE', 'default value'); // Example output: 'default value'
Cookie ($_COOKIE)
浏览器Cookie会随头部字段一起发送到服务器,因此要获取所有Cookie,请调用请求对象上的 header()
方法的 cookie()
方法。
$request->header()->cookie(); $request->cookie(); // short approach // Example output: [ 'PHPSESSID' => 'lopaavhboml1ua6a539b8u0rm7' ]
如果您想获取特定的Cookie,请调用 cookie()
方法并传递所需的键作为参数。
$request->header()->cookie('PHPSESSID'); $request->cookie('PHPSESSID'); // short approach // Example output: 'lopaavhboml1ua6a539b8u0rm7'
您还可以定义一个默认值,当Cookie中不存在该键时返回。
$request->header()->cookie('FONTSIZE', '14px'); $request->cookie('FONTSIZE', '14px'); // short approach // Example output: '14px'
消息体
RFC7230的3.3节 中指出:
HTTP消息的消息体(如果有的话)用于承载请求的有效负载
有效负载可以包含请求体参数,即 $_POST
,以及任何上传的文件,即 $_FILES
。
请求体参数 ($_POST)
要获取所有请求体参数,请调用 param()
方法。
$request->body()->param(); $request->param(); // short approach // Example output: [ 'username' => 'phpraptor', 'password' => 'johndoe' ]
如果您想获取特定的请求体参数,请调用 param()
方法并传递所需的键作为参数。
$request->body()->param('username'); $request->param('username'); // short approach // Example output: 'phpraptor'
您还可以定义一个默认值,当请求体参数中不存在该键时返回。
$request->body()->param('category_id', 30); $request->param('category_id', 30); // short approach // Example output: 30
上传的文件 ($_FILE)
要获取所有上传的文件,请调用 files()
方法。
$request->body()->files(); $request->uploads(); // short approach // Example output: [ 'image' => [ 'name' => 'image01.jpg', 'type' => 'image/jpeg', 'tmp_name' => '\path\to\tmp\php2660.tmp', 'error' => 0, 'size' => 135069 ] ]
如果您想获取特定的上传文件,请调用 file()
方法并传递所需的键作为参数。
$request->body()->file('image'); $request->file('image'); // short approach // Example output: [ 'name' => 'image01.jpg', 'type' => 'image/jpeg', 'tmp_name' => '\path\to\tmp\php2660.tmp', 'error' => 0, 'size' => 135069 ]
内容长度
要获取消息体的内容长度,请调用 contentLength()
方法。
$request->body()->contentLength(); // Example output: 135467
内容类型
要获取消息体的内容类型,请调用 contentType()
方法。
$request->body()->contentType(); // Example output: 'multipart/form-data; boundary=----WebKitFormBoundarySMTxQwbotazq4ctK'