Parse soap xml document php - php

I have this SOAP response.
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header/>
<soapenv:Body>
<pkup:PickupPendingStatusResponse xmlns:pkup="http://www.ups.com/XMLSchema/XOLTWS/Pickup/v1.1">
<common:Response xmlns:common="http://www.ups.com/XMLSchema/XOLTWS/Common/v1.0">
<common:ResponseStatus>
<common:Code>1</common:Code>
<common:Description>Success</common:Description>
</common:ResponseStatus>
</common:Response>
<pkup:PendingStatus>
<pkup:PickupType>01</pkup:PickupType>
<pkup:ServiceDate>20141006</pkup:ServiceDate>
<pkup:PRN>2929AONCALL</pkup:PRN>
<pkup:OnCallStatusCode>001</pkup:OnCallStatusCode>
<pkup:PickupStatusMessage>Received at dispatch</pkup:PickupStatusMessage>
<pkup:BillingCode>01</pkup:BillingCode>
<pkup:ContactName>Shipping Mgr.</pkup:ContactName>
<pkup:ReferenceNumber>OnCallNextDayAir</pkup:ReferenceNumber>
</pkup:PendingStatus>
</pkup:PickupPendingStatusResponse>
</soapenv:Body>
I need to get the PickupPendingStatusResponse->Response->ResponseStatus->Description.
I also would like to get pkup:PendingStatus into an array and be able to get each of it's children values.

I used the DOMDocument class to get the nodes
$doc = new DOMDocument();
$doc->loadXML($upsResponse);
$PRN = $doc->getElementsByTagName('PRN')->item(0)->nodeValue;
$success = $doc->getElementsByTagName('Description')->item(0)->nodeValue;

Related

PHP get data from multilevel tag colon XML file DOM PHP

I have question , how to parsing data from multilevel tag colon XML file with dom PHP.
Below is my XML sample data. I want to get data inside < transfer > and return as an array data
<soapenv:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<HostCustomerResponse xmlns="http://xx.xx.xx.xx">
<ns1:output xmlns:ns1="http://xx.xx.xx" xmlns:ns2="some:url" xsi:type="ns2:Output">
<ns2:statusCode>00</ns2:statusCode>
<ns2:statusMsg/>
<ns2:txnResponseDateTime>20190625164236</ns2:txnResponseDateTime>
<ns2:txnData>
<transferRequest>
<transfer>
<transferId>123456789</transferId>
<txnDate>123456789</txnDate>
<debitAcctNo>123456789</debitAcctNo>
<benAcctNo>123456789</benAcctNo>
</transfer>
</transferRequest>
</ns2:txnData>
</ns1:output>
</HostCustomerResponse>
</soapenv:Body>
</soapenv:Envelope>
and this result i want.
array(
[transferID] => 123456789,
[txnDate] => 123456789,
.....
)
Not sure if http://xx.xx.xx.xx is the real namespace in
<HostCustomerResponse xmlns="http://xx.xx.xx.xx">
but as this defines the namespace for any default elements (i.e. the ones your after) then you need to load the source XML and then register this namespace. Then you can use XPath to find the <transfer> element. You then just iterate through the elements within that ( using children() and add them into the output array...
$xml = simplexml_load_string($source);
$xml->registerXPathNamespace("d", "http://xx.xx.xx.xx");
$transfer = $xml->xpath("//d:transfer")[0];
$output = [];
foreach ( $transfer->children() as $key=> $value ) {
$output[$key] = (string)$value;
}
Your original XML is missing the definition of the soapenv namespace, so I added that to make the XML correct...
<soapenv:Envelope xmlns:soapenv="http://soapev.com"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<HostCustomerResponse xmlns="http://xx.xx.xx.xx">
<ns1:output xmlns:ns1="http://xx.xx.xx" xmlns:ns2="some:url" xsi:type="ns2:Output">
<ns2:statusCode>00</ns2:statusCode>
<ns2:statusMsg/>
<ns2:txnResponseDateTime>20190625164236</ns2:txnResponseDateTime>
<ns2:txnData>
<transferRequest>
<transfer>
<transferId>123456789</transferId>
<txnDate>123456789</txnDate>
<debitAcctNo>123456789</debitAcctNo>
<benAcctNo>123456789</benAcctNo>
</transfer>
</transferRequest>
</ns2:txnData>
</ns1:output>
</HostCustomerResponse>
</soapenv:Body>
</soapenv:Envelope>
You can get the transfer node by using the following code snippet
$response = preg_replace("/(<\/?)(\w+):([^>]*>)/", "$1$2$3", $soapXMLResult);
$xml = new SimpleXMLElement($response);
$body = $xml->xpath('//soapenvBody')[0];
$array = json_decode(json_encode((array)$body), TRUE);
$transfer = $array['HostCustomerResponse']['ns1output']['ns2txnData']['transferRequest']['transfer'];
https://3v4l.org/UKnZs

How i can get sessionid from this SOAPXML

I want to get sessionid from this XML piece of code:
<soapenv:envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:header>
<soapenv:body>
<p725:loginresponse xmlns:p725="http://www.fleetboard.com/data">
<p725:loginresponse sessionid="0001nABbah-I8f75oDrVbHrBgOv:s96fb0a4m3"></p725:loginresponse>
</p725:loginresponse>
</soapenv:body>
</soapenv:header>
</soapenv:envelope>
I have tried this but this doesn't work:
$soap=simplexml_load_string($result);
$xml_response = $soap->children('http://schemas.xmlsoap.org/soap/envelope/')->Body()->children()->p725;
echo $session_id = (int) $xml_response->session_id;
There are two ways to do this. The first is as you are currently doing it, but this involves various changes of namespace and means you need to keep on getting the right child elements and the attribute itself...
$soap=simplexml_load_string($result);
$xml_response = $soap->children("http://schemas.xmlsoap.org/soap/envelope/")->header->body;
$session_id = $xml_response->children("http://www.fleetboard.com/data")->loginresponse->loginresponse;
echo $session_id->attributes()->sessionid.PHP_EOL;
Or you can use XPath, where you will need to register the namespace with the document first and then select the loginresponse element with a sessionid element. This will return a list of matches, so you have to take the first one using [0]...
$soap=simplexml_load_string($result);
$soap->registerXPathNamespace("p725", "http://www.fleetboard.com/data");
$session_id = $soap->xpath("//p725:loginresponse/#sessionid");
echo $session_id[0];

Read Array Object from WSDL XML with namespaces in PHP

I am sending array of objects in XML Soap response from my java code to php in my project using WSDL.
I want to store values in php for my project.
I tried many ways but couldn't able to find how to parse my xml and read values. I am not export in xml area.
Please anyone help me for read my values from values.
My SOAP Response Body:
<soapenv:envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" \xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:body>
<getactualtimerequestresponse xmlns="http://impl.sample.com">
<getactualtimereturn>.
<ns1:projectlist xmlns:ns1="http://response.sample.com">
<item>
<ns2:userid xmlns:ns2="http://request.sample.com">4</ns2:userid>
<ns3:username xmlns:ns3="http://request.sample.com">Manoj Arun</ns3:username>
</item>
<item>
<ns5:userid xmlns:ns5="http://request.sample.com">5</ns5:userid>
<ns6:username xmlns:ns6="http://request.sample.com">Sethu Raman</ns6:username>
</item>
</ns1:projectlist>
<ns10:message xsi:nil="true" xmlns:ns10="http://response.sample.com"></ns10:message>
</getactualtimereturn>
</getactualtimerequestresponse>
</soapenv:body>
</soapenv:envelope>
projectList is my object created in java.
In PHP:
I tried to read like below but i didn't got anything.
foreach($xml->xpath('//ns:projectList') as $header)
{
foreach($header->item as $userIds)
{
echo $userIds->xpath('//ns:userId');
}
}
Thanks in advance...
$client = new SoapClient('http://url.com?wsdl');
$params = array(Java arg => php value);
$result = $client->Function( $parms );
print_r($result);

How to get values from xml output using PHP

How to get values from below result using PHP.
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Header/>
<soapenv:Body>
<p558:registerDonorResponse xmlns:p558="http://ws.ots.labcorp.com">
<p558:registerDonorReturn xmlns:p118="http://data.ws.ots.labcorp.com">
<p118:clientRegistrationId>clr1</p118:clientRegistrationId>
<p118:labcorpRegistrationNumber>100059064</p118:labcorpRegistrationNumber>
<p118:registrationTime>2012-12-01T05:40:51.628Z</p118:registrationTime>
</p558:registerDonorReturn>
</p558:registerDonorResponse>
</soapenv:Body>
</soapenv:Envelope>
Thanks.
Your XML contains namespace-prefixed tags as it's common for SOAP responses.
Have a look at following comment from php's SimpleXML docs:
$xml = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<people xmlns:p="http://example.org/ns" xmlns:t="http://example.org/test">
<p:person id="1">John Doe</p:person>
<p:person id="2">Susie Q. Public</p:person>
</people>
XML;
$sxe = new SimpleXMLElement($xml);
$ns = $sxe->getNamespaces(true);
$child = $sxe->children($ns['p']);
foreach ($child->person as $out_ns)
{
echo $out_ns;
}
In your case code accessing the properties should look like that (it's tested against your XML in so.xml file):
<?php
$xml = file_get_contents('so.xml');
$sxe = simplexml_load_string($xml);
$ns = $sxe->getNamespaces(true);
$child =
$sxe->children($ns['soapenv'])->
Body->children($ns['p558'])->
registerDonorResponse->registerDonorReturn->children($ns['p118']);
var_dump($child);
Result:
$ php -f so.php
object(SimpleXMLElement)#4 (3) {
["clientRegistrationId"]=>
string(4) "clr1"
["labcorpRegistrationNumber"]=>
string(9) "100059064"
["registrationTime"]=>
string(24) "2012-12-01T05:40:51.628Z"
}
Please note however that issuing SOAP requests and parsing responses by hand is generally a bad practice, consider using SOAP client for that.
What have you tried?
Anyway you can use PHP's simplexml_load_file or the full featured DOMDocument class

Extract values from soapenv envelope xml in php

i am new to XMl. i want to extract the status values in the following xml .i have no idea how to do that in php . this is a response that i ma getting from a API call.
<soapenv:envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:body>
<savesalesorderresponse xmlns="http://www.smartturn.com/services/OccamService/sales-order">
<uploadresponse>
<ns1:externalrefid xmlns:ns1="http://www.smartturn.com/services/occamtypes">007</ns1:externalrefid>
<ns2:status xmlns:ns2="http://www.smartturn.com/services/occamtypes">SUCCESS</ns2:status>
<ns6:systemid xmlns:ns6="http://www.smartturn.com/services/occamtypes">SO-059241</ns6:systemid>
</uploadresponse>
</savesalesorderresponse>
</soapenv:body>
</soapenv:envelope>
solution code will be appreciated
thanks in advance
All you need to do is Register namespaces with registerXPathNamespace
$xml = new SimpleXMLElement($data);
$xml->registerXPathNamespace("ns", "http://www.smartturn.com/services/occamtypes");
$status = $xml->xpath('//ns:status');
$status = (string) $status[0];
print($status);
Output
SUCCESS
The easiest way is to use SimpleXML

Categories