Guzzle 6, a popular HTTP client library for PHP, utilizes the PSR-7 standard which mandates the use of streams to store the body of a message. To retrieve this body as a string, utilize one of the following methods:
$contents = (string) $response->getBody();
$contents = $response->getBody()->getContents();
Key Difference:
For example:
$stream = $response->getBody(); $contents = $stream->getContents(); // returns all contents $contents = $stream->getContents(); // empty string $stream->rewind(); // reset stream position $contents = $stream->getContents(); // returns all contents again
In contrast:
$contents = (string) $response->getBody(); // returns all contents $contents = (string) $response->getBody(); // returns all contents again
For more details, refer to the Guzzle documentation: http://docs.guzzlephp.org/en/latest/psr7.html#responses
The above is the detailed content of How to Retrieve the Response Body in Guzzle 6?. For more information, please follow other related articles on the PHP Chinese website!