php-guzzleHow to get the response body in PHP Guzzle?
The response body in PHP Guzzle can be obtained using the getBody()
method.
Example code
$client = new \GuzzleHttp\Client();
$response = $client->request('GET', 'http://example.com');
$body = $response->getBody();
Output example
GuzzleHttp\Psr7\Stream Object
(
[stream:GuzzleHttp\Psr7\Stream:private] => Resource id #3
[size:GuzzleHttp\Psr7\Stream:private] =>
[seekable:GuzzleHttp\Psr7\Stream:private] => 1
[readable:GuzzleHttp\Psr7\Stream:private] => 1
[writable:GuzzleHttp\Psr7\Stream:private] => 1
[uri:GuzzleHttp\Psr7\Stream:private] => php://temp
[customMetadata:GuzzleHttp\Psr7\Stream:private] => Array
(
)
)
Code explanation
$client = new \GuzzleHttp\Client();
- creates a new Guzzle client$response = $client->request('GET', 'http://example.com');
- sends a GET request to the specified URL$body = $response->getBody();
- gets the response body
Helpful links
More of Php Guzzle
- How to use PHP Guzzle to make a batch request?
- How to set a user agent in PHP Guzzle?
- How to post form data with PHP Guzzle?
- PHP Guzzle get request example
- How to add an authorization header bearer in PHP Guzzle?
- How to set a timeout for a request with PHP Guzzle?
- How to convert a response to an array with PHP Guzzle?
- How to use Promises with PHP Guzzle (with an example)?
- How to send multipart requests with Guzzle in PHP?
- How to download an image with Guzzle in PHP?
See more codes...