How to return Guzzle JSON response

I use Guzzle to create an aSync request that returns JSON. The call is working fine and the answer is ok, however:

$client = new Client();
    $promise = $client->requestAsync($requestType ,$this->url.$resource, // endpoint
        [
            'auth' => [ // credentials
                $this->username, 
                $this->password
            ],
            'json' => $payload, // the package
            'curl' => [ // some curl options
                CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
                CURLOPT_RETURNTRANSFER => true,
            ],
            'headers' => [ // custom headers
                'Accept' =>  'application/json',
                'Content-Type' => 'application/json'
            ]
        ]
    );

    $response = $promise->wait();
    echo $response->getStatusCode().'<br /><br />';
    // Error handling
    if($response->getStatusCode() != 200){
        // Error Handling
    }else{
        echo $response->getBody(true);
    }

if echo response-> getBody () I see a JSON string, but if I assign it to the print_r property or return it, I get:

GuzzleHttp\Psr7\Stream Object ( [stream:GuzzleHttp\Psr7\Stream:private] => Resource id #245 [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 ( ) )

I need to use JSON to check my response from the service. How can i do this? I went through the documents, but I obviously missed something.

Essentially, by destination json getBody output, to say $ json:

if($json->first_field > 0)

Any help appreciated. Relations

+4
source share
1 answer

After some more research on SO, I dropped my head in this post

Guzzle 6: no more json () method for answers

Essentially, the following returns the original output.

return $response->getBody()->getContents();

. , -.

+10

Source: https://habr.com/ru/post/1609551/


All Articles