[英]Posting JSON strings with Kohana 3.2 in PHP
运行正常的后期操作时,我使用以下代码:
$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);
任何帮助将不胜感激。
我找到了这些解决方案。
使用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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.