Receiving JSON with PHP via $ http.post ()

0

I want to read a JSON in PHP received from a function $http.post() of AngularJS, I already tried to use json_decode() and set the header (both PHP and Angular) but it did not work. PHP claims to be an undefined index, I tried to use var_dump and it returns NULL.

Angular

$http.post(path, $scope.meuJson, {
            headers: {
                'Content-Type': 'application/json'
            }
        }).success(function(response) {
            ... 
        })

PHP

header('Content-type: application/json');
$json = json_decode($_POST['meuJson']);

var_dump ($ _ POST)

array(0) {
}


NOTE: I can see the data being sent as payload of my request by the Network

    
asked by anonymous 25.05.2014 / 04:20

1 answer

3

You can not get the angular data via $ _POST, as they are not serialized as parameters in the request body.

To get the body of the request, read the data this way:

$meuPost = file_get_contents("php://input");

$json = json_decode( $meuPost );

The php://input is an entry for the body of the raw request sent by the browser, before parsing by PHP. In a rough way, it would be comparable to reading stdin in a local application (not the same thing, but to illustrate what happens).

    
25.05.2014 / 08:52