Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing specific properties from Guzzle response?

Tags:

php

guzzle

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?

like image 729
daninthemix Avatar asked Jan 23 '26 14:01

daninthemix


1 Answers

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;
like image 192
Federkun Avatar answered Jan 25 '26 04:01

Federkun



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!