0

I'm using a u-blox gsm module (potentially irrelevant) to send POST commands to a server. I'm using http:/httpbin.org(/post) to echo back the POST information that Im sending. The result is

...

"args": {},
"data": "Hello World",
"files": {},
"form": {},

...

Thus suggesting that it's just data being received and not a file or parts of a form.

I've tried to use

<?php

  echo "THIS IS THE SERVER RESPONSE:\n\n";
  if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    print_r($_POST);
  }
?>

but got an empty array back

THIS IS THE SERVER RESPONSE:

Array
(
)

What is the correct PHP command to use to echo the "data" back to the user?

Note: obviously in a real situation you wouldn't echo arbitrary data back to the user, that isn't the point here.

Hazza_ob
  • 27
  • 8
  • 1
    try to `var_dump` this: `file_get_contents("php://input")`. – FirstOne Dec 18 '17 at 18:07
  • 1
    Yes, `$_POST` could be empty provided the content type isn't `application/x-www-form-urlencoded` or `multipart/form-data-encoded` – Kisaragi Dec 18 '17 at 18:09
  • @FirstOne `var_dump` returns `string(11) "Hello World"` - which is great! Is this common practice or are there better ways of integrating this? – Hazza_ob Dec 18 '17 at 18:14
  • It's mentioned in @Kisaragi's comment, but read this: [PHP “php://input” vs $_POST](https://stackoverflow.com/questions/8893574/php-php-input-vs-post). – FirstOne Dec 18 '17 at 18:15
  • Basically, PHP will only parse data into `$_POST` when said header is passed. – FirstOne Dec 18 '17 at 18:15

0 Answers0