PHP $ _POST is empty, but HTTP_RAW_POST_DATA has all the data

I'm just trying to send a POST request from JS to the server. But the server has an empty $_POST array. I could use HTTP_RAW_POST_DATA , but it will be deprecated in PHP 5.6. Can I post data in a $_POST array?

Environment: Chrome, apache2, PHP, AngularJS (I use the $http.post function).

Debug image (sorry for the lack of an attached image directly - I have no reputation 10)

+5
source share
4 answers

POST data must be in the query string or multipart / form data for proper decoding. Your data seems to be JSON, so you have to decode it yourself:

 $_POST = json_decode(file_get_contents('php://input'), true); 
+8
source

$_POST populated with a request of the form-urlencoded or multipart/form-data . Usually it looks like this:

 foo=bar&ipsum=lorm 

So kind of request GET .

Since you are sending JSON directly (this is awesome!) You can use:

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

See docs for more details.

+3
source

See by default, jQuery passes data using Content-Type: x-www-form-urlencoded and the familiar serialization foo=bar&baz=moe . However, AngularJS passes data using Content-Type: application/json and { "foo": "bar", "baz": "moe" } JSON serialization , unfortunately, some web server languages, especially PHP, are not separated from zero .

so that 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'; }); 

The answer from this message is from Felipe Myoso .

+3
source

looks like transferring json data directly without any attempts using

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

or use a variable when sending data like

 data{'myvar': data} 

and will receive POST data, for example

 print_r($_POST['myvar']); 
0
source

Source: https://habr.com/ru/post/1206866/


All Articles