Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return Guzzle JSON response

Tags:

php

guzzle

I am using Guzzle to make a aSync request that returns JSON. The call is working fine and the response 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 I echo response->getBody() I see the JSON string, but if I assign it to a property, print_r, 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 the JSON to validate my response from the service. How can I do this? I have gone through the docs, but am I obviously missing something.

Essentially along the lines of assigning the json getBody output to say $json:

if($json->first_field > 0)

Any Help appreciated. Regards

like image 693
Dbrandt Avatar asked Sep 30 '15 03:09

Dbrandt


People also ask

How do you get a response from guzzle?

As described earlier, you can get the body of a response using the getBody() method. Guzzle uses the json_decode() method of PHP and uses arrays rather than stdClass objects for objects. You can use the xml() method when working with XML data.

How do I get my status code from response guzzle?

You can use the getStatusCode function. $response = $client->request('GET', $url); $statusCode = $response->getStatusCode(); Note: If your URL redirects to some other URL then you need to set false value for allow_redirects property to be able to detect initial status code for parent URL. Save this answer.

How do I send HTTP request using guzzle?

Sending Requests You can create a request and then send the request with the client when you're ready: use GuzzleHttp\Psr7\Request; $request = new Request('PUT', 'http://httpbin.org/put'); $response = $client->send($request, ['timeout' => 2]);

Does guzzle use cURL?

Guzzle has historically only utilized cURL to send HTTP requests. cURL is an amazing HTTP client (arguably the best), and Guzzle will continue to use it by default when it is available. It is rare, but some developers don't have cURL installed on their systems or run into version specific issues.


1 Answers

After some more research on SO I tumbled head first into this post

Guzzle 6: no more json() method for responses

Essentially doing the following will return the raw output.

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

Huge headache gone. Hope this helps someone

like image 119
Dbrandt Avatar answered Sep 28 '22 07:09

Dbrandt