Guzzle实施PSR-7。这意味着它将默认将消息正文存储在使用PHP临时流的Stream中。要检索所有数据,可以使用casting操作符:
$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