1

I am using Guzzle with Symfony 2.7.3 and I don't know why I have the header of the response but not the body. (I am on localhost with WAMP)

$donnees = array(// Base URI is used with relative requests
            'base_uri' => $urlAuth,
            // You can set any number of default request options.
            'timeout'  => 2.0,
            'headers' => [
                'User-Agent' => 'testing/1.0',
                'Accept'     => 'application/json'
            ],
            'verify' => false,
            'json'      => ["Id" => $Id, 
                                                "Username" => $username,
                                                "Password" => $password,
                                                "SecretId" => $secretId]
            );

        $client = new Client($donnees);

        $response = $client->post( '/auth/', $donnees );
dump($response);

so I got :

enter image description here

But stream is empty whereas I should get a token (you can see content-length : 69)

Can you help me, I don't know that I missed...

(The server only accept POST to get the token)

4

2 回答 2

2

As it's json response you should decode it, add:

$response_body = json_decode($response->getBody(), true);

true means that returned objects will be converted into associative arrays.

于 2015-08-31T13:52:27.623 回答
1

By calling $response->getBody() you obtain a GuzzleHttp\Psr7\Stream object. This class has a useful __toString() method, so the following line will work as expected :

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

于 2016-03-30T14:41:58.873 回答