maplephp / http
此库完全集成了PSR-7 Http消息,并专为与MaplePHP框架一起使用而设计。
v1.0.7
2024-09-29 14:04 UTC
Requires
- php: >=8.0
- maplephp/dto: ^1.0
Requires (Dev)
- maplephp/unitary: ^1.0
README
MaplePHP/Http是一个PHP库,它将简化性和对PSR-7标准的遵守性引入到处理Web项目中HTTP消息、请求和响应中。它经过精心设计,使Stream、Client、Cookies、UploadedFile和Headers等关键元素的集成变得简单高效。
通过与PSR-7紧密对齐,MaplePHP/Http促进了Web组件之间的更好的互操作性,使得应用程序内部的通信更加有效。无论您是在处理客户端cookie、管理请求中的头部,还是通过UploadedFile处理文件上传,这个库都能为您提供支持,使这些任务更易于管理,节省时间。
MaplePHP/Http旨在通过提供可靠的基础来支持您的Web开发,简化处理请求和响应的过程。它是开发者寻求以用户友好的方式增强其应用程序的PSR-7合规HTTP处理的首选选择。
安装
composer require maplephp/http
初始化
以下示例使用下面的“命名空间”仅为了更方便地演示指南。
use MaplePHP\Http;
请求
$request = new Http\ServerRequest(UriInterface $uri, EnvironmentInterface $env);
获取请求方法
echo $request->getMethod(); // GET, POST, PUT, DELETE
获取Uri实例
$uri = $request->getUri(); // UriInterface echo $uri->getScheme(); // https echo $uri->getAuthority(); // [userInfo@]host[:port] echo $uri->getUserInfo(); // username:password echo $uri->getHost(); // example.com, staging.example.com, 127.0.0.1, localhost echo $uri->getPort(); // 443 echo $uri->getPath(); // /about-us/workers echo $uri->getQuery(); // page-id=12&filter=2 echo $uri->getFragment(); // anchor-12 (The anchor hash without "#") echo $uri->getUri(); // Get the full URI
响应
只需要(StreamInterface) Body属性,如果您不修改其他属性,它们将自动传播。
$response = new Http\Response( StreamInterface $body, ?HeadersInterface $headers = null, int $status = 200, ?string $phrase = null, ?string $version = null );
获取状态码
echo $response->getStatusCode(); // 200
获取状态码
$newInst = $response->withStatus(404); echo $newInst->getStatusCode(); // 404 echo $newInst->getReasonPhrase(); // Not Found
消息
Request和Response库都将继承Message下的方法,但信息不同。
echo $response->getProtocolVersion(); // 1.1 echo $response->getHeaders(); // Array with all headers echo $response->hasHeader("Content-Length"); // True echo $response->getHeader("Content-Length"); // 1299 echo $response->getBody(); // StreamInterface
标准示例使用
$stream = new Http\Stream(Http\Stream::TEMP); $response = new Http\Response($stream); $env = new Http\Environment(); $request = new Http\ServerRequest(new Http\Uri($env->getUriParts()), $env);
流
构建属性不需要,如果您不修改,将自动传播。
$stream = new Http\Stream( (mixed) Stream (string) permission );
基本的流示例
写入流
$stream = new Http\Stream(Http\Stream::TEMP); if ($stream->isSeekable()) { $stream->write("Hello world"); //echo $stream; // will print Hello world // Or $stream->rewind(); echo $stream->getContents(); // Hello world // Or Same as above //echo $stream->read($stream->getSize()); }
使用流获取文件内容
$stream = new Http\Stream("/var/www/html/YourApp/dir/dir/data.json"); echo $stream->getContents();
将流上传到服务器
$upload = new Http\UploadedFile($stream); $upload->moveTo("/var/www/html/upload/log.txt"); // Place Hello world in txt file
创建请求
客户端将使用curl,因此,如果出于任何原因已禁用它,则必须确保它已启用。
// Init request client $client = new Http\Client([CURLOPT_HTTPAUTH => CURLAUTH_DIGEST]); // Pass on Curl options // Create request data $request = new Http\Request( "POST", // The HTTP Method (GET, POST, PUT, DELETE, PATCH) "https://admin:mypass@example.com:443/test.php", // The Request URI ["customHeader" => "lorem"], // Add Headers, empty array is allowed ["email" => "john.doe@example.com"] // Post data ); // Pass request data to client and POST $response = $client->sendRequest($request); // Get Stream data var_dump($response->getBody()->getContents());