在php中获取没有变量的帖子数据

if i have a post request that looks like this:

POST /page.php HTTP/1.1
Host: www.example.com
...
...


{"name":"json"}

notice that post data is sent without a variable name.. is there a way of fetching this?

p.s. tried dumping $_POST, didn't help at all..

Thanks!

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 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"
}
*/

You need content-type header for json interprete:

POST /page.php HTTP/1.1
Host: www.example.com
Content-Type: application/json
...
...


{"name":"json"}

And now use $_POST['name']