Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP $_POST is empty, but HTTP_RAW_POST_DATA has all data

I'm just trying to send a POST request with JS to server. But server has empty $_POST array. I could use HTTP_RAW_POST_DATA, but it'll be deprecated in PHP 5.6. Could I have posted data in my $_POST array?

Environment: Chrome, apache2, PHP, AngularJS (I'm using $http.post function).

Debug image (sorry for not attaching image directly - I have no 10 reputation)

like image 994
Black Akula Avatar asked Nov 14 '14 10:11

Black Akula


2 Answers

The POST data must be in query string or multipart/form-data format to get decoded properly. Your data seems to be JSON, so you have to decode it by yourself:

$_POST = json_decode(file_get_contents('php://input'), true);
like image 53
BreyndotEchse Avatar answered Sep 28 '22 15:09

BreyndotEchse


$_POST is populated by a request that is of type form-urlencoded or multipart/form-data. Typically it looks like:

foo=bar&ipsum=lorm

So kinda like a GET request.

Since you're posting JSON directly (which is awesome!) you can use:

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

See the docs for more info.

like image 27
Halcyon Avatar answered Sep 28 '22 15:09

Halcyon