【问题标题】:Posting JSON strings with Kohana 3.2 in PHP在 PHP 中使用 Kohana 3.2 发布 JSON 字符串
【发布时间】:2011-11-21 21:40:03
【问题描述】:

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

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

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

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

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

$json = json_encode($params);

任何帮助将不胜感激。

【问题讨论】:

    标签: php json post kohana


    【解决方案1】:

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

    使用 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/guide/kohana/requests#external-requests

    【讨论】:

    • Request::PUT 应该不是必需的。 PHP 只知道 POST 或 GET。您只需要设置响应标头,以便浏览器将其获取为 JSON。如果您想使用例如 XML 或 PDF,则需要设置相同的标题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-23
    • 2018-05-08
    • 2016-03-25
    • 2011-03-06
    • 1970-01-01
    • 2015-04-06
    相关资源
    最近更新 更多