Jasarien
Jasarien

Reputation: 58448

Reading Data From $_POST[] in PHP

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!

Upvotes: 2

Views: 820

Answers (3)

troelskn
troelskn

Reputation: 117427

If you want to read the raw request-body, you can use:

file_get_contents('php://input');

Upvotes: 1

Ayman Hourieh
Ayman Hourieh

Reputation: 137116

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

Upvotes: 5

Kekoa
Kekoa

Reputation: 28240

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.

Upvotes: -1

Related Questions