Send Json to PHP and empty $_POST [duplicate] - php

This question already has answers here:
Receive JSON POST with PHP
(12 answers)
Closed 8 months ago.
Due to this problem I have summarized the error in the most basic example to find a solution.
I have a PHP file with the following code:
if( isset($_POST['nombre']) ){
echo("Do thing");
}
else{
echo("Error");
}
If I send the NAME data from a post form, it enters the IF without problem.
If from Postman I try to send a data from BODY FORM-DATA or x-www.form.urlencoded, the POST arrives without problem and enters the IF.
{"nombre": "fdsf"}
But if I send a JSON from postman, something like $_POST['name'] comes back empty and being empty does not enter the IF
Does anyone know why it happens?.

Try 'php://input'
It allows us to read raw data from the request body, regardless of the content type.
Try this
$json = file_get_contents('php://input');
$data = json_decode($json);
return print_r($data);

Related

How do I retrieve a token from a post to PHP?

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);

Retrieving json submitted by postman in php [duplicate]

This question already has answers here:
Send POST data via raw JSON with Postman
(6 answers)
Reading json input in php
(2 answers)
Closed 4 years ago.
I'm creating an api using php and I'm using postman to test my requests.
in postman I choose the method of posting and in body use the raw to send a json to my api
category{
"id":"1",
"desc": "testing",
"observation": "testing",
}
it sends perfectly, but how can I recover my json on the server side? in my php
i'm using
$result = json_decode($_POST['category'], true);
but the error occurs
Notice: Undefined index: category in
If the data is in the actual body you might need to retrieve it instead of looking in the $_POST array:
Try this:
$body = file_get_contents('php://input');
echo $body;
As seen in the comments of the question, you can use json_decode() to get a php object.
$object = json_decode($body);
echo '<pre>';
print_r($object);
echo '</pre>';

Read Angular2 POST data in PHP [duplicate]

This question already has answers here:
Reading JSON POST using PHP
(3 answers)
Closed 6 years ago.
I want to connect my angular2 app to my PHP backend. Ideally I want to do this:
this.http.post('/users/create', {email: email, password: password});
The problem is, when I do this my $_POST is empty in PHP. What must I do to make this work?
Angular's http implementation sends data as an application/json payload. to read such data from php, you have to use this kind of code :
$data = json_decode(file_get_contents("php://input"));
// you can even override the `$_POST` superglobal if you want :
$_POST = json_decode(file_get_contents("php://input"));
if you want to send your data as application/x-www-form-urlencoded and then be able to read it from php's $_POST superglobal without any change to your server code, you need to encode your data as such.
const body = new URLSearchParams();
Object.keys(value).forEach(key => {
body.set(key, value[key]);
}
let headers = new Headers();
headers.append('Content-Type','application/x-www-form-urlencoded');
this._http.post(this._contactUrl, body.toString(), {headers}).subscribe(res => console.log(res));
I mean with jQuery for example it works with $_POST and json objects
it does not work with json object, if you can read data via $_POST, it means it has been sent as application/x-www-form-urlencoded, not application/json, parameters are set as a plain js object though...
you can use php://input for the post data with angular2 like this and json_decode by this
$arr = json_decode(file_get_contents('php://input'),TRUE);
echo "<pre>";print_r($arr);exit;
so by this $arr prints the whole post array and used it anywhere you want.

How to read JSON data using PHP [duplicate]

This question already has answers here:
Reading JSON POST using PHP
(3 answers)
Closed 7 years ago.
I am trying to read data send by curl request but I cant able to read that.
example
curl -x post -d '{user:"test", pwd:"123"}' http://localhost/api/login/
and in PHP code
$user = $_POST['user'];
but always I am getting a null value, how to fix this issue?
You can specify the post name in curl. Right now you're submitting JSON, but without a name. (Link to curl man pages)
-d data={JSON}
Then in php you can load and decode the JSON: (Link to json_decode in the php manual)
$data = json_decode($_POST['data'], true);
$user = $data['user'];
If you want to continue sending data without using key/value pairs, you can keep your existing implementation of curl and use the following in PHP:
$data = #file_get_contents("php://input");
$json = json_decode($data, true);
This was taken from: XMLHTTP request passing JSON string as raw post data

Unable to get data Posted by webhook in JSON String

I am using web hook for get user details from our company's signup page.
Data posted from signup page in JSON format. Posted JSON String is below
{"FirstName":"dharampal","EmailAddress":"er.dpyadav#gmail.com","Mobile":"123456789","Company":"Instasafe","LandingPageURL":"http://instasafe.com/SignUp","LandingPageId":"11e2-b071-123141050daa"}
In receiving end, we are using PHP codeIgNiter, I tried below methods but unable to get data.
$postedData = $_POST;
print_r($postedData);
it gives : empty array
other method i tried is :
$postedData = json_decode(file_get_contents('php://input'), TRUE);
print_r($postedData);
it gives : NULL
Seems like the problem is a typo with $ostedData and $postedData
$ostedData = json_decode(file_get_contents('php://input'), TRUE);
print_r($postedData);

Categories