-2

I call a PHP file from POSTMAN with this URL: http://localhost/st/user_login.php?surat=DRIVER&sandi=123

In user_login.php, I type this to process JSON:

$json = file_get_contents('php://input');
$obj = json_decode($json,true);
$email = $obj['surat'];
$password = $obj['sandi'];

The problem is, $email always returns a null value. Can somebody tell me where the problem is?

0xCursor
  • 2,242
  • 4
  • 15
  • 33
  • You are sending GET variables - have you checked `$_GET` ? Try `print_r($_GET)` and see if you have the data you want – ivanivan Aug 15 '18 at 02:43
  • `file_get_contents('php://input')` does not return JSON because that isn't what you sent to it. You data is in the `$_GET` variables. – ryantxr Aug 15 '18 at 03:02

1 Answers1

0

When we use php://input PHP "php://input" vs $_POST

Try to use $_GET or $_POST. In your case, clearly, you need to use $_GET. But, it's better for security reason we need to use $_POST.

For example, using POST man and php://input

  • Body: raw - JSON(application/json)
  • POST

enter image description here

PHP test script

<?php

header('Content-Type: application/json');

$json = file_get_contents('php://input');
$obj = json_decode($json,true);

$result = [];
$result['message'] = 'Success Message';
$result['email'] = $obj['email'];
echo json_encode($result);
Khoa TruongDinh
  • 913
  • 1
  • 13
  • 26