Reading Request Payload in PHP - php

How can I read content of Request Payload (POST) using PHP?
I´m using a Angular aplication to send the POST.
I've tried with: $_POST, $_FILES and file_get_contents('php://input') ...
None worked.

If you need view x-www-form-urlencoded post data, just use
print_r($_POST);
If you need view json post data, use this
$data=json_decode(file_get_contents('php://input'),1);
print_r($data);

Try:
$payload = file_get_contents('php://input');

Related

Difficulty receiving Json Form-Data Through PostMan

While receiving data on PHP file code is like -
print_r($_POST);
echo $name = $_REQUEST['name'];
I am getting null array
How to get the value while posting ? Here is the headers I used
use x-www-form-urlencoded while posting data.Then
echo json_encode($_POST);//prints into json format
Try it will works.
NOTE: By default, the $http service will transform the outgoing request by serializing the data as JSON and then posting it with the content- type, "application/json". When we want to post the value as a FORM post, we need to change the serialization algorithm and post the data with the content-type, "application/x-www-form-urlencoded".

Advanced REST client does not send POST data

I have this simple code
<?php
$json = array("status" => $_POST['name']);
header('Content-type: application/json');
echo json_encode($json);
?>
and when i send POST data with Advanced REST cliend, allways have an empty $_POST table.
You are using wrong transport method. If you want to read POST data in $_POST array you have to send it either as multipart or www form urlencoded.
To read the request body you have to use following code:
$postdata = file_get_contents("php://input");
Then you can parse the JSON and transform it to object.
If you want to read the data from the request using $_POST array you need to set Content-Type header to application/x-www-form-urlencoded and send the data as:
param-name=param+value
(note that it is url encoded).

How to read GET and POST request in PHP (post is in json)

I have a javascript (not from me) to sync information between client (javascript) and server (php) and javascript send to server GET and POST information, I can read easy GET information so I try to read $_POST information and cannot retrieve POST information.
I try print_r($_POST) and just returns Array() with nothing.
I try to look it with Firebug and Console into Firefox and I see that
How can I retrieve and treat json string into >Transmission de la requete< into my PHP code? I can easily retrieve GET parameters such as token, src and etc so I can't retrieve POST transmission.
Thank you for your helping !
I don't really get you. But since it's something like JSON request sent to the page. You can use the file_get_contents('php://input') to grab any JSON request sent. Then you can decode it into an object or an array.
Example
$request = file_get_contents('php://input');
To obtain an object
$input = json_decode($request);
So Input fields will be
$input->name; $input->password;
To Obtain an array
$input = json_decode($request, true);
So Input fields will be
$input[name]; $input[password];

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)
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);
$_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.
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.
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']);

Send json as request body or POST json in a variable

I am trying to figure out, is there any way of sending raw json to php instead of POST parameter. If it is possible then which way is best i mean either request body or POST parameter.
Send an HTTP request which has a JSON string as its request body (here: using curl on the command line):
$ curl -d '{"foo":"bar"}' example.com/test.php
Read this request body in PHP:
$json = file_get_contents('php://input');
Decode it:
$data = json_decode($json, true);
PHP's $_POST superglobal is automatically populated as an array if the request body of a POST request contains URL encoded key-value pairs (e.g. foo=bar&baz=42). In the above example you're still using an HTTP POST request with "POST data". It just doesn't automatically end up in $_POST because PHP doesn't know how to decode JSON automagically.

Categories