Friday, May 6, 2011

Reading Data From $_POST[] in PHP

Hey guys,

I'm trying to read POST data in a PHP script.

My first instincts led me to the $_POST[] array, but for whatever reason, it's never populated.

I'm using HTTP Client for OS X, http://ditchnet.org/httpclient/ to send POST requests. I enter the URL of the script, set the method to POST, set the content-type header to text/plain and then enter myVar=foobar as the body of the request.

I hit send, and there's nothing in the $_POST[] array.

I tried another route after reading some questions here on StackOverflow, and tried reading from the $HTTP_RAW_POST_DATA, but no dice there either. I also tried reading from the php://input stream, and nothing.

As a heads up, I'm able to read from the $_GET[] array if I add some parameters to the URL, but I don't want to do that.

I also need to post the data from a different application, so I can't use the HTML post forms...

Any help would be awesome, thanks guys!

From stackoverflow
  • Check to see if post works from a browser... I would suspect the HTTP client you're using is not sending the request properly.

    The GET request is considerably simpler than a multipart POST request.

  • Set the Content-Type header to application/x-www-form-urlencoded. text/plain is not a valid content type for post data.

    Jasarien : Thanks! That was exactly it!
  • If you want to read the raw request-body, you can use:

    file_get_contents('php://input');
    
    Kris : I really like that method, even though it's almost never actually required.

0 comments:

Post a Comment