I have response from webservice:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://tempuri.org/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<SOAP-ENV:Body>
<ns1:H1 xsi:type="ns1:H1">
<BOGUS>
<time>1411967345</time>
<status>1</status>
<speed>0</speed>
</BOGUS>
<BOGUS>
<time>1411964888</time>
<status>10</status>
<speed>0</speed>
</BOGUS>
</ns1:H1>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
How can I access to element time or status in BOGUS[0] or BOGUS[1]?
I tried this:
$soap = simplexml_load_string($str);
$response = $soap->children('http://schemas.xmlsoap.org/soap/envelope/')->Body->children('http://tempuri.org/')->H1;
$time = $response->BOGUS[1]->time;
echo $time;
, but it's not working. Returns: Notice: Trying to get property of non-object
tempuri.org is right. I pasted xml response on: xmlgrid.net and got correct tree.
I'd recommandate to use Zend Soap Client for PHP. There u can do like this:
$client = new Zend_Soap_Client("MyService.wsdl");
$result = $client->yourMethod(<YouParameters ...>);
echo $result->H1->BOGUS[1]->time;
See:
http://framework.zend.com/manual/1.12/de/zend.soap.client.html
You can do it by loops as you are getting array in return
foreach ($response as $res)
{
$time = $res->BOGUS[1]->time;
echo $time;
}
Related
I have been spending hours trying to parse a SOAP response that I have no control over. I have tried numerous methods I found on SO with no luck.
Here is the response body I get from edge browser:
<?xml version='1.0' encoding='UTF-8'?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<SOAP-ENV:Body>
<ns1:gXMLQueryResponse xmlns:ns1="urn:com-photomask-feconnect-IFeConnect" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<return xsi:type="xsd:string"><?xml version = '1.0' encoding = 'UTF-8'?>
<ROWSET>
<ROW num="1">
<CUSTOMER_NAME>HITACHI</CUSTOMER_NAME>
</ROW>
</ROWSET>
</return>
</ns1:gXMLQueryResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
I'm trying to get the CUSTOMER_NAME value.
Here is the code I am using:
$client = new SoapClient($urla, array('trace' => 1));
try {
$result = $client->__soapCall("gXMLQuery", $params);
$response = ($client->__getLastResponse());
$xml = simplexml_load_string($response);
$rows = $xml->children('SOAP-ENV', true)->Body->children('ns1', true)->gXMLQueryResponse->return->ROWSET->ROW;
foreach ($rows as $row)
{
$customer = $row->CUSTOMER_NAME;
echo $customer;
}
} catch (SoapFault $e) {
}
return is a string, you need to parse it first before you can access it using SimpleXML.
First you need to decode the string using html_entity_decode, after that you can load the decoded string with simplexml_load_string:
$return = $xml->children('SOAP-ENV', true)->Body->children('ns1', true)->gXMLQueryResponse->return;
$decodedReturn = html_entity_decode($return, ENT_QUOTES | ENT_XML1, 'UTF-8');
$rowset = simplexml_load_string($decodedReturn);
echo $rowset->ROW->CUSTOMER_NAME;
Response from CURL request:
<?xml version="1.0" encoding="UTF-8"?>
<soap-env:envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/">
<soap-env:header>
<soap-env:body>
<ipgapi:ipgapiorderresponse xmlns:ipgapi="http://ipg-online.com/ipgapi/schemas/ipgapi" xmlns:a1="http://ipg-online.com/ipgapi/schemas/a1" xmlns:v1="http://ipg-online.com/ipgapi/schemas/v1">
<ipgapi:approvalcode>Y:761862:4515799310:PPXP:0037</ipgapi:approvalcode>
<ipgapi:avsresponse>PPX</ipgapi:avsresponse>
<ipgapi:brand>VISA</ipgapi:brand>
<ipgapi:country>GBR</ipgapi:country>
<ipgapi:commercialserviceprovider>BOSMS</ipgapi:commercialserviceprovider>
<ipgapi:orderid>A-966025d3-81a2-453a-820e-bb145e8390d1</ipgapi:orderid>
<ipgapi:ipgtransactionid>84515799310</ipgapi:ipgtransactionid>
<ipgapi:paymenttype>CREDITCARD</ipgapi:paymenttype>
<ipgapi:processorapprovalcode>761862</ipgapi:processorapprovalcode>
<ipgapi:processorccvresponse>P</ipgapi:processorccvresponse>
<ipgapi:processorreferencenumber>761862</ipgapi:processorreferencenumber>
<ipgapi:processorresponsecode>00</ipgapi:processorresponsecode>
<ipgapi:processorresponsemessage>AUTH CODE:761862</ipgapi:processorresponsemessage>
<ipgapi:tdate>1521047872</ipgapi:tdate>
<ipgapi:tdateformatted>2018.03.14 18:17:52 (CET)</ipgapi:tdateformatted>
<ipgapi:terminalid>21400371</ipgapi:terminalid>
<ipgapi:transactionresult>APPROVED</ipgapi:transactionresult>
<ipgapi:transactiontime>1521047872</ipgapi:transactiontime>
</ipgapi:ipgapiorderresponse>
</soap-env:body>
</soap-env:header>
</soap-env:envelope>
I've tried the following:
$responseXML = '<?xml version="1.0" encoding="UTF-8"?>
<soap-env:envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/">
<soap-env:header>
<soap-env:body>
<ipgapi:ipgapiorderresponse xmlns:ipgapi="http://ipg-online.com/ipgapi/schemas/ipgapi" xmlns:a1="http://ipg-online.com/ipgapi/schemas/a1" xmlns:v1="http://ipg-online.com/ipgapi/schemas/v1">
<ipgapi:approvalcode>Y:761862:4515799310:PPXP:0037</ipgapi:approvalcode>
<ipgapi:avsresponse>PPX</ipgapi:avsresponse>
<ipgapi:brand>VISA</ipgapi:brand>
<ipgapi:country>GBR</ipgapi:country>
<ipgapi:commercialserviceprovider>BOSMS</ipgapi:commercialserviceprovider>
<ipgapi:orderid>A-966025d3-81a2-453a-820e-bb145e8390d1</ipgapi:orderid>
<ipgapi:ipgtransactionid>84515799310</ipgapi:ipgtransactionid>
<ipgapi:paymenttype>CREDITCARD</ipgapi:paymenttype>
<ipgapi:processorapprovalcode>761862</ipgapi:processorapprovalcode>
<ipgapi:processorccvresponse>P</ipgapi:processorccvresponse>
<ipgapi:processorreferencenumber>761862</ipgapi:processorreferencenumber>
<ipgapi:processorresponsecode>00</ipgapi:processorresponsecode>
<ipgapi:processorresponsemessage>AUTH CODE:761862</ipgapi:processorresponsemessage>
<ipgapi:tdate>1521047872</ipgapi:tdate>
<ipgapi:tdateformatted>2018.03.14 18:17:52 (CET)</ipgapi:tdateformatted>
<ipgapi:terminalid>21400371</ipgapi:terminalid>
<ipgapi:transactionresult>APPROVED</ipgapi:transactionresult>
<ipgapi:transactiontime>1521047872</ipgapi:transactiontime>
</ipgapi:ipgapiorderresponse>
</soap-env:body>
</soap-env:header>
</soap-env:envelope>';
$xml = simplexml_load_string($responseXML);
print_r($xml);
// Returns empty object
I've also tried the following:
$xml = new SimpleXMLElement($responseXML);
print_r($xml);
// This also returns empty object
Could someone help me figure out what's going wrong with this?
Using print_r(); with a SimpleXMLElement usually doesn't give anything useful. Instead you should use asXML() which outputs the original XML.
$xml = simplexml_load_string($responseXML);
echo $xml->asXML();
Which gives
<?xml version="1.0" encoding="UTF-8"?>
<soap-env:envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/">
<soap-env:header>
<soap-env:body>
...
You can access the data by using something like...
$xml = simplexml_load_string($responseXML);
$body = $xml->xpath("//soap-env:body");
$bodyData = $body[0]->children("ipgapi", true);
echo $bodyData->ipgapiorderresponse->approvalcode;
So $bodyData is the <ipgapi:ipgapiorderresponse ...> element and so accessing each part of that is by using the last line. You can use the element name (minus the namespace prefix ipgapi as this is taken care of with the children() call earlier). That line outputs
Y:761862:4515799310:PPXP:0037
I am trying to parse a soap response with simplexml_load_string(). I have my soap client set with trace = 1 and exceptions = 0 and $client->__getLastResponse() gives me this result:
<?xml version='1.0' encoding='UTF-8'?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV = "http://schemas.xmlsoap.org/soap/envelope/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<MDHeader>
<userid>inspector#prime.com</userid>
<password>prime123456</password>
<batchid>1234</batchid></MDHeader>
<RECORDSET>
<ROW id='0'>
<INSPECTIONS>
<FOLDER_ID>835410936</FOLDER_ID>
<FOLDER_ID>835221706</FOLDER_ID>
<FOLDER_ID>835222299</FOLDER_ID>
</INSPECTIONS>
</ROW>
</RECORDSET>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
I then read the results into $xml:
$xml = "<?xml version='1.0' encoding='UTF-8'?>";
$xml .= urldecode($client->__getLastResponse());
An finally try to echo an element with no luck:
$xml1 = simplexml_load_string($xml,null,null,'http://schemas.xmlsoap.org/soap/envelope/',true);
echo "ELEMENT:" . $xml1->Envelope->Body->MDHeader->userid;
I believe it is the namespace SOAP-ENV that is giving me the issue but I don't know how to resolve it.
You can see my test page at: http://www.primevaluationservices.com/myriad/test.php
I think you don't need this line because it is already in the xml:
$xml = "<?xml version='1.0' encoding='UTF-8'?>";
To get the 'userid', I think you can use the SimpleXMLElement children method:
$source = <<<SOURCE
<?xml version='1.0' encoding='UTF-8'?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV = "http://schemas.xmlsoap.org/soap/envelope/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<MDHeader>
<userid>inspector#prime.com</userid>
<password>prime123456</password>
<batchid>1234</batchid></MDHeader>
<RECORDSET>
<ROW id='0'>
<INSPECTIONS>
<FOLDER_ID>835410936</FOLDER_ID>
<FOLDER_ID>835221706</FOLDER_ID>
<FOLDER_ID>835222299</FOLDER_ID>
</INSPECTIONS>
</ROW>
</RECORDSET>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
SOURCE;
$xml1 = simplexml_load_string($source);
echo $xml1->children('SOAP-ENV', true)->Body->children('')->MDHeader->userid->__toString();
Or the SimpleXMLElement xpath method:
$elements = $xml1->xpath('//SOAP-ENV:Envelope/SOAP-ENV:Body/MDHeader/userid');
$userid = $elements[0]->__toString();
echo '<br>';
echo $userid;
Will both result in:
inspector#prime.com
Demo
I'm very very new to being on the Server side of things. I need to set up a simple server.php to receive xml requests.
I'm not even sure how to start with this. I am used to the normal Post/Get variables from forms.
Here's an example of what I'm listening for, and needing to respond to:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Body>
<authenticate xmlns="http://someplace.someplace.com/">
<strUserName xsi:type="xsd:string">username</strUserName>
<strPassword xsi:type="xsd:string">password</strPassword>
</authenticate>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Obviously there's a username/password I can see inside.
Validating the user/pass is the easy part, but how do I parse that out?
You can also use DOM element to access your desired values:
$xml = '<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Body>
<authenticate xmlns="http://someplace.someplace.com/">
<strUserName xsi:type="xsd:string">username</strUserName>
<strPassword xsi:type="xsd:string">password</strPassword>
</authenticate>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>';
$dom = new DOMDocument();
$dom->loadXML( $xml );
$username = $dom->getElementsByTagName('strUserName')->item(0)->nodeValue;
$password = $dom->getElementsByTagName('strPassword')->item(0)->nodeValue;
Give it a try with SimpleXML, you have a few examples here:
SimpleXML examples
You should probably use existing API when parsing a SOAP request. This will not only automatically get rid of your parsing problems but also generate correct SOAP output.
Example:
$request = '<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Body>
<authenticate xmlns="http://someplace.someplace.com/">
<strUserName xsi:type="xsd:string">foo</strUserName>
<strPassword xsi:type="xsd:string">bar</strPassword>
</authenticate>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>';
$s = new SoapServer(NULL, array('uri' => 'http://someplace.someplace.com/'));
$s->setClass("Auth");
$s->handle($request);
class Auth
{
public function authenticate($strUserName, $strPassword)
{
return "U: $strUserName; P: $strPassword";
}
}
Note: If you do not pass an argument to handle() it will use POST data.
I am trying to consume a web service with no success. It looks like the xml is no properly set.
This is my PHP code:
<?php
$wsdlUrl = "http://api.clickatell.com/soap/webservice.php?WSDL";
$serviceUrl = "http://api.clickatell.com/soap/webservice.php";
$request = array("user"=>"anyuser",
"password"=>"asdsda",
"api_id"=>"1234"
);
var_dump($request);
try {
$client = new SoapClient($wsdlUrl, array("trace" => 1, "location" => $serviceUrl));
var_dump($client);
echo "\n";
$response = $client->auth($request);
var_dump($response);
var_dump($client->__getLastRequest());
echo "\n";
}
catch(Exception $exp) {
echo "EXCEPTION";
}
?>
And the SOAP packet is:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://api.clickatell.com/soap/webservice" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<ns1:auth>
<api_id xsi:type="xsd:int">1</api_id>
<user xsi:nil="true"/>
<password xsi:nil="true"/>
</ns1:auth>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
What is wrong with the code that causes that api_id, user and password are not being sent?
If you check out var_dump($client->__getFunctions()), you can see, that the parameters have to be passed like with a normal function call.
So you could do either the verbose thing with SoapParam:
$response = $client->auth(
new SoapParam($request["api_id"], "api_id"),
new SoapParam($request["user"], "user"),
new SoapParam($request["password"], "password")
);
Or less verbose by just giving the parameters directly:
$response = $client->auth($request["api_id"],
$request["user"], $request["password"]);