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
getContents
the method is to return the rest of the content, so the second call does not return anything, unless you use therewind
method or find the location of the streamseek
method 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