1

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?

2
  • Maybe try to post a relevant piece of C# code, so we can see where's the problem. For now I can say just one thing - you're obviously doing "something" wrong :) Commented Apr 26, 2012 at 23:12
  • You could setup fiddler as a proxy for your request, that way you can see whats gets transferred, and if its well formed, or show us the code. Commented Apr 26, 2012 at 23:24

2 Answers 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.

Sign up to request clarification or add additional context in comments.

1 Comment

Idan, you're an absolute legend. Worked like a charm, and great explanation. Thanks!
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.

1 Comment

Currently I'm using the example code from the MSDN link above, no changes

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.