0

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?

4

2 回答 2

1

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 with file_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:

string postData="fieldName="+System.Web.HttpUtility.UrlEncode("The data that goes in the field");

As always, if you need multiple fields you need to separate them with &s.

于 2012-04-26T23:23:33.590 回答
0

Those are just your Headers. You shouldn't expect your POST parameters in the result of getallheaders() call. You're sending something as the content-length suggest 54 bytes. Try var dumping $_REQUEST parameter, see if they're being submitted with another method.

于 2012-04-26T23:24:00.187 回答