Receiving POST from a Json in PHP

0

I'm trying to get a Json via POST and display it on the screen, Json is sent via an API that I do not have access to the script it sends, I'm trying to print everything I receive on the screen and then proceed, I know it is sent via POST and Content-Type: application / json; charset = UTF-8

<?php
header("Access-Control-Allow-Origin: *");
header('Cache-Control: no-cache, must-revalidate'); 
header("Content-Type: application/json; charset=UTF-8");

print_r($_POST);

?>

But always returns the error "Unexpected 'A".

    
asked by anonymous 25.05.2018 / 16:46

2 answers

1

I was able to resolve, as the received data is in Json it does not come through POST but in 'php: // input'

<?php
header("Access-Control-Allow-Origin: *");
header('Cache-Control: no-cache, must-revalidate'); 
header("Content-Type: text/plain; charset=UTF-8");
header("HTTP/1.1 200 OK");

$dados = file_get_contents("php://input");
echo $dados;
?>
    
25.05.2018 / 19:17
0

Is Daniel all right? Try to change the 'header' function to 'require_once', and also when you receive something that is coming in JSON you must use the code 'json_decode' because it will dismember this data that is coming to you wherever you want. So for example:

$obj = json_decode($json,true);

I hope I have helped! Thanks.

    
25.05.2018 / 17:59