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.
Getting HTTP POST Body as a String To retrieve the body of the POST request sent to the handler, we'll use the @RequestBody annotation, and assign its value to a String.
PHP $_POST is a PHP super global variable which is used to collect form data after submitting an HTML form with method="post". $_POST is also widely used to pass variables. The example below shows a form with an input field and a submit button.
POST does require a body, but that body can be an empty document. The difference is subtle, but it's not the same thing.
To make a POST request to an API endpoint, you need to send an HTTP POST request to the server and specify a Content-Type request header that specifies the data media type in the body of the POST request. The Content-Length header indicates the size of the data in the body of the POST request.
$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)
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With