LMSouq
general-dev Open

Guzzlehttp - How get the body of a response from Guzzle 6?

GR
Greg
1 month ago
3 views
Problem Description
I'm trying to write a wrapper around an api my company is developing. It's restful, and using Postman I can send a post request to an endpoint like `http://subdomain.dev.myapi.com/api/v1/auth/` with a username and password as POST data and I am given back a token. All works as expected. Now, when I try and do the same from PHP I get back a `GuzzleHttp\Psr7\Response` object, but can't seem to find the token anywhere inside it as I did with the Postman request. The relevant code looks like: $client = new Client(['base_uri' => 'http://companysub.dev.myapi.com/']); $response = $client->post('api/v1/auth/', [ 'form_params' => [ 'username' => $user, 'password' => $password ] ]); var_dump($response); //or $resonse->getBody(), etc... The output of the code above looks something like (warning, incoming wall of text): object(guzzlehttp\psr7\response)#36 (6) { ["reasonphrase":"guzzlehttp\psr7\response":private]=> string(2) "ok" ["statuscode":"guzzlehttp\psr7\response":private]=> int(200) ["headers":"guzzlehttp\psr7\response":private]=> array(9) { ["connection"]=> array(1) { [0]=> string(10) "keep-alive" } ["server"]=> array(1) { [0]=> string(15) "gunicorn/19.3.0" } ["date"]=> array(1) { [0]=> string(29) "sat, 30 may 2015 17:22:41 gmt" } ["transfer-encoding"]=> array(1) { [0]=> string(7) "chunked" } ["content-type"]=> array(1) { [0]=> string(16) "application/json" } ["allow"]=> array(1) { [0]=> string(13) "post, options" } ["x-frame-options"]=> array(1) { [0]=> string(10) "sameorigin" } ["vary"]=> array(1) { [0]=> string(12) "cookie, host" } ["via"]=> array(1) { [0]=> string(9) "1.1 vegur" } } ["headerlines":"guzzlehttp\psr7\response":private]=> array(9) { ["connection"]=> array(1) { [0]=> string(10) "keep-alive" } ["server"]=> array(1) { [0]=> string(15) "gunicorn/19.3.0" } ["date"]=> array(1)...

AI-Generated Solution

Powered by LMSouq AI · GPT-4.1-mini

✓ Solution Ready
Analyzing problem and generating solution…
Was this solution helpful?
Back to Knowledge Base