tenaga / http
快速且简单的 HTTP 组件
v1.1
2020-04-24 09:34 UTC
This package is auto-updated.
Last update: 2024-09-27 01:38:55 UTC
README
为 Tenaga 框架 提供的 Http 基础库,灵感来源于 PatrickLouys/http
如何安装?
composer require tenaga\Http
用法?
请求
请求提供了一些函数来访问超级全局 PHP 变量。如何初始化?写一些代码如下
use Tenaga\Http\Request; $request = new Request; $request->set();
然后你可以调用以下函数
# Get value from $_REQUEST superglobals variables $request->all($key, $default = null); # Get value from $_POST superglobals variables $request->post($key, $default = null); # Get value from $_GET superglobals variables $request->get($key, $default = null); # Get value from $_FILES superglobals variables $request->file($key, $default = null); # Get value from $_COOKIE[$key] superglobals variables $request->cookie($key, $default = null); # Get all parameters key & value that passe by $_GET and $_POST $request->parameters(); # Get uri, i.e : test.com/test1/test2/... $request->uri(); # Get url path, i.e : test1/test2/test3/.../... $request->path(); # Get method that receive $request->method(); # Get HTTP_ACCEPT for current request $request->httpAccept(); # Get HTTP_REFERER for current request $request->referer(); # Get user agent for current request $request->userAgent(); # The IP address from which the user is viewing the current page. $request->ipAddress(); # Check if the current page using https, that will return boolean $request->isSecure();
响应
响应提供了一些函数来准备以字符串类型的变量作为响应。初始化代码如下。
use Tenaga\Http\Response; $response = new Response;
然后你可以调用以下函数
# Set up the response $response->setResponse($response); # Get the response, you must add it at end $response->getResponse(); # Append the current response $response->appendResponse($response);
头部
头部将提供一些函数来操作头部。初始化代码如下。
use Tenaga\Http\Header; $header = new Header;
然后你可以使用以下函数
# Set status code $header->setStatusCode($statusCode , $statusText = null); # Set content type that will show $header->setContentType($type, $charset = NULL); # Set expires $header->setExpired($date); # Set header $header->setHeader($name,$value); # Get all header $header->getAllHeader();
Cookie
Cookie 提供了一些函数来轻松操作和添加 Cookie。初始化代码如下。
use Tenaga\Http\Cookie; $cookie = new Cookie;
然后你可以使用以下函数
# Set cookie by just passing the cookie by an array prepare(array $cookie); # Set cookie name setName(string $name); # Set cookie value setValue($value); # Set Cookie expired setExpired(int $time); # Set cookie path setPath($path); # Set Cookie Domain setDomain($domain); # Set cookie secure (optional), default : false setSecure(bool $secure); # Set Http only (optional), default : false setHttpOnly(bool $httpOnly); # Create the cookie create();
要使用 Cookie,你必须写如下代码
$cookie->prepare ->setName('ExampleCookie') ->setValue('Example') ->setExpired(3600) ->setPath('/') ->setDomain('localhost.com') ->setSecure(true) ->setHttpOnly(true) ->create();
你可以调用其中一些或所有函数,但必须使用 prepare 来准备 Cookie,并在最后使用 create() 函数来创建 Cookie
或者你可以使用 prepare() 函数来创建新的 Cookie。
/** * List Of key of array * 0 => name * 1 => value * 2 => expire * 3 => path * 4 => domain (optional) * 5 => secure (optional) * 6 => httponly (optional) */ $cookieData = [ 'ExampleCookie', 'Example', 3600, '/', 'localhost.com' ]; $cookie->prepare($cookieData)->create();