Guzzlehttp-如何从Guzzle 6获得响应的主体?

我正在尝试为我公司正在开发的API编写包装器。这很安静,使用邮递员,我可以向http://subdomain.dev.myapi.com/api/v1/auth/用户端发送一个邮寄请求,例如使用用户名和密码作为POST数据,并且还给我一个令牌。所有工作均按预期进行。现在,当我尝试从PHP中进行操作时,我得到了一个GuzzleHttp\Psr7\Response对象,但似乎无法像在Postman请求中那样在其内部的任何地方找到令牌。

相关代码如下:

$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...

上面代码的输出看起来像(警告,文本输入墙):

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) {

}

}

}

Postman的输出如下:

{

"data" : {

"token" "fasdfasf-asfasdfasdf-sfasfasf"

}

}

显然,我缺少有关在Guzzle中使用响应对象的知识。Guzzle响应在请求中指示200状态代码,因此我不确定要检索返回的数据到底需要做什么。

回答:

食尸鬼执行PSR-7。这意味着默认情况下,它将在使用PHP临时流的Stream中存储消息的主体。要检索所有数据,可以使用强制转换运算符:

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

您也可以使用

$contents = $response->getBody()->getContents();

两种方法之间的区别是getContents返回剩余内容,因此第二次调用将不返回任何内容,除非您使用rewind或查找流的位置seek

$stream = $response->getBody();

$contents = $stream->getContents(); // returns all the contents

$contents = $stream->getContents(); // empty string

$stream->rewind(); // Seek to the beginning

$contents = $stream->getContents(); // returns all the contents

取而代之的是,使用PHP的字符串强制转换操作,它将从流中读取所有数据,从头到尾。

$contents = (string) $response->getBody(); // returns all the contents

$contents = (string) $response->getBody(); // returns all the contents

文档:http :

//docs.guzzlephp.org/en/latest/psr7.html#responses

以上是 Guzzlehttp-如何从Guzzle 6获得响应的主体? 的全部内容, 来源链接: utcz.com/qa/409302.html

回到顶部