Guzzlehttp - 如何从Guzzle 6获得回复?

Gre*_*eg 140 php response guzzle guzzle6

我正在尝试围绕我公司正在开发的api编写一个包装器.它很安静,使用Postman我可以发送一个帖子请求到一个端点,比如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...
Run Code Online (Sandbox Code Playgroud)

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

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) {
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

邮差的输出是这样的:

{
    "data" : {
        "token" "fasdfasf-asfasdfasdf-sfasfasf"
    }
}
Run Code Online (Sandbox Code Playgroud)

显然,我错过了在Guzzle中处理响应对象的一些事情.Guzzle响应指示请求上有200个状态代码,因此我不确定我需要做什么才能检索返回的数据.

Fed*_*kun 387

Guzzle实现了PSR-7.这意味着它将默认将消息正文存储在使用PHP临时流的Stream中.要检索所有数据,可以使用cast运算符:

$contents = (string) $response->getBody();
Run Code Online (Sandbox Code Playgroud)

你也可以这样做

$contents = $response->getBody()->getContents();
Run Code Online (Sandbox Code Playgroud)

两种方法之间的区别在于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
Run Code Online (Sandbox Code Playgroud)

相反,使用PHP的字符串转换操作,它将从开头读取流中的所有数据,直到达到结束.

$contents = (string) $response->getBody(); // returns all the contents
$contents = (string) $response->getBody(); // returns all the contents
Run Code Online (Sandbox Code Playgroud)

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

  • 谢谢.令人难以置信的是,文档中的内容并不明确.甚至他们的官方文档(http://docs.guzzlephp.org/en/latest/)也让人觉得调用$ res-> getBody()会返回你通常所期望的内容. (52认同)
  • 他们应该在官方文件中加入类似注释或通知的内容.我在这个问题上浪费了两天时间. (22认同)
  • 您也可以使用json_decode.例如,将您的响应包装在`json_decode($ response,true);`这将返回一个数组. (7认同)
  • getContents函数只是Guzzle 6文档的一小部分(在流部分),我错过了它.你从大量的搜索中拯救了我. (5认同)
  • 强烈建议您不要使用 (string) $response->getBody() 因为它实际上并不检索所有数据。它检索默认值的长度,因此它可以截断实际值。https://github.com/guzzle/guzzle/issues/1610 (2认同)

Mak*_*nov 26

如果期望返回 JSON,获取它的最简单方法是:

$data = json_decode($response->getBody()); // returns an object

// OR

$data = json_decode($response->getBody(), true); // returns an array
Run Code Online (Sandbox Code Playgroud)

json_decode()会自动投身到string,所以没有必要调用getContents()

  • 为什么这个答案没有得到更多关注???这正是我所需要的。谢谢@MaskimIvanov (3认同)
  • 这个简单的答案“解决”了眼前的问题,但它忽略了@Federkun 接受的答案的细微差别。大多数时候这种“正常工作”的原因是因为对“json_decode”的调用**隐式地将**响应主体转换为字符串。话虽如此,理解接受的答案是值得的,因为字符串转换方法可能存在问题,并不总是检索所有数据,正如 @Shardj 的评论中强调的那样。 (2认同)
  • 这种方法可行,但是在静态分析时会出现错误 (2认同)

Bha*_*iya 13

对于获取 JSON 格式的响应:

1.

$response = (string) $res->getBody();
$response =json_decode($response); // Using this you can access any key like below
$key_value = $response->key_name; //access key  
Run Code Online (Sandbox Code Playgroud)
$response = json_decode($res->getBody(),true);
$key_value =   $response['key_name'];//access key
Run Code Online (Sandbox Code Playgroud)