psx / http
可变HTTP模型和HTTP客户端实现
v4.0.5
2024-09-15 10:25 UTC
Requires
- php: >=8.0
- guzzlehttp/guzzle: ^7.0
- psr/http-message: ^1.0
- psr/log: ^1.0 || ^2.0 || ^3.0
- psx/uri: ^3.0
Requires (Dev)
- phpunit/phpunit: ^9.0
- vimeo/psalm: ^5.0
README
关于
此库包含用于描述HTTP消息、中间件和客户端类的优雅接口。它还包含相应的参考实现,这些实现可以由任何需要坚固HTTP堆栈的应用程序使用。
HTTP
HTTP体
HTTP中间件
HTTP客户端
示例
中间件
以下展示了一个简单的中间件,它始终返回响应体 Hello World!
<?php use PSX\Http; use PSX\Http\Server; $chain = new Http\Filter\FilterChain(); // enforce user agent in HTTP request $chain->on(new Http\Filter\UserAgentEnforcer()); // display maintenance file if available $chain->on(new Http\Filter\Backstage(__DIR__ . '/.maintenance.html')); // closure middleware $chain->on(function(Http\RequestInterface $request, Http\ResponseInterface $response, Http\FilterChainInterface $filterChain){ // get query parameter $request->getUri()->getParameter('foo'); // set header $response->setHeader('X-Foo', 'bar'); // write data to the body $response->getBody()->write('Hello World!'); $filterChain->handle($request, $response); }); // create global HTTP request and response $request = (new Server\RequestFactory())->createRequest(); $response = (new Server\ResponseFactory())->createResponse(); // start middleware chain $chain->handle($request, $response); // send response (new Server\Sender())->send($response);
客户端
以下向google发送HTTP GET请求
<?php use PSX\Http\Client; use PSX\Http\Exception\StatusCodeException; // create HTTP client $client = new Client\Client(); // build request $request = new Client\GetRequest('https://google.com', ['Accept' => 'text/html']); // send request $response = $client->request($request); // check response if ($response->getStatusCode() == 200) { // get header $contentType = $response->getHeader('Content-Type'); // output response body echo (string) $response->getBody(); } else { // the client never throws an exception for unsuccessful response codes but // you can do this explicit StatusCodeException::throwOnError($response); }
上传
以下展示了如何处理文件上传的示例
<?php use PSX\Http; use PSX\Http\Server; $chain = new Http\Filter\FilterChain(); // closure middleware $chain->on(function(Http\RequestInterface $request, Http\ResponseInterface $response, Http\FilterChainInterface $filterChain){ // get body $body = $request->getBody(); if ($body instanceof Http\Stream\MultipartStream) { // move uploaded file to a new location $body->getPart('userfile')->move('/home/new/file.txt'); // or access the file directly through the normal stream functions //$body->getPart('userfile')->read(32); // write data to the body $response->getBody()->write('Upload successful!'); } else { // no upload so show form $html = <<<'HTML' <!-- The data encoding type, enctype, MUST be specified as below --> <form enctype="multipart/form-data" action="" method="POST"> <!-- MAX_FILE_SIZE must precede the file input field --> <input type="hidden" name="MAX_FILE_SIZE" value="30000" /> <!-- Name of input element determines name in $_FILES array --> Send this file: <input name="userfile" type="file" /> <input type="submit" value="Send File" /> </form> HTML; $response->getBody()->write($html); } $filterChain->handle($request, $response); }); // create global HTTP request and response $request = (new Server\RequestFactory())->createRequest(); $response = (new Server\ResponseFactory())->createResponse(); // start middleware chain $chain->handle($request, $response); // send response (new Server\Sender())->send($response);