0

I am passing an object into a PHP script like this:

$http({
    method: 'POST',
    url: 'updateCoins.php',
    data: {flips: $scope.data.flips, error: $scope.data.pe},
})

In my PHP script I tried:

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

and

$data = json_decode(file_get_contents("php://input"));

I then tried echoing back the received value like this:

echo $data.flips

or

echo $data[flips]

or

echo $data->flips

I keep on getting either a blank result or the word "flips" back. How do I access those variables?

2
  • its in the post suerperglobal echo $_POST['flips']; Commented Jul 23, 2017 at 20:23
  • There's no reason to use $_POST over php://input, especially if you're sending JSON -- you're fine with what you're doing there already. Try dumping $data to see what's actually in it Commented Jul 23, 2017 at 20:30

2 Answers 2

2

If the data was sent JSON-encoded, then it would make sense to json_decode(). I suspect that your data is x-www-form-urlencoded (eg: name=john&age=7). In this case, PHP parses it into the $_POST array for you. If you wish to do it on your own, and handle either case, you could do something like:

// Read content-type to determine whether it's JSON data
$type = $_SERVER["CONTENT_TYPE"];
$content = file_get_contents('php://input');

if(strpos($type,'json') > -1):
    $data = json_decode($content, true);
else:
    parse_str($content, $data);
endif;

print_r($data);

You can always visually inspect the data with echo file_get_contents('php://input');

Sign up to request clarification or add additional context in comments.

2 Comments

When I try to echo 'file_get_contents', I get this error in the console: POST msolonko.net/updateCoins.php 500 (Internal Server Error) AND Possibly unhandled rejection: {"data":"","status":500,"config":{"method":"POST","transformRequest":[null],"transformResponse":[null],"jsonpCallbackParam":"callback","url":"updateCoins.php","data":{"flips":10,"error":20},"headers":{"Accept":"application/json, text/plain, /","Content-Type":"application/json;charset=utf-8"}},"statusText":"Internal Server Error"}
My data is in JSON - your code helped me realize that and then I used print_r to see the array structure.
1

Php won't grab the posted data from $_POST if the post is formatted in json. You can serialize the post data and submit it thusly

var data = 'flips='+$scope.data.flips+'&error='+$scope.data.pe;

$http({
    method: 'POST',
    url: 'updateCoins.php',
    headers: {'Content-Type': 'application/x-www-form-urlencoded'},
    data: data
});

OR you can do like BeetleJuice recommended. In my application I just do the following so that the rest of the code is uninterrupted.

if (empty($_POST) && file_get_contents('php://input')!==""){
    $_POST = json_decode(file_get_contents('php://input'), true);
}

at which point you can access it with $_POST['flips'] and $_POST['error']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.