# PSR-7 Message Implementation
This repository contains a full [PSR-7](http://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)
# Stream implementation
This package comes with a number of stream implementations and stream
decorators.
## 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;
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);
```
## `GuzzleHttp\Psr7\Message::bodySummary`
`public static function bodySummary(MessageInterface $message, int $truncateAt = 120): string|null`
Get a short summary of the message body.
Will return `null` if the response is not printable.
## `GuzzleHttp\Psr7\Message::rewindBody`
`public stati
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
仿闪客网盘蓝奏云网盘源码修复运营版 (5525个子文件)
00033c37f0b5b4a2ec66759f7eebf86e1f3fc6 8KB
001176d018bd72f9f2128f2f0bf775aab99aa8 204B
00412a5b19c27b129fa1da6ce8f4a321464d14 2KB
0052b16ecc5d6928dcaecca2beb9c0335c56fe 2KB
0084963159b64a9c65fba875d9ec6404d38541 371B
00addfd5f7b83668eacf777b6bc6b015709e3d 221B
00beab1a0e65178c53493e3807a11093a47a57 148B
00c11ad063c08aa1a2aec93ea0faa01860bfba 80B
00fc4117d938236eb5eb6c32ba21c43106016f 302B
00fd9a68f41c70b91819e2f71ce6a36d922e4b 1KB
010888a70dfbd3d762421a364d2594eafba902 73KB
011410ead3bc912968296bd9932f8dc493bb1d 2KB
01145afd675163e0dbb13ace5ae9431ed5cabf 80B
012918e033082e4f727ec48169322da7fa3c5c 403B
0132707b2d9b49c4e3ed0cebc3695d015d42e4 7KB
013aa2170853f137521c57561244c2cf28bbc2 80B
015ab4143687e0e4434d81ba4ea3b25e7dcc94 396B
015b74a0f4222ae88d4540435052509280ad93 305B
01696385a92addcb5a1160e8bbe8e25026958a 68B
01802d2fe569dd5d731552870d02c01a3e211f 292B
01de9f2412fc881de6fd79efa93e59ef74da66 564B
0206931cacff2d22e9252ababd5c56216daa47 440B
020cc1b7d489578334cee644e972473ff7ab32 305B
0220f4d0cd21495d0de45a33d6a0f5b87e0024 322B
0236d543c3b89c6ed47d60cc7f1814a3ec8767 958B
02488d8aa7035358ac4376a81f7b836980ef08 923B
026c9babf871c94d4a6382bf45787b942ebd12 652B
028dc6d0fc8bf0160240f5d0896a2e0bf69867 769B
02e036e322b8908e760beda3edbf06b5c490ed 171B
02e9767f51772043b369d926e0aa452ad0616a 2KB
02fdbf9853eabf558763907ebf2286fe820bca 8KB
02feb5a17a8a80aeace82b7b5b2870ae9750dd 1KB
03407b1175f15b69312fa437393684061c59af 308B
0341a05fbca13862f7ea049fa7dd049e12f38e 668B
0351c5f3541a7407252e56c6d90a740f3cdd95 128B
038e9c1ceb9d8da8f3bf2dc5b31a3b5eebb8ac 670B
03a0d7e1efef82173238d41571b3dc2fee3710 285B
03af28149b862a033dc731f98cbec44dbab9e4 31KB
03b1ea2d2775be271d74bdd42b296d3555ab35 485B
03c07f9cd2d765f6760cf933db6a923e0985bc 297B
03cc378533f22c970de0b1896670a0df51de2e 56B
03cc38571175a1faa98bd038ce9b1c515afb0a 3KB
03d09a2841a87de1ced8e4446d051e7f62f455 196B
03e02b883d48cf93b29553de4f845ee40eab06 232B
042572115e3aa5bcd62cfd0e211660a6547f20 383B
0440ce89f2a408aa699b65100e18f15e0f09ca 8KB
045fcce57d0090abfc39ce1e6781f4b9f2649c 2KB
0472443b8340f587f5f72b54e80a1b7b334e63 564B
048394b6ffd766f82cdff4ec9d6dee19311e69 144B
0487120940698533144c1f1f9a598366403656 566B
049efe2a4f36ffb323bb03540b0987ac409e90 673B
04c4540a819e3bbb989306665c053a7ba70322 286B
050820471872cdaa208f6cec167aa3e612c805 3KB
0536286f3081c6c0646817037faf5446e3547d 40B
054d2b8e0b687eed39d9c0ff6a64c4813f7fc1 382B
05551e93aa9e12dfe21deed4202d0adcbddcb6 498B
057299b059410e746a74bb14d7ddd63bbbcb17 293B
059e994468c5b929b67a352335e08791d968f3 2KB
05ae49f5029b118d5c257c5239845a8e77bf07 737B
05c31b6c8131935b924008e49120bf57b6be47 1KB
05d2d92397eab9e99807011fd1c75cabc82314 923B
05dbf698904c9f3f5f8acb8ac1e91795d697dc 18KB
05e233c3a891bed1e8bb971ed06220a7d430fe 5KB
060410199c4d43cf7f6ef345c54f8a7ed1c589 157B
0612bf0e3f1f77cf23b2f812d1e18c0b5b66d6 445B
063592cafcac494ae4245b990961019bfc0106 286B
065087d4720326776b26c7b9d2482c9e467bca 2KB
06514e6bbdf07282bda9e3dc61d91f6554e642 1KB
067c46b5b8b1cb5d2f12c65bf7a9bfc0127869 608B
06b67e4419e056f5a3fb4b7232769ee9976232 76B
06cd38d11356a653fe8bdeac08055811dda393 2KB
070efc10f886dc762a36d25e73749c6655b905 276B
0717c7237f5b2bec9a988fbfb65d0e326b2586 309B
07650fd4b1ab24e420adc08dee0077bfc5624a 58B
0784361c33d16a01b70c9172f24e78710dfc56 3KB
07a1b2873f2d0cf871c0e2ec6285638e107907 602B
07bba550fb8ebfbf732e4a1990cb6c19f7854b 2KB
07da2e8e907140f4a5b0b2eb138512c54cb578 151B
07db6403ad25de7f9f4e612daf4096c3b6f68d 4KB
07de3f6cec08afb8b35a5a871391c7495d43dc 291B
07e084b8ff17d5eacfe81fe2ea4c7dcf7b9f15 2KB
07f1f03852c2782a55a216558eb839402ea8f1 5KB
0803383997e6da576b2be8f3234cc5a94374d7 3KB
08213e8cc56a709cf60398cc472ba194318b59 226B
082794a785ae657f79c866f2ffd616c0391b86 24KB
084de008cbb0aac62096fe1627e842dc7fa9b7 578B
084e7e939cd2a424c8c2105c08cdccdca73562 12KB
0871d019e5342ac52c15c2e2cd9bae97e49074 488B
089b41948d92605213b8d54ca14bd67656e76e 7KB
089c9edaafabe3859b9a7a3f1b327dd2459559 1KB
08e224004c64af49c1e6cca80442704701b4bc 138B
08e7b54b055bd99c53ff788f77f0f935601d5a 3KB
08f808b3f734db44b3244bc7819eb782bc0d11 6KB
09086bcb69ac51c663647e9f649725cffb4213 2KB
09443c999176f8fc629a7a1d25bb8a9f698679 3KB
0956c9d2a0368a3de8c114b357f2dd7f9e338a 471B
09589af76c40586cbb1b343059d10c0840d54f 249B
095f72cdfea386ca6adcf4ae1ac7c1b14128c2 17KB
0967cfe949cea9a2f6d941e1afd84ee4d445f0 1000B
0970371bef20fe31f4a7af82f0213bd5600331 52B
共 5525 条
- 1
- 2
- 3
- 4
- 5
- 6
- 56
资源评论
Mr阁主
- 粉丝: 11
- 资源: 6
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功