5

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)

Black Akula
  • 616
  • 2
  • 6
  • 13
  • 1
    Try adding headers: {'Content-Type': 'application/x-www-form-urlencoded'}, to your $http.post? – Matt Burrow Nov 14 '14 at 11:00
  • See my [Debug image](http://i.share.pho.to/c7e0e850_l.png) for code. I tried `{'Content-Type': 'application/x-www-form-urlencoded'}` but in this case I have no neither `$_POST` nor `HTTP_RAW_POST_DATA` on server – Black Akula Nov 14 '14 at 11:21

4 Answers4

10

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);
BreyndotEchse
  • 2,192
  • 14
  • 20
  • 1
    Although, to exactly use it like $_POST we should add true as second parameter to json_decode so that it returns an array. $_POST = json_decode(file_get_contents('php://input'), true); – SuperNOVA Sep 21 '15 at 21:59
  • You're right. Thank you for your comment. Answer edited. – BreyndotEchse Sep 23 '15 at 13:24
4

$_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.

Halcyon
  • 57,230
  • 10
  • 89
  • 128
  • Why is posting JSON directly awesome? (curiosity, no offense) – Attila Fulop Nov 14 '14 at 11:07
  • 1
    Because using form encoding on JSON data is problematic. Specifically empty values do not get transmitted. `{"foo":[],"bar:"baz"}` will result in just `bar=baz`. Form encoding has no way of specifying an empty array. – Halcyon Nov 14 '14 at 11:49
  • 1
    A missing empty array may not seem like a problem because you can add it as a default value. But, if you have: `{foo:[[],[]]}` form encoding gives an empty result. I think that's a problem. – Halcyon Nov 14 '14 at 11:57
3

See by default, jQuery transmits data using Content-Type: x-www-form-urlencoded and the familiar foo=bar&baz=moe serialization. AngularJS, however, transmits data using Content-Type: application/json and { "foo": "bar", "baz": "moe" } JSON serialization, which unfortunately some Web server languages—notably PHP—do not unserialize natively.

so you can do this when you define your angular module:

angular.module('MyModule', [], function($httpProvider) {
    // Use x-www-form-urlencoded Content-Type
    $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';
});

answer taken from this post by Felipe Miosso.

Community
  • 1
  • 1
Jai
  • 74,255
  • 12
  • 74
  • 103
  • Yeah, it's working. But I don't know, why direct setting 'Content-Type' in `$http.post(filtersUrl, data, {headers: {'Content-Type': 'application/json; charset=utf-8'})` is not working... I've did `$http.defaults.headers.post['Content-Type'] = 'application/json; charset=utf-8'` and it's also working – Black Akula Nov 14 '14 at 11:32
  • `$http.defaults.headers.post['Content-Type'] = 'application/json; charset=utf-8'` also does the same thing, but providing as suggested is available in the whole application i think. – Jai Nov 14 '14 at 11:35
  • And yes, in this case I was needed to do `json_decode(file_get_contents('php://input'));` on my server... – Black Akula Nov 14 '14 at 13:43
0

looks like json data posting directly without any variable try with

$request = file_get_contents('php://input');
print_r($request);

or use a variable on posting data like

data{'myvar': data}

and will get on POST data like

print_r($_POST['myvar']);
Rakesh Sharma
  • 13,680
  • 5
  • 37
  • 44
  • Both are not working: 1) `file_get_contents("php://input")` gives empty string; 2) `{'myvar': data}` has the same result as original post data (`$_POST` is empty) – Black Akula Nov 14 '14 at 11:05