0

I have the following response from jsonRPC endpoint:

{
    "jsonrpc": "2.0",
    "result": {
        "jwt": "EXAMPLE"
    }
}

When I log $response->getRpcResult() I get

{
        "jwt": "EXAMPLE"
    }

When I try taking the value of jwt using $token = $response->getRpcResult()["jwt"]

I get phP message: PHP Warning: Illegal string offset of jwt

Any help is appreciated, I'm a beginner in PHP.

Melchia
  • 22,578
  • 22
  • 103
  • 117
  • It should work (tested with your endpoint output and worked for me). Maybe the response isn't really that? Can you please share more of your code or the endpoint itself to verify the response? – Ofir Baruch Sep 13 '21 at 18:47
  • You should examine the result of `$response->getRpcResult()` - the error message is telling you there's no key with a value of `jwt` in the result array. – Preston Guillot Sep 13 '21 at 19:07
  • I'll check again. I'll be right asap – Melchia Sep 13 '21 at 19:09
  • Looks like you got JSON, and still need to decode it. – CBroe Sep 14 '21 at 07:15

2 Answers2

0

No need for getRpcResult(). Just this should do:

$token = $response['jwt'];
dump($token);
Dennis Kozevnikoff
  • 2,078
  • 3
  • 19
  • 29
0

I was able to solve it using:

$token = json_decode(json_encode($response->getRpcResult()), true)["jwt"];

I have no idea why I need to decode it again.

Melchia
  • 22,578
  • 22
  • 103
  • 117