Guzzlehttp - 如何从Guzzle 6获得响应的身体?
我正在尝试围绕我的公司正在开发的API编写一个包装器。这很安宁,使用Postman,我可以向端点发送一个post请求,例如使用用户名和密码作为POST数据,并且我得到了一个令牌。一切按预期工作。现在,当我尝试从PHP中做同样的事情时,我得到了一个对象,但似乎无法像我对Postman请求所做的那样在里面的任何地方找到令牌。http://subdomain.dev.myapi.com/api/v1/auth/
GuzzleHttp\Psr7\Response
相关代码如下所示:
$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 状态代码,因此我不确定我需要执行哪些操作来检索返回的数据。