I want to retrieve the token as seen in the developer console image below. How do I do this?
PHP gets the raw request, and if you send JSON content it can get the raw data with the below method:
$postRaw = file_get_contents("php://input");
print_r($postRaw);
Sometimes $_POST return empty but if $_POST return data , you can basically use $_POST['postname']:
print_r($_POST);
Related
I'm sending data through POST from one application to another, using the file_get_contents function, I can't receive the data but using the variable $ _POST I can get it. Which would be the most suitable to receive the data?
I would like to send a token for authentication together, how could I do that?
$cont = json_decode(file_get_contents('php://input'), true);
echo gettype($cont);
NULL
And when I try the pre-defined variable, I can receive the data.
$cont = $_POST;
echo gettype($cont);
array
file_get_contents('php://input') gets the raw POST data as a string.
$_POST gets the POST data after PHP has automatically decoded it from application/x-www-form-urlencoded or multipart/form-data encodings.
If the data isn't encoded using one of those methods (e.g. if it is JSON as per your first example which uses json_decode) then $_POST won't be populated.
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');
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).
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];
I'm just a newbie in the Slim framework. I've written one API using Slim framework.
A POST request is coming to this API from an iPhone app. This POST request is in JSON format.
But I'm not able to access the POST parameters that are sent in a request from iPhone. When I tried to print the POST parameters' values I got "null" for every parameter.
$allPostVars = $application->request->post(); //Always I get null
Then I tried to get the body of a coming request, convert the body into JSON format and sent it back as a response to the iPhone. Then I got the parameters' values but they are in very weird format as follows:
"{\"password\":\"admin123\",\"login\":\"admin#gmail.com\",\"device_type\":\"iphone\",\"device_token\":\"785903860i5y1243i5\"}"
So one thing for sure is POST request parameters are coming to this API file. Though they are not accessible in $application->request->post(), they are coming into request body.
My first issue is how should I access these POST parameters from request body and my second issue is why the request data is getting displayed into such a weird format as above after converting the request body into JSON format?
Following is the necessary code snippet:
<?php
require 'Slim/Slim.php';
\Slim\Slim::registerAutoloader();
//Instantiate Slim class in order to get a reference for the object.
$application = new \Slim\Slim();
$body = $application->request->getBody();
header("Content-Type: application/json");//setting header before sending the JSON response back to the iPhone
echo json_encode($new_body);// Converting the request body into JSON format and sending it as a response back to the iPhone. After execution of this step I'm getting the above weird format data as a response on iPhone.
die;
?>
Generally speaking, you can access the POST parameters individually in one of two ways:
$paramValue = $application->request->params('paramName');
or
$paramValue = $application->request->post('paramName');
More info is available in the documentation: http://docs.slimframework.com/#Request-Variables
When JSON is sent in a POST, you have to access the information from the request body, for example:
$app->post('/some/path', function () use ($app) {
$json = $app->request->getBody();
$data = json_decode($json, true); // parse the JSON into an assoc. array
// do other tasks
});
"Slim can parse JSON, XML, and URL-encoded data out of the box" - http://www.slimframework.com/docs/objects/request.html under "The Request Body".
Easiest way to handle a request in any body form is via the "getParsedBody()". This will do guillermoandrae example but on 1 line instead of 2.
Example:
$allPostVars = $application->request->getParsedBody();
Then you can access any parameters by their key in the array given.
$someVariable = $allPostVars['someVariable'];
Slim will json_decode the post body for you using this middleware
https://www.slimframework.com/docs/v4/middleware/body-parsing.html