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

Stream::getContents() unexpected behavior #38

Closed
slavcodev opened this issue Sep 14, 2015 · 4 comments
Closed

Stream::getContents() unexpected behavior #38

slavcodev opened this issue Sep 14, 2015 · 4 comments

Comments

@slavcodev
Copy link

Hello, I hit one problem when use instance of GuzzleHttp\Psr7\Stream.

echo $response->getBody()->getContents();

method returns me empty string, although the logger show me correct response.

The problem was that the logger use (string) $response->getBody() and we seek to the end of stream.

Should we seek back before returns content?

public function getContents()
{
    $this->rewind();
    $contents = stream_get_contents($this->stream);

    if ($contents === false) {
        throw new \RuntimeException('Unable to read stream contents');
    }

    return $contents;
}

Seems __toString() can call getContent().

@mtdowling
Copy link
Member

If you need to seek and store the entire stream in memory then cast the stream to a string using (string)

On Sep 14, 2015, at 8:49 AM, Veaceslav Medvedev notifications@github.com wrote:

Hello, I hit one problem when use instance of GuzzleHttp\Psr7\Stream.

echo $response->getBody()->getContents();
method returns me empty string, although the logger show me correct response.

The problem was that the logger use (string) $response->getBody() and we seek to the end of stream.

Should we seek back before returns content?

public function getContents()
{
$this->rewind();
$contents = stream_get_contents($this->stream);

if ($contents === false) {
    throw new \RuntimeException('Unable to read stream contents');
}

return $contents;

}
Seems __toString() can call getContent().


Reply to this email directly or view it on GitHub.

@slavcodev
Copy link
Author

Well, I think getContents meaning all body contents, not a part. To read body partial we have read($length). Or I''m wrong?

@mtdowling
Copy link
Member

@kenorb
Copy link

kenorb commented Sep 29, 2016

The $response->getBody() should be enough, however when calling it for the first time, you've to rewind it if you're planning to access content for the second time.

For example, I have called $response->getBody(), then when calling getContents();, the result was always empty. Doing $response->rewind(); in this case helped.

Related: Guzzlehttp - How get the body of a response from Guzzle 6?

$contents = (string) $response->getBody();

vs

$contents = $response->getBody()->getContents();

The difference between the two approaches is that getContents returns the remaining contents, so that a second call returns nothing unless you seek the position of the stream with rewind or seek .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants