sn01615 / request
PHP中HTTP请求的包装器。
This package is not auto-updated.
Last update: 2024-09-27 02:19:18 UTC
README
'Request'类是HTTP请求的包装器。它提供了一系列简单的方法,以便轻松可靠地获取与HTTP请求关联的信息。
安装
composer require sn01615/request
用法
通用数据访问方法
以下方法可用于从与HTTP请求关联的任何PHP超级全局数组中检索数据: get
、post
、files
、session
、cookie
、env
和 server
。
例如,要获取通过POST请求提交的'username'属性
$username = Request::post('username');
传递给数据访问方法的参数是区分大小写的。此外,通过 server
方法可访问的头部变量必须用下划线而不是连字符指定,并带有 HTTP_ 前缀。
// Request the Cache-Control header:
$cc = Request::server('HTTP_CACHE_CONTROL');
如果请求的变量未定义,访问方法将返回 NULL,除非您提供了默认值。
// Will return Fred Nurk if $_POST['username'] is undefined.
$username = Request::post('username', 'Fred Nurk');
如果没有参数调用,方法将返回一个包含关联超级全局的所有变量的数组。
// Requested: http://a.com/?username=fred&id=1
// Returned: array('username' => 'fred', 'id' => '1')
$input = Request::get();
为了模拟浏览器不支持HTTP方法(例如PUT、DELETE),将方法名称分配给名为HTTP_X_HTTP_METHOD_OVERRIDE的请求变量。例如
// To simulate the PUT method via a HTML form:
<form method="post">
<input type="text" name="username" />
<input type="submit" name="Submit" />
<input type="hidden" name="HTTP_X_HTTP_METHOD_OVERRIDE" value="PUT" />
</form>
要访问通过PUT或DELETE提交的输入数据,请使用 put
和 delete
方法。它们与 get
和 post
的工作方式相同。
使用 body
方法获取请求的原始正文。可以提供可选的默认值。
// Requested: The form above is submitted with username 'Fred'
// Returned: username=Fred&Submit=Submit&HTTP_X_HTTP_METHOD_OVERRIDE=PUT
$body = Request::body();
input
方法也像其他访问方法一样工作,可以用于检索无论通过GET、POST、PUT还是DELETE请求提交的输入数据。如果没有参数调用,input
将返回一个包含所有输入超级全局数据的数组。 all
方法更进一步,还将返回 $_FILES 数组中的数据。
使用 only
方法仅返回输入数据的一个子集。
// Only get the email variable from the input data:
$email = Request::only('email');
// Only get the username and email variables from the input data:
$input = Request::only(array('username', 'email'));
相反,使用 except
方法返回除了一个或多个项之外的所有输入数据。
// Get all input data except for username and email:
$input = Request::except(array('username', 'email'));
only
和 except
方法可以用字符串或数组调用,但两者都将始终返回数组或 NULL。
使用 has
方法检查输入数据中是否存在一个或多个变量。如果至少有一个变量未定义或为空,方法将返回 FALSE;否则返回 TRUE。
// Has `id` been submitted?
if (Request::has('id')) { echo 'The `id` exists.'; }
// Have `id` and `name` both been submitted?
if (Request::has(array('id', 'name'))) { // do cool stuff }
请求属性
host
方法返回请求的域名或主机名。如果无法确定主机名,则可以返回默认值。
// Requested: http://a.com/blog/
// Returned: a.com
$host = Request::host();
port
方法返回请求的目标端口号。如果调用时带有参数值 TRUE(表示结果应“装饰”),则方法将在端口号前加上冒号,但不会为标准端口80和443返回值。
// Requested: http://a.com/blog/
// Returned: 80
$port = Request::port();
// No value returned for standard ports:
$port = Request::port(TRUE);
protocol
方法返回请求中使用的HTTP协议版本。该方法接受默认值,但如果未提供,且无法确定协议,则返回 'HTTP/1.1'。
scheme
方法返回请求中使用的HTTP方案(即http或https)。
// Requested: http://a.com/
// Returned: http
$scheme = Request::scheme();
// Requested: http://a.com/
// Returned: http://
$scheme = Request::scheme(TRUE);
secure
方法如果HTTP方案是 'https' 则返回 TRUE,如果是 'http' 则返回 FALSE。
if (Request::secure()) { 'Requested over HTTPS.'; }
使用 method
获取HTTP请求的方法(例如GET、POST)。结果始终以大写形式返回。
$method = Request::method();
如果实现REST API,使用safe
方法来判断请求是否为“只读”(即GET或HEAD)。
使用ajax
方法来判断请求是否通过XMLHttpRequest提交。
if (Request::ajax()) { 'An AJAX request.'; }
referrer
方法返回请求的引用地址。如果引用未定义,可以返回默认值。
entrusted
方法如果所有代理服务器都被隐式信任(即未定义信任的代理服务器),或者请求已经通过一个特定信任的代理服务器,则返回TRUE。下面将详细介绍如何定义信任的代理服务器。
与URL协同工作
使用url
方法返回请求的完整URL,使用uri
返回除查询字符串之外的URL路径。URI总是以'/'前缀返回。
// Requested: http://a.com/blog/?id=1
// Returned: http://a.com/blog/?id=1
$url = Request::url();
// Returned: /blog
$uri = Request::uri();
URI是通过检查以下标题确定的:PATH_INFO、REQUEST_URI、PHP_SELF和REDIRECT_URL。可以通过指定自定义URI解析器来扩展或覆盖这些标题的使用。这是通过调用resolvers
方法并传递一个包含可选修改函数的数组来实现的,这些函数可以调整返回值。如果无法(或不应)从标题解析URI,修改函数应返回FALSE;任何其他由修改函数返回的值都将被视为URI。
// Add X_REWRITE_URI as a source for the URI, but only
// if Windows is the server platform:
Request::resolvers(array('X_REWRITE_URI' => function($uri) {
return stripos(PHP_OS, 'WIN') ? $uri : FALSE;
}));
使用query
方法返回请求的查询字符串。调用方法时传入TRUE以包含'?'前缀。
// Requested: http://a.com/blog/?name=joe&id=1
// Returned: name=joe&id=1
$query = Request::query();
segments
方法返回一个包含所有URI段的数组。如果URI为空,可以提供一个可选的默认数组值。
// Requested: http://a.com/blog/admin/posts/
// Returned: array('blog', 'admin', 'posts');
$segments = Request::segments();
可以使用segment
方法检索单个URI段。该方法接受一个基于1的索引和可选的第二个参数作为默认值。使用负索引调用segment
将以相反的顺序评估段。
// Requested: http://a.com/blog/admin/posts/
// Returned: blog
$first = Request::segment(1);
// Returned: posts
$last = Request::segment(-1);
客户端属性
languages
方法返回一个包含客户端接受的语言的数组。语言按偏好顺序排序(最高偏好优先)。
// e.g. array('en-US', 'en');
$languages = Request::languages();
可以使用language
方法检索首选语言,该方法也接受一个可选的默认值。
accepts
方法返回一个包含按偏好顺序排列的接受媒体类型列表的数组(例如,'text/html'),而accept
方法返回最高偏好的媒体类型。
默认情况下,accept
方法以“友好”格式返回媒体类型(例如,“html”或“json”),但如果调用时带有一个可选的第二个参数为TRUE,则将返回完整的媒体类型。
// Requested: http://a.com/code.js
// Returned: js
// Default: html
$accepted = Request::accept('html');
// Returned: application/javascript
$accepted = Request::accept('text/html', FALSE);
Request类可以无缝地将大多数常用媒体类型转换为“友好”格式,但您可以通过使用format
方法添加对其他格式转换的支持。第一个参数是友好格式,第二个是媒体类型。
// Support a new media type format:
Request::format('font', array('application/x-font-woff',
'application/font-off'));
type
方法返回请求主体的内容类型。与accepts
方法一样,支持两个可选参数;第一个设置默认值,第二个控制结果的格式。
charsets
方法返回一个包含按偏好顺序排列的接受字符集列表的数组(例如,“utf-8”),而charset
方法返回最高偏好的字符集。
与language
和accept
方法一样,charset
支持一个可选的默认值。
agent
方法以字符串形式返回客户端用户代理(例如,Mozilla/5.0 (Macintosh; …))。
ip
方法试图返回客户端的IP地址。返回值受以下因素的影响,包括HTTP_CLIENT_IP头变量值是否可信,以及请求是否通过信任的代理服务器发出。
默认情况下,客户端以及所有代理服务器都被信任。如果您不信任客户端,请调用方法并传入值FALSE。
// Do not retrieve the IP address from HTTP_CLIENT_IP:
$ip = Request::ip(FALSE);
如果只想信任通过特定代理服务器中继的IP地址,您必须首先调用proxies
方法,并传入包含受信任代理服务器IP地址的数组。
// Only trust IP addresses relayed via the proxy 10.0.0.10:
Request::proxies('10.0.0.10');
// To trust 10.0.0.10 and 10.0.0.11:
Request::proxies(array('10.0.0.10', '10.0.0.11'));