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
Related
i have a problem dedoding a JSON response from a webservice using Guzzle 6 under Laravel 6.
The response i'm trying to decode is here:
https://json.volotea.com/dist/stations/stations.json
In my php code i try in this way:
$client = new Client();
$response = $client->request('GET','https://json.volotea.com/dist/stations/stations.json');
To decode the response i tried several methods like:
$response->getBody(); // that returns a stream
$response->getBody()->getContents(); that returns an apparently correct answer but if I run the json_decode it returns a null as a result
I'm going crazy!
How can i decode the response from this service?
many thanks
it seems with method getContent you already manipulated Json structure
try json_decode on $response.
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>';
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
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');
This question already has answers here:
Inspect XML created by PHP SoapClient call before/without sending the request
(3 answers)
Closed 1 year ago.
Is it possible to get the XML generated by SOAP Client before sending it to webservice?
I need this because response from webservice if one of parameters is really wrong I get errors like
Server was unable to read request.
---> There is an error in XML document (2, 408).
---> Input string was not in a correct format.
This typically includes firing up tcpmon or some other tcp watcher utility, capturing the webservice call, copy and paste xml to text editor and go to column 408 to see what's the problem.
I'd really like to simplify this process by getting the XML before sending it.
It is very, very hard (nigh impossible) to do that. What is much easier is using the SoapClient class' built-in debugging functionality to output the request after it has been sent. You can do that like so:
First, when creating your SOAPClient, enable tracing, like so:
$client = new SoapClient($wsdl, array('trace' => true));
Then do whatever processing is necessary to get ready to make the SOAP call and make it. Once it has been made, the following will give you the request you have just sent:
echo("<pre>"); //to format it legibly on your screen
var_dump($client->__getLastRequestHeaders()); //the headers of your last request
var_dump($client->__getLastRequest()); //your last request
And, if you want to see the response as well, the following should work:
var_dump($client->__getLastResponseHeaders()); //response headers
var_dump($client->__getLastResponse()); //the response