Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add notes about closing streams after use (#3128) #3129

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/psr7.rst
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,12 @@ write-only, read-write, allow arbitrary random access (seeking forwards or
backwards to any location), or only allow sequential access (for example in the
case of a socket or pipe).

.. note::

When a stream is no-longer needed, the ``close()`` method should be called.
Guzzle will not close streams automatically when the request or response object
is destroyed.

Guzzle uses the ``guzzlehttp/psr7`` package to provide stream support. More
information on using streams, creating streams, converting streams to PHP
stream resource, and stream decorators can be found in the
Expand Down
22 changes: 20 additions & 2 deletions docs/request-options.rst
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,11 @@ This setting can be set to any of the following types:
// You can send requests that use a stream resource as the body.
$resource = \GuzzleHttp\Psr7\Utils::tryFopen('http://httpbin.org', 'r');
$client->request('PUT', '/put', ['body' => $resource]);
$resource->close();

.. note::

It is the responsibility of the calling code to close the resource.

- ``Psr\Http\Message\StreamInterface``

Expand All @@ -217,6 +222,12 @@ This setting can be set to any of the following types:
// You can send requests that use a Guzzle stream object as the body
$stream = GuzzleHttp\Psr7\Utils::streamFor('contents...');
$client->request('POST', '/post', ['body' => $stream]);
$stream->close();

.. note::

It is the responsibility of the calling code to close the stream.


.. note::

Expand Down Expand Up @@ -652,6 +663,8 @@ the following key value pairs:

use GuzzleHttp\Psr7;

$baz = Psr7\Utils::tryFopen('/path/to/file', 'r');
$qux = Psr7\Utils::tryFopen('/path/to/file', 'r');
$client->request('POST', '/post', [
'multipart' => [
[
Expand All @@ -661,16 +674,19 @@ the following key value pairs:
],
[
'name' => 'baz',
'contents' => Psr7\Utils::tryFopen('/path/to/file', 'r')
'contents' => $baz
],
[
'name' => 'qux',
'contents' => Psr7\Utils::tryFopen('/path/to/file', 'r'),
'contents' => $qux,
'filename' => 'custom_filename.txt'
],
]
]);

$baz->close();
$qux->close();

.. note::

``multipart`` cannot be used with the ``form_params`` option. You will need to
Expand All @@ -679,6 +695,8 @@ the following key value pairs:

This option cannot be used with ``body``, ``form_params``, or ``json``

If using a resource, or class implementing the `StreamInterface``, it is the responsibility of the calling code to close the resource.


.. _on-headers:

Expand Down