php - Send json as request body or POST json in a variable -


i trying figure out, there way of sending raw json php instead of post parameter. if possible way best mean either request body or post parameter.

send http request has json string request body (here: using curl on command line):

$ curl -d '{"foo":"bar"}' example.com/test.php 

read request body in php:

$json = file_get_contents('php://input'); 

decode it:

$data = json_decode($json, true); 

php's $_post superglobal automatically populated array if request body of post request contains url encoded key-value pairs (e.g. foo=bar&baz=42). in above example you're still using http post request "post data". doesn't automatically end in $_post because php doesn't know how decode json automagically.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -