Symfony 2 - Guzzle 6.X HTTP |获得 body 响应

Symfony 2 - Guzzle 6.X HTTP | get the body response

我在 Symfony 2.7.3 中使用 Guzzle,我不知道为什么我的响应是 header 而不是 body。 (我在本地主机上使用 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);

所以我得到了:

但是流是空的,而我应该得到一个令牌(你可以看到 content-length : 69)

你能帮帮我吗,我不知道我错过了...

(服务器只接受POST获取token)

因为它是 json 响应,您应该对其进行解码,添加:

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

true表示返回的对象会转为关联数组

通过调用 $response->getBody() 您可以获得一个 GuzzleHttp\Psr7\Stream 对象。 这个 class 有一个有用的 __toString() 方法,所以下面一行将按预期工作:

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