Parse SOAP with namespaces using php - 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'));

Related

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

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}";
}

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

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

Converting string to array when using tag xmlns:soapenv

I have the follwing string:
$string='<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:bac="http://backupbank.com/"> <soapenv:Header/> <soapenv:Body>
<bac:CreateUser>
<bac:request>
<!--Optional:-->
<bac:username>anandneema</bac:username>
<bac:product>Workstation</bac:product>
<bac:credentials>
<bac:server>serveripaddress</bac:server>
<bac:user>username</bac:user>
<bac:password>password</bac:password>
</bac:credentials>
</bac:request>
</bac:CreateUser> </soapenv:Body> </soapenv:Envelope>';
$xmldata=simplexml_load_string($string);
echo "<pre>"; print_r($xmldata);
It is not parsing the data.
But when I use:
$string='<soapenv:Envelope> <soapenv:Header/> <soapenv:Body>
<bac:CreateUser>
<bac:request>
<!--Optional:-->
<bac:username>anandneema</bac:username>
<bac:product>Workstation</bac:product>
<bac:credentials>
<bac:server>serveripaddress</bac:server>
<bac:user>username</bac:user>
<bac:password>password</bac:password>
</bac:credentials>
</bac:request>
</bac:CreateUser> </soapenv:Body> </soapenv:Envelope>';
$xmldata=simplexml_load_string($string); echo "<pre>"; print_r($xmldata);
Actually when I am removing the attributes: xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:bac="http://backupbank.com/"
Then it parses the data
Can any one suggest to me what the problem might be?
Don't have much idea on this but try calling the service by properly format the xml as string then use CURL may be something like in this quesion
Handling web service errors within PHP

Categories