如何使用 cakephp 4 从 Response 对象读取数据

发布于 2025-01-20 03:44:59 字数 853 浏览 4 评论 0 原文

我需要从包含此数据的 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。 为什么? 我怎样才能返回值?

I need to read some data from a http URL with contains this data:

{"result":[{"reg":408,"val":670,"mod":5}]}

I've done this in my Controller:

public function param()
{
    $http = new Client();
    $response = $http->get('my https');
    $json = $response->getJson();
    $this->set("json", "$json");
}

And in my php file:

<!DOCTYPE html>
<html>
<head>
    <title>Here the data</title>
</head>
<body>
     <td><?php  echo $json; ?></td>
</body>
</html>

But I receive the error:

Array to string conversion

in my Controller for the $this->set("json", "$json");.
And in the php, it returns Array.
Why?
How can I return the values?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

追我者格杀勿论 2025-01-27 03:44:59

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, or null.

If you want to pass the raw JSON data of the response, then you should use getStringBody() instead.

See also

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文