如何使用 cakephp 4 从 Response 对象读取数据
我需要从包含此数据的 http URL 读取一些数据:
{"result":[{"reg":408,"val":670,"mod":5}]}
我已经在我的控制器中完成了此操作:
public function param()
{
$http = new Client();
$response = $http->get('my https');
$json = $response->getJson();
$this->set("json", "$json");
}
在我的 php 文件中:
<!DOCTYPE html>
<html>
<head>
<title>Here the data</title>
</head>
<body>
<td><?php echo $json; ?></td>
</body>
</html>
但我收到错误:
数组到字符串的转换
我的控制器中 $this->set("json", "$json");
的 。
在 php 中,它返回Array
。
为什么?
我怎样才能返回值?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
getjson()
不返回原始JSON,而是 decoded JSON数据,因此它可以是一个数组,字符串,数字,一个,布尔值或null
。如果要传递响应的原始JSON数据,则应改用
gets -getStringBody()
。另请参见
getJson()
doesn't return the raw JSON, but the decoded JSON data, so it could be an array, a string, a number, a boolean, ornull
.If you want to pass the raw JSON data of the response, then you should use
getStringBody()
instead.See also