19

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.

Josh Darnell
  • 11,304
  • 9
  • 38
  • 66
Mick F
  • 7,312
  • 6
  • 51
  • 98

3 Answers3

63
$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)

salathe
  • 51,324
  • 12
  • 104
  • 132
  • 4
    One thing to note: i was posting to my website such as www.example.com/test and i could not get php://input to work. It turns out i had to post to www.example.com/test/index.php – AlBeebe Feb 09 '11 at 07:18
  • I just needed that again today and I had the opportunity to use this trick. Very efficient. Thanks again. – Mick F Nov 18 '11 at 21:27
3

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;
Anax
  • 9,122
  • 5
  • 34
  • 68
  • Sorry for the late answer but I've convinced my client to use a GET method. But I'll keep all the answers here in mind for any future need ! Thanks everyone ! – Mick F Aug 06 '10 at 16:29
0

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

Try this

<?php
var_dump($_POST);

and see what happens.

middus
  • 9,103
  • 1
  • 31
  • 33