Receive PHP Json POST

Asked

Viewed 2,568 times

0

I am trying to receive in PHP a Json via POST and display it on the screen, the Json is sent through an API that does not have access to the script you send, I am trying to print on the screen everything I receive and then proceed, know that it is sent through the POST and Content-Type method: 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'".

2 answers

1

I managed to resolve, as the data received is in Json it does not come through the 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;
?>

0

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

$obj = json_decode($json,true);

I hope I helped! Thanks

  • Victor thanks for the help but also failed so I managed to find the solution and posted below.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.