Posting JSON strings with Kohana 3.2 in PHP
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开发者_Python百科,
'var2' => $var2,
// etc
);
$json = json_encode($params);
Any help would be greatly appreciated.
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
精彩评论