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


i'm trying write wrapper around api company developing. it's restful, , using postman can send post request endpoint http://subdomain.dev.myapi.com/api/v1/auth/ username , password post data , given token. works expected. now, when try , same php guzzlehttp\psr7\response object, can't seem find token anywhere inside did 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 code above looks (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) {       [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"     }   }   ["protocol":"guzzlehttp\psr7\response":private]=>   string(3) "1.1"   ["stream":"guzzlehttp\psr7\response":private]=>   object(guzzlehttp\psr7\stream)#27 (7) {     ["stream":"guzzlehttp\psr7\stream":private]=>     resource(40) of type (stream)     ["size":"guzzlehttp\psr7\stream":private]=>     null     ["seekable":"guzzlehttp\psr7\stream":private]=>     bool(true)     ["readable":"guzzlehttp\psr7\stream":private]=>     bool(true)     ["writable":"guzzlehttp\psr7\stream":private]=>     bool(true)     ["uri":"guzzlehttp\psr7\stream":private]=>     string(10) "php://temp"     ["custommetadata":"guzzlehttp\psr7\stream":private]=>     array(0) {     }   } } 

the output postman like:

{     "data" : {         "token" "fasdfasf-asfasdfasdf-sfasfasf"     } } 

clearly i'm missing working response objects in guzzle. guzzle response indicates 200 status code on request, i'm not sure need retrieve returned data.

guzzle implements psr-7. means default store body of message in stream uses php temp streams. retrieve data, can use casting operator:

$contents = (string) $response->getbody(); 

you can with

$contents = $response->getbody()->getcontents(); 

the difference between 2 approaches getcontents returns remaining contents, second call returns nothing unless seek position of stream rewind or seek .

$stream = $response->getbody(); $contents = $stream->getcontents(); // returns contents $contents = $stream->getcontents(); // empty string $stream->rewind(); // seek beginning $contents = $stream->getcontents(); // returns contents 

instead, usings php's string casting operations, reads data stream beginning until end reached.

$contents = (string) $response->getbody(); // returns contents $contents = (string) $response->getbody(); // returns contents 

documentation: http://docs.guzzlephp.org/en/latest/psr7.html#responses


Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -