Retrieving json submitted by postman in php [duplicate] - php

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

Related

Send Json to PHP and empty $_POST [duplicate]

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

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.

Laravel Guzzle Parse XML Response [duplicate]

This question already has answers here:
Retrieve the whole XML response body with Guzzle 6 HTTP Client
(4 answers)
Closed 7 years ago.
I'm pretty new to using Laravel (5.1)/PHP and especially doing anything with the Guzzle HTTP package. I have an api I'm trying to return a response from. I'm successfully getting the response, but I need to save pieces of it to variables to use later in my applicaiton
How do I parse through the response to get the pieces of xml that I need, say anything within <status>Passing</status>?
The following retrieves the entire xml response.
use GuzzleHttp\Client;
$client = new Client();
$res = $client->request('GET', 'https://api.com?parameter=value');
$body = $res->getBody();
echo $body;
Thanks for your help!
I'm not super familiar with Guzzle but try this.
$xml = $response->xml();
$status = $xml->status;
Here's the documentation from which I based this off of:
http://guzzle3.readthedocs.org/http-client/response.html#xml-responses

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

How to send the request to url and receive the response in the xml format using php [duplicate]

This question already has answers here:
how to read xml file from url using php
(5 answers)
Closed 8 years ago.
The below mentioned URL Link sends a XML response in browser. The same response is received in a variable using PHP
In single PHP page I should raise the URL request and should get the response in the same page.
http://localhost:8090/solr/salesreport/select?q=salesdates:[2007-05-01 TO 2014-05-15]&wt=xml&rows=10&indent=true
$xmlDoc = new DOMDocument();
$xmlDoc->loadXML(file_get_contents("http://203.196.191.92:8090/solr/salesreport/select?q=salesdates:[2007-05-01TO2014-05-13]&wt=xml&rows=10&indent=true"));
I found the solution:
$url = 'http://localhost:8090/solr/salesreport/select?q='.$grystrres.'&wt=xml&rows=10&indent=true';
$xml = simplexml_load_file($url);
print_r($xml);
$variable = file_get_contents('http://203.196.191.92:8090/solr/salesreport/select?q=salesdates:[2007-05-01TO2014-05-13]&wt=xml&rows=10&indent=true');

Categories