4

File is running in apache server and when a request was made from postman it showing empty input data below is the code

$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
     die("Connection failed: " . $conn->connect_error);
}
echo "I am here";
$tempval = file_get_contents('php://input');
//$postdata = json_decode(file_get_contents('php://input'), true);
echo "Data ".$tempval;

Output for this file

I am hereData

which means that tempval is empty i.e no data is recieved from the request

Screen shot of postman request

Phil
  • 157,677
  • 23
  • 242
  • 245
  • Welcome to SO. Please read: [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) and also [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask) - Currently, this question lacks enough information for us to be able to know what your code is actually doing. Please show us all relevant code (and any htaccess file, if you have any). – M. Eriksson Aug 22 '18 at 06:08
  • 1
    Are you printing the data? For example, `echo file_get_contents('php://input');` Just running file_get_contents won't print anything to the screen. – Dave Chen Aug 22 '18 at 06:11
  • I tried printing data it isnt giving any output – Murali Krishnamraju Aug 22 '18 at 06:13
  • @DavidChen In the above code I even tried printing $tempval still it shows empty – Murali Krishnamraju Aug 22 '18 at 06:14
  • Are you sure you're sending POST or GET data using postman? Can you try `print_r($_REQUEST)`? Also note that you cannot use php://input with `enctype="multipart/form-data"`. – Dave Chen Aug 22 '18 at 06:16
  • I am sending it using postman POST request and data input type is raw data – Murali Krishnamraju Aug 22 '18 at 06:17
  • @DavidChen I tried printing print_r(_$REQUEST) it is printing Array () – Murali Krishnamraju Aug 22 '18 at 06:19
  • Can you show how you're sending your request with postman? Take a screenshot of your postman. – Dave Chen Aug 22 '18 at 06:21
  • 1
    read this: https://stackoverflow.com/a/8893758/454827 – ZiTAL Aug 22 '18 at 06:21
  • @DavidChen screenshot sent to your mail please check – Murali Krishnamraju Aug 22 '18 at 06:28
  • Just a helpful advice. Don't use `echo "I am here";`, etc. Get yourself an IDE with a debugger (such as [Eclipse](https://www.eclipse.org/pdt/) (there are plenty of others, but that one is pretty much "industry standard"), and learn how to debug. You can set a “breakpoint” on a line and run your code until that line is reached. At that point, you can see the values of variables (and change them), see the “call stack” (which function called the function …. That called the current function, plus their parameters); you can step through your code, line by line, and more. Make that a habit – Mawg says reinstate Monica Aug 22 '18 at 06:28
  • @MuraliKrishnamraju Can you change the data type to `JSON (application/json)`? See this screenshot https://i.stack.imgur.com/RHEih.png – Dave Chen Aug 22 '18 at 06:30
  • @DavidChen yeah I tried that way as well it still shows a empty request – Murali Krishnamraju Aug 22 '18 at 06:33
  • 1
    Have you made sure you can see any errors that might be reported? See [How to get useful error messages in PHP?](https://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php) – Phil Aug 22 '18 at 06:34
  • Can't help but notice the URL in your screenshot looks a little odd. Instead of `localhost/postmasterstorelogs.php/`, try `http://localhost/postmasterstorelogs.php`, ie with `http://` prefix and no trailing slash – Phil Aug 22 '18 at 06:36
  • Is it possible file_get_contents is disabled, and you don't have warnings/errors enabled? Can you try what Phil suggested and see if any errors show? – Dave Chen Aug 22 '18 at 06:37
  • actually the original code is present on remote server i cant change stuff there if something goes wrong everything will be screwed i just tested piece of code locally – Murali Krishnamraju Aug 22 '18 at 06:40
  • Can you post the output of `phpinfo()` please? – Alex Shesterov Aug 22 '18 at 06:41

0 Answers0