Guzzle: How i can get body response?

Created on 20 Aug 2015  路  3Comments  路  Source: guzzle/guzzle

My code:

use GuzzleHttp\Client;
$client = new Client();
$response = $client->post('http://httpbin.org/post', array());

How i can get body response?

getBody not returned response body

echo '<pre>' . print_r($response->getBody(), true) . '</pre>';
GuzzleHttp\Psr7\Stream Object
(
    [stream:GuzzleHttp\Psr7\Stream:private] => Resource id #80
    [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
        (
        )

)

Most helpful comment

You can cast to string

// that will return whole content. seek(0) and then get content
echo '<pre>' . print_r((string)$response->getBody(), true) . '</pre>';

or call method

// this will return remaining content of the stream
echo '<pre>' . print_r($response->getBody()->getContents(), true) . '</pre>';

All 3 comments

You can cast to string

// that will return whole content. seek(0) and then get content
echo '<pre>' . print_r((string)$response->getBody(), true) . '</pre>';

or call method

// this will return remaining content of the stream
echo '<pre>' . print_r($response->getBody()->getContents(), true) . '</pre>';

Thank you!

Thank you! It worked like a charm! @kkopachev

Was this page helpful?
0 / 5 - 0 ratings