栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Guzzlehttp-如何从Guzzle 6获得响应的主体?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Guzzlehttp-如何从Guzzle 6获得响应的主体?

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



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/443009.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号