0

I know this question has been asked severally and for two days I have been trying to follow the suggested fixes and answers from other questions but nothing is working. I am trying to test my php page for receiving json Data but the page seems not to be receiving anything. I am using postman to send the json and I am using PHP 7.3.27 Here is my code

<?php

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

$content = file_get_contents('php://input');
echo "$content";

$decoded = json_decode($content,true);
var_dump($decoded);
$data = array();

if ($decoded['name']=='James') {
    $data = array("Name"=>"James Blunt","username"=>"jamesBlunt","shoeSize"=>10);
    }
print_r($data);

?>

The above code return NULL and an empty array. The following is my photo for postman. Postman Response

So I am confused about this, am I doing something wrong? Do I need to add or edit anything in my php.ini file?

allow_url_fopen=On

allow_url_include=Off

I already have these two in my php.ini

Benson Kiprono
  • 129
  • 1
  • 1
  • 12
  • your code works fine on my machine I just tested. make sure you have `Content-Type:application/json` in your headers in postman – Umer Abbas Feb 27 '21 at 09:54
  • @OmarAbbas which php version are you using please? – Benson Kiprono Feb 27 '21 at 11:38
  • Are you using NGINX? Is NGINX changing your POST to a GET on rewrite for HTTPS? Because I have seen that a bajillion times. No matter what you POST, the change to GET strips all of your body content There is also the classic Postman problem of trailing slashes nginx redirects POST requests to GET request - https://stackoverflow.com/questions/24691560/nginx-redirects-post-requests-to-get-request Moved 'Answer' to 'Comment' as should have been done – Beerswiller Feb 28 '21 at 02:33
  • try using the built-in server `php -S localhost:8080` then POST to http://localhost:8080/test.php I tried your code, it works fine – Beerswiller Feb 28 '21 at 02:33

0 Answers0