Guzzle 返回值取值解析

Guzzle实现了PSR-7。 这意味着它将默认将消息正文存储在使用PHP临时流的Stream中。 要检索所有数据,可以使用类型转换操作符。

示例:
$client = new Client($this->getOptions());
$response = $client->request($method, $url, $options);

我们可以有两种取值方式如下:

$contents = (string) $response->getBody();
// or
$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

文档:http://docs.guzzlephp.org/en/latest/psr7.html#responses
参考:https://stackoverflow.com/questions/30549226/guzzlehttp-how-get-the-body-of-a-response-from-guzzle-6

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值