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

我怎样才能得到 body react ?

getBody 未返回 响应正文
echo '<pre>' . print_r($response->getBody(), true) . '</pre>';

GuzzleHttp\Psr7\Stream 对象
(
[流:GuzzleHttp\Psr7\Stream:private] => 资源 ID #80
[大小:GuzzleHttp\Psr7\Stream:private] =>
[可搜索:GuzzleHttp\Psr7\Stream:private] => 1
[可读:GuzzleHttp\Psr7\Stream:private] => 1
[可写:GuzzleHttp\Psr7\Stream:private] => 1
[uri:GuzzleHttp\Psr7\Stream:private] => php://temp
[customMetadata:GuzzleHttp\Psr7\Stream:private] => 数组
(
)

)

打印体 react 如何?

最佳答案

您可以使用 getContents 方法来拉出响应的正文。

$response = $this->client->get("url_path", [
                'headers' => ['Authorization' => 'Bearer ' . $my_token]
            ]);
$response_body = $response->getBody()->getContents();
print_r($response_body);

当您发出 make guzzle 请求时,您通常会将其放入 try catch 块中。此外,您还需要将该响应解码为 JSON,以便您可以像使用对象一样使用它。这是如何做到这一点的示例。在这种情况下,我正在使用服务器进行身份验证:
    try {

        $response = $this->client->post($my_authentication_path, [
            'headers' => ['Authorization' => 'Basic ' . $this->base_64_key,
                            'Content-Type' => 'application/x-www-form-urlencoded;charset=UTF-8'
                            ],
            'form_params'    => ['grant_type' => 'password',
                            'username' => 'my_user_name',
                            'password' => 'my_password']
        ]);

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

    } catch (GuzzleHttp\Exception\RequestException $e){
        $response_object = $e->getResponse();
        //log or print the error here.
        return false;
    } //end catch

    $authentication_response = json_decode($response_body);
    print_r($authentication_response);

关于php - 如何在 php guzzle 上获取响应正文?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32119856/

10-10 11:00