# PSR-7 Message Implementation
This repository contains a full [PSR-7](https://www.php-fig.org/psr/psr-7/)
message implementation, several stream decorators, and some helpful
functionality like query string parsing.
![CI](https://github.com/guzzle/psr7/workflows/CI/badge.svg)
![Static analysis](https://github.com/guzzle/psr7/workflows/Static%20analysis/badge.svg)
## Features
This package comes with a number of stream implementations and stream
decorators.
## Installation
```shell
composer require guzzlehttp/psr7
```
## Version Guidance
| Version | Status | PHP Version |
|---------|---------------------|--------------|
| 1.x | Security fixes only | >=5.4,<8.1 |
| 2.x | Latest | >=7.2.5,<8.4 |
## AppendStream
`GuzzleHttp\Psr7\AppendStream`
Reads from multiple streams, one after the other.
```php
use GuzzleHttp\Psr7;
$a = Psr7\Utils::streamFor('abc, ');
$b = Psr7\Utils::streamFor('123.');
$composed = new Psr7\AppendStream([$a, $b]);
$composed->addStream(Psr7\Utils::streamFor(' Above all listen to me'));
echo $composed; // abc, 123. Above all listen to me.
```
## BufferStream
`GuzzleHttp\Psr7\BufferStream`
Provides a buffer stream that can be written to fill a buffer, and read
from to remove bytes from the buffer.
This stream returns a "hwm" metadata value that tells upstream consumers
what the configured high water mark of the stream is, or the maximum
preferred size of the buffer.
```php
use GuzzleHttp\Psr7;
// When more than 1024 bytes are in the buffer, it will begin returning
// false to writes. This is an indication that writers should slow down.
$buffer = new Psr7\BufferStream(1024);
```
## CachingStream
The CachingStream is used to allow seeking over previously read bytes on
non-seekable streams. This can be useful when transferring a non-seekable
entity body fails due to needing to rewind the stream (for example, resulting
from a redirect). Data that is read from the remote stream will be buffered in
a PHP temp stream so that previously read bytes are cached first in memory,
then on disk.
```php
use GuzzleHttp\Psr7;
$original = Psr7\Utils::streamFor(fopen('http://www.google.com', 'r'));
$stream = new Psr7\CachingStream($original);
$stream->read(1024);
echo $stream->tell();
// 1024
$stream->seek(0);
echo $stream->tell();
// 0
```
## DroppingStream
`GuzzleHttp\Psr7\DroppingStream`
Stream decorator that begins dropping data once the size of the underlying
stream becomes too full.
```php
use GuzzleHttp\Psr7;
// Create an empty stream
$stream = Psr7\Utils::streamFor();
// Start dropping data when the stream has more than 10 bytes
$dropping = new Psr7\DroppingStream($stream, 10);
$dropping->write('01234567890123456789');
echo $stream; // 0123456789
```
## FnStream
`GuzzleHttp\Psr7\FnStream`
Compose stream implementations based on a hash of functions.
Allows for easy testing and extension of a provided stream without needing
to create a concrete class for a simple extension point.
```php
use GuzzleHttp\Psr7;
$stream = Psr7\Utils::streamFor('hi');
$fnStream = Psr7\FnStream::decorate($stream, [
'rewind' => function () use ($stream) {
echo 'About to rewind - ';
$stream->rewind();
echo 'rewound!';
}
]);
$fnStream->rewind();
// Outputs: About to rewind - rewound!
```
## InflateStream
`GuzzleHttp\Psr7\InflateStream`
Uses PHP's zlib.inflate filter to inflate zlib (HTTP deflate, RFC1950) or gzipped (RFC1952) content.
This stream decorator converts the provided stream to a PHP stream resource,
then appends the zlib.inflate filter. The stream is then converted back
to a Guzzle stream resource to be used as a Guzzle stream.
## LazyOpenStream
`GuzzleHttp\Psr7\LazyOpenStream`
Lazily reads or writes to a file that is opened only after an IO operation
take place on the stream.
```php
use GuzzleHttp\Psr7;
$stream = new Psr7\LazyOpenStream('/path/to/file', 'r');
// The file has not yet been opened...
echo $stream->read(10);
// The file is opened and read from only when needed.
```
## LimitStream
`GuzzleHttp\Psr7\LimitStream`
LimitStream can be used to read a subset or slice of an existing stream object.
This can be useful for breaking a large file into smaller pieces to be sent in
chunks (e.g. Amazon S3's multipart upload API).
```php
use GuzzleHttp\Psr7;
$original = Psr7\Utils::streamFor(fopen('/tmp/test.txt', 'r+'));
echo $original->getSize();
// >>> 1048576
// Limit the size of the body to 1024 bytes and start reading from byte 2048
$stream = new Psr7\LimitStream($original, 1024, 2048);
echo $stream->getSize();
// >>> 1024
echo $stream->tell();
// >>> 0
```
## MultipartStream
`GuzzleHttp\Psr7\MultipartStream`
Stream that when read returns bytes for a streaming multipart or
multipart/form-data stream.
## NoSeekStream
`GuzzleHttp\Psr7\NoSeekStream`
NoSeekStream wraps a stream and does not allow seeking.
```php
use GuzzleHttp\Psr7;
$original = Psr7\Utils::streamFor('foo');
$noSeek = new Psr7\NoSeekStream($original);
echo $noSeek->read(3);
// foo
var_export($noSeek->isSeekable());
// false
$noSeek->seek(0);
var_export($noSeek->read(3));
// NULL
```
## PumpStream
`GuzzleHttp\Psr7\PumpStream`
Provides a read only stream that pumps data from a PHP callable.
When invoking the provided callable, the PumpStream will pass the amount of
data requested to read to the callable. The callable can choose to ignore
this value and return fewer or more bytes than requested. Any extra data
returned by the provided callable is buffered internally until drained using
the read() function of the PumpStream. The provided callable MUST return
false when there is no more data to read.
## Implementing stream decorators
Creating a stream decorator is very easy thanks to the
`GuzzleHttp\Psr7\StreamDecoratorTrait`. This trait provides methods that
implement `Psr\Http\Message\StreamInterface` by proxying to an underlying
stream. Just `use` the `StreamDecoratorTrait` and implement your custom
methods.
For example, let's say we wanted to call a specific function each time the last
byte is read from a stream. This could be implemented by overriding the
`read()` method.
```php
use Psr\Http\Message\StreamInterface;
use GuzzleHttp\Psr7\StreamDecoratorTrait;
class EofCallbackStream implements StreamInterface
{
use StreamDecoratorTrait;
private $callback;
private $stream;
public function __construct(StreamInterface $stream, callable $cb)
{
$this->stream = $stream;
$this->callback = $cb;
}
public function read($length)
{
$result = $this->stream->read($length);
// Invoke the callback when EOF is hit.
if ($this->eof()) {
call_user_func($this->callback);
}
return $result;
}
}
```
This decorator could be added to any existing stream and used like so:
```php
use GuzzleHttp\Psr7;
$original = Psr7\Utils::streamFor('foo');
$eofStream = new EofCallbackStream($original, function () {
echo 'EOF!';
});
$eofStream->read(2);
$eofStream->read(1);
// echoes "EOF!"
$eofStream->seek(0);
$eofStream->read(3);
// echoes "EOF!"
```
## PHP StreamWrapper
You can use the `GuzzleHttp\Psr7\StreamWrapper` class if you need to use a
PSR-7 stream as a PHP stream resource.
Use the `GuzzleHttp\Psr7\StreamWrapper::getResource()` method to create a PHP
stream from a PSR-7 stream.
```php
use GuzzleHttp\Psr7\StreamWrapper;
$stream = GuzzleHttp\Psr7\Utils::streamFor('hello!');
$resource = StreamWrapper::getResource($stream);
echo fread($resource, 6); // outputs hello!
```
# Static API
There are various static methods available under the `GuzzleHttp\Psr7` namespace.
## `GuzzleHttp\Psr7\Message::toString`
`public static function toString(MessageInterface $message): string`
Returns the string representation of an HTTP message.
```php
$request = new GuzzleHttp\Psr7\Request('GET', 'http://example.com');
echo GuzzleHttp\Psr7\Message::toString($request);
```
##
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
最新海外 资产管理系统源码 外太空投资理财系统源码 个人理财管理系统.zip (2001个子文件)
0308b9e5305c86c8774c819bce2ab6fb4f1a600f 14B
07ac43e41b5087c9c8649157dc6587da373116d9 23B
0b5ce062ea24a15f0ca1caa362e0ea23312ae3fd 14B
0f582b4567a4400229a48f20d90afaea8ded1419 14B
116d2c4df794a0f49ac80999c60067cec928d44d 14B
1316145bc87a2d3e02e8423276bfbd5eb05b7bf9 14B
133bfa29898cf1224ac00adee1775ac6fe002a03 14B
136436ca9cb61385b50a6d121f003a1eabeec2d7 23B
13b89c14035ebb14688e6feef5bed85d8484cdb8 23B
1bb7cc3b779342cf31fb2fc904981c6cb297bacf 14B
1d4b12aec247ad94ae85bf2cfd411e75449a651c 14B
1d67d0c9022efb54615b2b92e47a43edcebf8aea 23B
1e40c18d4176f626543852a4cefb0fbdd94b8586 23B
1ee5e5b70d076e339256a9f38ff014b7b94368d4 14B
1f75d3ca5634984a2e7291e4f0d1acbf51b0ab15 14B
200ff36fd3f3efea926ebec27ce288e92a7d68f8 23B
20da37b3814c877c441ce1b0ebf3d405e0507f7c 23B
282e24369cb5f6111a660655dd0337fb6db8f649 14B
284eb343f0d55f62269ace793150e9185fae892b 14B
28f9918153674677bc577849c16217bc7b8e194f 23B
294c4d06756437a2d7f474af0c5add1a4526d641 23B
29649c764c5b2141d6af725ea664d79b31910a57 23B
2cb2f0f5e872cd89a74186873e5c58e1866627ee 23B
2d852acfada15a9b17bfda5b06be140b18cd013c 14B
2dfafcbe175fe51da16ab8ad12ddaafaff7ccc5f 23B
2f827c00a129d36e3b4530de0097de99e0e50fee 23B
300e7ea4489d543bb1a95cb371208f013700e419 23B
3011e9bbdb2ef2c047142b11b6a12af76171a699 14B
37465ca013e6dc802c59472d040b169aa0657464 23B
38c5117c9e6b827ca6b53ce4e430de1ab15d4c70 14B
3c045bc0b90a65d1d516b73dbbe971294cbdaf62 14B
3c0dd122d6d5c30a9cbc3b19e6027ad734f28504 14B
3c638980fae788134bb52c9a068b0b99efed2544 23B
3d331f6a7e64a54434c0de4201d53a902d08a05f 23B
3dbe643f71c16bef5db4cce4dacbad5a9cf4a517 14B
3f7c2e407da8f73a36f45ed4eb39ebb0379bf5f4 14B
3fe4c07e970fc53e932310829807a1dfaf5a77fd 23B
400db1f620f498f36d4fdce3a9b61291f8c97ecd 14B
429de8858e311621c028662660a3bbe8eca6e5aa 14B
43921130655b8af770e21a20acca675dfba5e6fd 23B
4505332eb7620695e73a4d96e6b46ecc602894f4 14B
45e3e75b0cd56489bb3159ace9199d50282b7088 23B
4b4e4e39af354970e033aa0a3b0642eea1c0f6de 14B
4bcef7c182d4a3cf37d922deca579d71df618932 14B
53e383c9a4a756245f921580e5684932fe9c4b69 14B
573d16e76a6668131389e5126fe37785f9847f4b 23B
5916275b5b50d62512ecd7cc99ac604081212b12 23B
5c08c8b7e0e04e02205aa8aaa3084a9f07ca0185 23B
5c133a191091a1dda8acbc87fa2a21f467d794ba 23B
5f56de4dee69e2e20421e83b8ec82bce4862a86b 14B
5f9ff04e2f728d2731357b108c0da226344be0e7 23B
618d89466b228ca7cede81daedb7b045d48277f3 23B
63103583cd42c49100a268c41e8d9d4131b7976a 23B
6360c2c9d234b68cb49daabeecc07b274bd4dfab 23B
65382864d140734cab3f59a89e20d357e01283b3 14B
6606915e1987624f4a08e948458be2c52dbc978c 23B
6745592e168d358200de988b72288a69aa8a1127 23B
67d27301b07443713f73478fcecb900e0795fc30 23B
6a231acd82ce394ec9d64f7c4013631fd5871bb8 23B
70955231155664f9c466d34749f238e661ea5cd2 14B
712f6e3b676ec9525c6c83c9e29752b0d4c8dde5 23B
747912983c5761bf5d47d13dd573a9b9c420fc83 14B
7f2bb6344009d7316cb8af14a30ed8d3afd0783b 23B
820ec93367b35cc4a1cd97d1b46d1e935ed939f3 14B
82296da55e35a1e9e0983d91ff4df621ef3cec6b 23B
8272541752c623f021d6650a4b05b26f0c6e38ff 14B
8b5d572b9559fa773116271b5c93fd7023845de0 14B
8be8d125caa26db0644d668c26202e30ce5783a7 14B
8d241d57ce00bcffec0428223465307fc713ee76 23B
9121d7bcbdd4713fc9f2c214583c0eb8823da3d5 14B
9560da7c11fa88b75a70e66b9704a46d66aa8924 23B
979f72178b81bbc48a3e2b606f04d0bb10b01c0e 14B
9961f86b3bfb11e167fe17c334c58a23882c6131 23B
9b59740eca321fcfb2d34079e115e45548015d80 23B
9f48f55b25ac7bceca78ac4cdeced47dca3c5e67 14B
a1daf1d48057f0e5a6c2792ce392cc42d2bec8a9 14B
a2cd7aaea17b1f60cf5319f4d347463c304ab669 23B
a2fa88326c3835b28820a53491c34ca4f5456119 23B
a4888601de935f6b0f5b78772298072322ec8d59 14B
a572e0b654ed6158063e010d63594d475f18828e 23B
ae2eb1a827ab861deec0586605cb0435b00b0ad6 23B
b9dea7db07dbd3fbcbbd8e6d6deed0087c34504b 14B
bd50219fc016093655e17314c0721ede2b42e739 23B
be88ee37265a4ce654dc50fa2888f796baf97be4 14B
bee486cbc36843cc19b6b49ad6c0813350e4b3fd 14B
c071422490d526a8aada046adf3a9c9fa0593468 14B
c214a4c5a8f6a70103649c98273ce033c73f6602 23B
c289c73fd44243bbefe76dc485cc2ed6ed9f2ab8 23B
c36ddd7747946e09c3b45a779a74651ce9e92735 23B
c3ce03e72d0b08e2de3f17d4bb342a3fd6850d70 14B
cf5c5cf37947d691f9607c529f309c0871a713ee 14B
index-8da9c173.css 429KB
chunk-vendors.321cebb9.css 250KB
bootstrap.css 208KB
bootstrap.css 208KB
bootstrap.min.css 205KB
bootstrap.min.css 205KB
bootstrap.css 187KB
bootstrap.css 187KB
index-614d86dd.css 171KB
共 2001 条
- 1
- 2
- 3
- 4
- 5
- 6
- 21
资源评论
智慧浩海
- 粉丝: 1w+
- 资源: 5431
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 基于ssm框架+Spring boot+Mybatis整合实现增删改查(适合初学者入门必备也可以做脚手架开发)
- python实现DES算法
- 基于php+html实现的成绩管理系统【源码+数据库】
- 大模型实战教程:从理论到实践的全面指南
- Spring Boot整合Shiro:实现动态权限加载更新、Session共享与单点登录的示例
- 圣诞树项目中硬件和MATLAB结合的控制与仿真技术
- 基于Springboot+shiro开发的权限管理(适合初学者练手必备快速掌握理解shiro权限框架)
- 基于JSP、JavaBean、JDBC、Servlet技术实现用户登录功能 【源码+数据库】
- boston-house-prices
- 解决mac安装key-patch安装报错
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功