在 PHP 中使用 Kohana 3.2 发布 JSON 字符串

发布于 2024-12-05 14:24:13 字数 433 浏览 4 评论 0原文

运行正常的 post 操作时,我使用以下代码:

 $request = Request::factory($url)->method(Request::POST)->post($params);
 $response = $request->execute();

我不确定需要更改什么才能使我能够 POST json 字符串而不是数组变量。

我的 json 字符串基本上是使用参数数组上的 json_encode() 函数创建的,如下所示:

$params = array(
    'var1' => $var1,
    'var2' => $var2,
    // etc
);

$json = json_encode($params);

任何帮助将不胜感激。

When running normal post operations I use the following code:

 $request = Request::factory($url)->method(Request::POST)->post($params);
 $response = $request->execute();

I'm not sure what it is I need to change though to enable me to POST a json string instead of an array variable.

My json string is basically created using the json_encode() function on an array of parameters, like so:

$params = array(
    'var1' => $var1,
    'var2' => $var2,
    // etc
);

$json = json_encode($params);

Any help would be greatly appreciated.

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

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

发布评论

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

评论(1

风吹短裙飘 2024-12-12 14:24:13

我已经找到了这些解决方案。

使用 PUT:

$request = Request::factory('http://example.com/put_api')->method(Request::PUT)->body(json_encode('the body'))->headers('Content-Type', 'application/json');

使用 POST:

$request = Request::factory('http://example.com/post_api')->method(Request::POST)->body(json_encode('the body'))->headers('Content-Type', 'application/json');

从这里: http://kohanaframework.org/3.2/指南/kohana/requests#external-requests

I have found these solutions.

Using PUT:

$request = Request::factory('http://example.com/put_api')->method(Request::PUT)->body(json_encode('the body'))->headers('Content-Type', 'application/json');

Using POST:

$request = Request::factory('http://example.com/post_api')->method(Request::POST)->body(json_encode('the body'))->headers('Content-Type', 'application/json');

From here: http://kohanaframework.org/3.2/guide/kohana/requests#external-requests

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