Guzzlehttp - How Get the Body of a Response from Guzzle 6

Guzzlehttp - How get the body of a response from Guzzle 6?

Guzzle implements PSR-7. That means that it will by default store the body of a message in a Stream that uses PHP temp streams. To retrieve all the data, you can use casting operator:

$contents = (string) $response->getBody();

You can also do it with

$contents = $response->getBody()->getContents();

The difference between the two approaches is that getContents returns the remaining contents, so that a second call returns nothing unless you seek the position of the stream with rewind or seek .

$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

Instead, usings PHP's string casting operations, it will reads all the data from the stream from the beginning until the end is reached.

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

Documentation: http://docs.guzzlephp.org/en/latest/psr7.html#responses

How can I get response from guzzle 6 in Laravel 5.3?

I see at least one syntax mistake. The third argument of the request() method should look like this:

$requestContent = [
'headers' = [],
'json' = []
];

In your case it could be:

public function testApi()
{
$requestContent = [
'headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/json'
],
'json' => [
'email' => 'test@gmail.com',
'password' => '1234',
// 'debug' => true
]
];

try {
$client = new GuzzleHttpClient();

$apiRequest = $client->request('POST', 'https://myshop/api/auth/login', $requestContent);

$response = json_decode($apiRequest->getBody());

dd($response);
} catch (RequestException $re) {
// For handling exception.
}
}

There are other parameters instead of json for your data, for example form_params. I suggest you take a look at the Guzzle documentation.

Assign getBody() to a Variable Guzzle

The response is a stream, cast it:

$contents = (string)$response->getBody();

How to get body in Guzzle?

Your solution:

$res->getBody()->getContents();

Take a look at this answer for details.

guzzle 6.+ send request with header and body

You should use form_params instead. body takes a raw byte sequence.

Setting request body for Guzzle 6 put()

Of course the part I think is irrelevant is the answer: I did not show that I was using json_encode() on $data before passing it. The array, not a json object string, was expected so with $data as an array this works correctly.

$response = $client->put("/api/v1/Functions/10/".$_SESSION['eventID'].'/'.$functionID, 
array(
'headers' => array(
'token' => $token,
'debug' => true
),
'json' => $data
)
);

Retrieve the whole XML response body with Guzzle 6 HTTP Client

The GuzzleHttp\Psr7\Stream implemtents the contract of Psr\Http\Message\StreamInterface which has the following to offer to you:

/** @var $body GuzzleHttp\Psr7\Stream */
$contents = (string) $body;

Casting the object to string will call the underlying __toString() method which is part of the interface. The method name __toString() is special in PHP.

As the implementation within GuzzleHttp "missed" to provide access to the actual stream handle, so you can't make use of PHP's stream functions which allows more "stream-lined" (stream-like) operations under circumstances, like stream_copy_to_stream, stream_get_contents or file_put_contents. This might not be obvious on first sight.



Related Topics



Leave a reply



Submit