gandung / http-message
PHP 中 PSR-7 HTTP 消息实现
v1.0.0
2018-02-03 16:22 UTC
Requires
- psr/http-message: ^1.0
- squizlabs/php_codesniffer: ^3.1
Requires (Dev)
- phpunit/phpunit: ^5.7.16 || ^6.0.8
Provides
This package is auto-updated.
Last update: 2024-09-19 18:23:28 UTC
README
这是 PSR-7 HTTP 消息 在 PHP 中的实现。
响应
启动响应
使用字符串作为体参数
use Gandung\Psr7\Response;
$content = 'this is a text.';
$response = new Response($content, 200, ['Content-Type' => 'text/plain']);
echo sprintf("%s\n", $response);
使用实现了 Psr\Http\Message\StreamInterface
的对象
use Gandung\Psr7\PhpTempStream;
use Gandung\Psr7\Response;
$content = 'this is a text.';
$stream = new PhpTempStream;
$stream->write($content);
$response = new Response($stream, 200, ['Content-Type' => 'text/plain']);
echo sprintf("%s\n", $response);
使用本地(类似于 fopen() 的函数)或远程(类似于 fsockopen() 的函数)流资源
use Gandung\Psr7\Response;
$handler = fopen('php://temp', 'r+b');
fseek($handler, 0);
fwrite($handler, 'this is a text.');
$response = new Response($handler, 200, ['Content-Type' => 'text/plain']);
echo sprintf("%s\n", $response);
重定向响应
启动 HTTP 重定向响应
使用 URI 字符串
use Gandung\Psr7\Response\RedirectResponse;
$response = new RedirectResponse('http://example.com/a/b/c?api_version=1.0');
使用 URI 对象
use Gandung\Psr7\Uri;
use Gandung\Psr7\Response\RedirectResponse;
$uri = (new Uri())
->withScheme('http')
->withHost('example.com')
->withPath('/a/b/c')
->withQuery('api_version=1.0');
$response = new RedirectResponse($uri);
空响应
启动 HTTP 空响应
use Gandung\Psr7\Response\EmptyResponse;
$response = new EmptyResponse;
请求
启动请求
使用 URI 字符串
use Gandung\Psr7\Request;
$request = new Request('GET', 'http://example.com/a/b/c?api_version=1.0');
使用 URI 对象
use Gandung\Psr7\Request;
use Gandung\Psr7\Uri;
$uri = (new Uri())
->withScheme('http')
->withHost('example.com')
->withPath('/a/b/c')
->withQuery('api_version=1.0');
$request = new Request('GET', $uri);
流
文件流
use Gandung\Psr7\FileStream;
$stream = new FileStream('your-file', 'r');
php://input
流
use Gandung\Psr7\PhpInputStream;
$stream = new PhpInputStream;
php://temp
流
use Gandung\Psr7\PhpTempStream;
$stream = new PhpTempStream;
$stream->write('this is a text.');
echo sprintf("%s\n", (string)$stream);
常见流
use Gandung\Psr7\Stream;
$handler = fopen('your-file', 'r');
fseek($handler, 0);
fwrite($handler, 'this is a text.');
$stream = new Stream($handler);
echo sprintf("%s\n", (string)$stream);
URI
使用完整构建的 URI(RFC 3986)
use Gandung\Psr7\Uri;
$uri = new Uri('http://user:[email protected]:13123/a/b/c?foo=bar#fragment');
echo sprintf("%s\n", $uri);
使用不可变的 URI 组件
use Gandung\Psr7\Uri;
$uri = (new Uri())
->withScheme('http')
->withUserInfo('user', 'password')
->withHost('example.com')
->withPort(13123)
->withPath('/a/b/c')
->withQuery('foo=bar')
->withFragment('fragment');
echo sprintf("%s\n", $uri);
文件上传
这适用于 SAPI 和非 SAPI PHP 环境
use Gandung\Psr7\UploadedFile;
$uploadedFile = new UploadedFile(
'source-file',
'destination-file',
\UPLOAD_ERR_OK
);
服务器请求
仅使用 URI
use Gandung\Psr7\ServerRequest;
$uri = (new Uri())
->withScheme('http')
->withHost('example.com')
->withPath('/a/b/c')
->withQuery('foo=bar');
$request = new ServerRequest($uri);
来自 PHP 超全局变量
use Gandung\Psr7\ServerRequestFactory;
$request = ServerRequestFactory::createFromGlobals();