食尸鬼执行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



