Guzzle return value, in parsing

Guzzle realized the PSR-7. This means that it will default message body is stored in the temporary use Stream PHP stream. To retrieve all the data, conversion operator may be used.

Example:
$client = new Client($this->getOptions());
$response = $client->request($method, $url, $options);

We can have two values ​​as follows:

$contents = (string) $response->getBody();
// or
$contents = $response->getBody()->getContents();

The difference between the two methods is the use of getContentsthe method is to return the rest of the content, so the second call does not return anything, unless you use the rewindmethod or find the location of the stream seekmethod will stream pointer rewind start position.

$stream = $response->getBody();
$contents = $stream->getContents(); // returns all the contents
$contents = $stream->getContents(); // empty string
$stream->rewind(); // Seek to the beginning
$contents = $stream->getContents(); // returns all the contents

Conversely, with PHP string conversion operation, it will read in all data streams from the beginning until the end.

$contents = (string) $response->getBody(); // returns all the contents

Documentation: http://docs.guzzlephp.org/en/latest/psr7.html#responses
Reference: https://stackoverflow.com/questions/30549226/guzzlehttp-how-get-the-body-of-a-response -from-guzzle-6

Published 41 original articles · won praise 21 · views 70000 +

Guess you like

Origin blog.csdn.net/u010324331/article/details/96485652