3

I'm using Guzzle to get an HTTP response. If I do this:

$response = $res->getBody();

I get an object with 'email' as one of the properties. But if I do either:

$email = $res->getBody()->email;

or

$email = $response->email

I get a 'No value for email' error. What am I missing?? How can I access a specific property in the response object?

daninthemix
  • 2,542
  • 8
  • 29
  • 40

1 Answers1

7

The getBody method returns an instance of StreamInterface. You first need to retrieve the contents of the response:

$response = (string) $res->getBody();

Only then you can decode the json payload:

$json = json_decode($response); 
$email = $json->email;
Community
  • 1
  • 1
Federkun
  • 36,084
  • 8
  • 78
  • 90