Home » Php » php – How to read post request without knowing variables passed?

php – How to read post request without knowing variables passed?

Posted by: admin July 12, 2020 Leave a comment

Questions:

I’m trying to read a post request sent from a client using PHP whether they pass variables or not. What I want is to read the post data. I have tried using, the following without any luck:

echo file_get_contents('php://input');

I have tried sending the post request to http://posttestserver.com/ and the HTTP Post returns 200 and shows the post data sent to it.

How do I go about this using php?

How to&Answers:

You can read the post data from the $_POST variable. If you want to know which keys the array holds, use array_keys():

$postKeys = array_keys($_POST);

Alternatively, you could use foreach to scan the array:

foreach ($_POST as $key => $value) {
    echo "Key: $key; Value: $value\n";
}