Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

t0berius's avatar

guzzle HTTP decode response

After I've consumed a API, I've decoded the guzzle HTTP response, the object looks like:

{#2149 ▼
  +"id": 0
  +"jsonrpc": "2.0"
  +"result": {#2146}
}

How can I get the content of result? It only shows this number, but how can I get deeper access into it? Guzzle code:

    try{
        $response = $this->client->request('POST', '/json_api', [
            'json' => [
                'jsonrpc'   => '2.0',
                'method'    => $method,
                'params'    => $params,
            ]
        ]);

        $rpcResult = json_decode($response->getBody());
    }

    catch(ClientException | ServerException $e){

        Log:error("error catch");

    }
    return $rpcResult->result;
0 likes
2 replies
petrit's avatar
$rpcResult->id;
$rpcResult->result;
$rpcResult->jsonrpc;

or, if you will have to iterate on results than

foreach($rpcResult->result as $result)
{
	...
}
t0berius's avatar

@petrit

This won't work directly on the GuzzleHttp\Psr7\Response object. Please check my code above, any why result only shows a "number"?

It's strange I've dived a bit deeper:

    try{
        $response = $this->client->request('POST', '/json_api,' [
            'json' => [
                'jsonrpc'   => '2.0',
                'method'    => $method,
                'params'    => $params,
            ]
        ]);
		
		//this shows correct "string"
        $body = $response->getBody();
        $stringBody = (string) $body;
        //dd($stringBody);

		//but here it shows the result field with strange numbers
        $rpcResult = json_decode($stringBody);
        dd($rpcResult);

Using this code I can see the correct response (only as string):

"""
{\r\n
  "id": 0,\r\n
  "jsonrpc": "2.0",\r\n
  "result": {\r\n
  }\r\n
}
"""

But the json decoded shows:

{#1474
  +"id": 0
  +"jsonrpc": "2.0"
  +"result": {#1471}
}

Please or to participate in this conversation.