I’ve been trying to get some client-server interaction happening with JSON, however, the data that I post to the server is not arriving; the $_POST array in my PHP script is empty.
I’ve just ended up grabbing the WebRequest example straight from MSDN and testing that. That still doesn’t work. I’ve also tried just posting some data with HTML form, and that works fine. I’m completely stumped.
When I call getallheaders() in my PHP I get
OK
array(5) {
["Content-Type"]=>
string(33) "application/x-www-form-urlencoded"
["Host"]=>
string(9) "localhost"
["Content-Length"]=>
string(2) "54"
["Expect"]=>
string(12) "100-continue"
["Connection"]=>
string(10) "Keep-Alive"
though doing a var_dump($_POST) returns array(0)
Can anyone suggest anything?
The code in MSDN sends a raw string, so PHP doesn’t know how to brake it into fields and put it in
$_POST. You should be able to get that POST data withfile_get_contents('php://input').If you want the data to get to
$_POST, you need to encode it(the same way you encode it, the same way you encode GET parameters:As always, if you need multiple fields you need to separate them with
&s.