I can't read my POST HTTP request's body with PHP !

I’ve never used PHP but right now, I need to write a PHP file that displays in a log file the content of the body of a POST HTTP request.

I’ve read that you can access variables of the body via the _POST array.
Unfortunately, it seems to be empty, although I’m pretty sure there is stuff in my HTTP request’s body !

What should I use to be 100% sure of the content of my HTTP body ?

Thanks.

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

$post_body = file_get_contents('php://input');

php://input allows you to read raw POST data. It is a less memory intensive alternative to $HTTP_RAW_POST_DATA and does not need any special php.ini directives. php://input is not available with enctype=”multipart/form-data”.

(Source: http://php.net/wrappers.php)

Solution 2

The global variable is $_POST, not _POST. Also it might be that you are sending the data via GET method, in which case you need to use the $_GET global variable.

If you want to check for either POST or GET method, you can use the global variable $_REQUEST. Sample code bellow:

<html>
<body>
<form method="POST" action="postdata.php">
<input type="text" name="mydata" />
<input type="submit">
</form>
</body>
</html>

file postdata.php:

<?php

$result = $_POST['mydata'];
echo $result;

Solution 3

Maybe you misspelled it. The array’s correct name $_POST.

Try this

<?php
var_dump($_POST);

and see what happens.

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply