Read Array Object from WSDL XML with namespaces in PHP - 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);

Related

Parse SOAP with namespaces using php

I am trying to parse the following SOAP using PHP. I have tried every possible solution found in here but I did not manage it due to the namespaces used. Can please someone help?
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:geo="http://path.to.geo" xmlns:geo1="http://path/">
<soapenv:Header/>
<soapenv:Body>
<geo:SendClient>
<geo1:SendClientRequest>
<geo1:GeneralInfo>
<geo1:Team>AP</geo1:Team>
</geo1:GeneralInfo>
</geo1:SendClientRequest>
</geo:SendClient>
</soapenv:Body>
</soapenv:Envelope>
I want to get the value AP in the Output.
$xmlData = simplexml_load_file('request.xml');
$xmlData->registerXPathNamespace('geo1', 'http://path/');
foreach ($xmlData->xpath('//geo1:GeneralInfo') as $item)
{
print_r($item);
var_export($item->xpath('//geo1:Team'));
}
After spending hours I found out that the right way to print the output is:
$result = $item->xpath('//geo1:Team');
echo (string)$result[0];
instead of
var_export($item->xpath('//geo1:Team'));

Parse XML using PHP with multiple namespaces

I have an xml file which i am having a hard time to correctly parse:
I need to retrieve the Value from the xml file
<?xml version="1.0" encoding="UTF-8"?>
<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>
<ns1:PerfmonCollectCounterDataResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="http://schemas.cisco.com/ast/soap/">
<ArrayOfCounterInfo soapenc:arrayType="ns1:CounterInfoType[2]" xsi:type="soapenc:Array" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
<item xsi:type="ns1:CounterInfoType">
<Name xsi:type="ns1:CounterNameType">\\172.16.106.18\Number of Replicates Created and State of Replication(ReplicateCount)\Number of Replicates Created</Name>
<Value xsi:type="xsd:long">603</Value>
<CStatus xsi:type="xsd:unsignedInt">1</CStatus>
</item>
<item xsi:type="ns1:CounterInfoType">
<Name xsi:type="ns1:CounterNameType">\\172.16.106.18\Number of Replicates Created and State of Replication(ReplicateCount)\Replicate_State</Name>
<Value xsi:type="xsd:long">2</Value>
<CStatus xsi:type="xsd:unsignedInt">1</CStatus>
</item>
</ArrayOfCounterInfo>
</ns1:PerfmonCollectCounterDataResponse>
</soapenv:Body>
</soapenv:Envelope>
Below is the code that I am running:
// fclose($fopen);
// if (file_exists($filename)) {
// $output = simplexml_load_file($filename,null,null,"http://www.w3.org/2001/XMLSchema-instance",true); // I have also tried without the namespaces
;
//$output = simplexml_load_string($xmldata);
//var_dump($output);
//print_r($output);
} else {
echo "File not found";
}
Below is the empty object I am getting:
object (SimpleXMLElement)[3]
SimpleXMLElement Object ( )
Pls help.
That actually is not an empty object, and has methods against it. This is expected. If you did
echo $output->asXML();
You will see your XML string returned.
Now perform methods against this to iterate the data set and retrieve your specific values.
Also, if you're dealing with debugging simplexml frequently, consider this function

Parse soap xml document 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;

Recreate a working SOAP request from SoapUI to PHP

I have created a soap client in PHP that signs on, but for the second request I want to make I cannot seem to get the PHP to structure the request properly.
This is request that works in SoapUI
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v1="http://v1.productapi.gs1ca.org" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Header/>
<soapenv:Body>
<v1:searchProducts>
<sessionId>f53c5450-392e-4ca4-b592-adbb436cfe1f</sessionId>
<searchCriteria>
<v1:AttributeValue>
<v1:attribute>dateupdated</v1:attribute>
<v1:value i:type="b:string" xmlns:b="http://www.w3.org/2001/XMLSchema">08/01/2013</v1:value>
</v1:AttributeValue>
<v1:AttributeValue>
<v1:attribute>dateupdatedcompare</v1:attribute>
<v1:value i:type="b:string" xmlns:b="http://www.w3.org/2001/XMLSchema">1</v1:value>
</v1:AttributeValue>
</searchCriteria>
<includeImageAttributes>0</includeImageAttributes>
<sortOrder>dateupdated</sortOrder>
</v1:searchProducts>
</soapenv:Body>
</soapenv:Envelope>
How would I use PHP to format the XML the same way as the working request?
Some progress has been made.
I have been able to recreate the xml up to a point now. The request looks like this:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://v1.productapi.gs1ca.org">
<SOAP-ENV:Body>
<ns1:searchProducts>
<sessionId>2a7d0294-8d96-428d-abd8-08add9cfc427</sessionId>
<searchCriteria>
<ns1:AttributeValue>
<ns1:attribute>dateupdated</ns1:attribute>
<ns1:value>01/01/2013</ns1:value>
</ns1:AttributeValue>
<ns1:AttributeValue>
<ns1:attribute>dateupdatedcompare</ns1:attribute>
<ns1:value>1</ns1:value>
</ns1:AttributeValue>
</searchCriteria>
<includeImageAttributes>false</includeImageAttributes>
<sortOrder>dateupdated</sortOrder>
</ns1:searchProducts>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
The PHP creating that request is:
$args0 = array(
'sessionid'=>$session,
'searchcriteria'=> array(array('attribute'=>'dateupdated','value'=>'01/01/2013'),array('attribute'=>'dateupdatedcompare','value'=>'1')),
'includeimageattributes'=>0,
'sortorder'=>'dateupdated');
$result = $client->__soapCall('searchProducts',$args0);
The error this throws is:
Error: SoapFault exception: [a:DeserializationFailed] The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter http://v1.productapi.gs1ca.org:searchCriteria. The InnerException message was 'Element value from namespace http://v1.productapi.gs1ca.org cannot have child contents to be deserialized as an object. Please use XmlNode[] to deserialize this pattern of XML.
I am still missing a portion of the envelope:
xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
And I need the value tags to look like this:
<v1:value i:type="b:string" xmlns:b="http://www.w3.org/2001/XMLSchema">
Any ideas on how I can add those portions in?
Okay! I Finally figured this out. It is ugly but it works.
try {
$args = array(
'sessionid'=>$session,
'searchcriteria'=> new SoapVar('<searchCriteria><ns1:AttributeValue>
<ns1:attribute>dateupdated</ns1:attribute>
<ns1:value xsi:type="b:string" xmlns:b="http://www.w3.org/2001/XMLSchema">01/01/2013</ns1:value>
</ns1:AttributeValue>
<ns1:AttributeValue>
<ns1:attribute>dateupdatedcompare</ns1:attribute>
<ns1:value xsi:type="b:string" xmlns:b="http://www.w3.org/2001/XMLSchema">1</ns1:value>
</ns1:AttributeValue></searchCriteria>
', XSD_ANYXML, "http://www.w3.org/2001/XMLSchema-instance"),
'includeimageattributes'=>0,
'sortorder'=>'dateupdated');
$result = $client->__soapCall('searchProducts',$args);
} catch (SoapFault $e) {
echo "Error: {$e}";
}

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