Extract values from soapenv envelope xml in php - 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

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

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

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 add prefix and URI on xml objects (PHP)

in this sample found here:
<?php
$newsXML = new SimpleXMLElement("<news></news>");
$newsXML->addAttribute('newsPagePrefix', 'value goes here');
$newsIntro = $newsXML->addChild('content');
$newsIntro->addAttribute('type', 'latest');
Header('Content-type: text/xml');
echo $newsXML->asXML();
?>
We create an xml object from scratch. all righ.
my question is, how to embed a prefix in a tag during the construction of the object?
<?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:Header />
<soapenv:Body>
<EnviarLoteRpsEnvio xmlns="http://www.betha.com.br/e-nota-contribuinte-ws">
...some important xml...
</EnviarLoteRpsEnvio>
</soapenv:Body>
</soapenv:Envelope>
in this code above, show a final xml, how, during the create of xml object, i embed the prefixes in the tags? sorry for my bugged english..
thanks for all help.

Categories