Getting JSON POST

Possible duplicate:
How to get POST body in php?

I get a POST that contains JSON, the problem is that I get that $ _POST is empty. To check when I get POST, I create a file that contains $ _POST, $ _GET and $ _REQUEST, and they are all empty.

The client that sends the request does something like this:

$itemJson = '{ "id": 00, "value": "ok" }'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json', 'Content-Length: '. strlen($itemJson)) ); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_POSTFIELDS, $itemJson); curl_close($ch); 

Honestly, I do not understand how I get this data, since there are no parameters.

Any ideas?

+4
source share
2 answers

Try it in PHP, as shown below (when the request is an application / json, then you will not receive the data in $ _POST)

 var_dump(json_decode(file_get_contents("php://input"))); 
+17
source

Try

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

in your reception script.

see also: http://docs.php.net/wrappers.php.php

+5
source

Source: https://habr.com/ru/post/1441826/


All Articles