getting post data with no variables in php
if i have a post request that looks like this:
POST /page.php HTTP/1.1
Host: www.example.com
...
...\r\n\r\n
{"name":"json"}
notice that post data is sent without a variable name开发者_C百科.. is there a way of fetching this?
p.s. tried dumping $_POST, didn't help at all..
Thanks!
You can get the POST body via php://input
:
$json_object = json_decode(file_get_contents('php://input'));
/*
object(stdClass)#1 (1) {
["name"]=>
string(4) "json"
}
*/
Have you tried dumping $HTTP_RAW_POST_DATA
? Because I'll bet that you can find it in there.
Note: per the comment, this is no longer the correct behavior with the most recent versions of PHP. You are better off reading the content of 'php://input'
You can read more about that here.
You need content-type header for json interprete:
POST /page.php HTTP/1.1
Host: www.example.com
Content-Type: application/json
...
...\r\n\r\n
{"name":"json"}
And now use $_POST['name']
精彩评论