How to parse SOAP response without SoapClient
Could you clarify which version of PHP you're using (4 or 5)? Also is there a particular reason why you don't want to/can't use PHP 5's SOAP extension? Knowing this information should help us to give you a better answer.
The reason the code sample above isn't working is that you're looking in the wrong namespace for the oproduct nodes. While the root node is contained in the SOAP namespace the oproduct ones are under the "http://v3.core.com.productserve.com/" namespace. You also need to use the namespace alias in the XPath query. Try this, although I haven't tested it:
$xml = simplexml_load_string($response);
$xml->registerXPathNamespace('ns', 'http://v3.core.com.productserve.com/');
foreach ($xml->xpath('//ns:oproduct') as $item)
{
// do something
}
Hopefully that will solve your immediate problem.
EDIT
Thanks for the clarification. Again, untested but maybe this would work:
$xml = simplexml_load_string($response);
$xml->registerXPathNamespace('soapenv', 'http://schemas.xmlsoap.org/soap/envelope/');
$xml->registerXPathNamespace('ns', 'http://v3.core.com.productserve.com/');
foreach ($xml->xpath('/soapenv:envelope/soapenv:body/ns:getproductlistresponse/ns:oproduct') as $item)
{
// do something
}
Perhaps you need to go from the root node to the soap-Env:body to the oproduct nodes in the query. Hopefully that will work.
Further edit:
I think I've just cracked this. Try the following code:
$xml = simplexml_load_string($response);
$ns = $xml->getNamespaces(true);
$soap = $xml->children($ns['soap-env']);
$getproductlistresponse = $soap->body->children($ns['ns1']);
foreach ($getproductlistresponse->children() as $item)
{
//This example just accesses the iid node but the others are all available.
echo (string) $item->iid . '<br />';
}
Not the prettiest code but it works. I was hoping to get this to work with an XPath query but it was beyond my rudimentary knowledge of XPath. Perhaps someone else can post an answer using XPath?
How to parse soap xml response in php and get the information from the string
Another possible solution is to use for example SimpleXML. You can register the namespace and use an xpath expression:
$source = <<<SOURCE
<?xml version="1.0" encoding="utf-8" ?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<GetInfoFromSendingResponse xmlns="http://test.test.com/">
<GetInfoFromSendingResult>{"SendingID":"2468","Subject":"Test","ID":"2468","CampaignID":"890","ForwardAddress":"test@test.ro","SendingTime":"1/14/2016 8:00:00 AM","SendLeadsToEmail":"0","LanguageID":"6","LeadsTestMode":true,"WebversionLink":"","Language":"FR"}</GetInfoFromSendingResult>
</GetInfoFromSendingResponse>
</soap:Body>
</soap:Envelope>
SOURCE;
$xml = simplexml_load_string($source);
$xml->registerXPathNamespace('test', 'http://test.test.com/');
$elements = $xml->xpath('//soap:Envelope/soap:Body/test:GetInfoFromSendingResponse/test:GetInfoFromSendingResult');
$result = json_decode($elements[0], true);
print_r($result);
Will result in:
Array
(
[SendingID] => 2468
[Subject] => Test
[ID] => 2468
[CampaignID] => 890
[ForwardAddress] => test@test.ro
[SendingTime] => 1/14/2016 8:00:00 AM
[SendLeadsToEmail] => 0
[LanguageID] => 6
[LeadsTestMode] => 1
[WebversionLink] =>
[Language] => FR
)
Parse XML data from PHP SOAP Response
This is the usual problem of having various namespaces which you need to navigate around. The root node defines xmlns:soap
so you can use that without having to do anything, so the XPath uses //soap:Body/*
to find the element inside the body tag, as xpath()
returns a list of matching nodes, use [0]
to just pick the only one out.
As the body data is all under a default namespace (defined as xmlns="http://webservice.nada.com/"
) you can extract all of them using $data->children("http://webservice.nada.com/")
. This now allows you to use standard object notation to access the values.
One thing to note is although echo
automatically converts it to a string, if you use these values elsewhere - you may need to convert it using (string)
as the item is in fact a SimpleXMLElement.
$data = <<< XML
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<getDefaultVehicleAndValueByVinResponse xmlns="http://webservice.nada.com/">
<getDefaultVehicleAndValueByVinResult>
<Uid>1182699</Uid>
<VehicleYear>2015</VehicleYear>
<MakeCode>47</MakeCode>
</getDefaultVehicleAndValueByVinResult>
</getDefaultVehicleAndValueByVinResponse>
</soap:Body>
</soap:Envelope>
XML;
$xml = simplexml_load_string($data);
$data = $xml->xpath("//soap:Body/*")[0];
$details = $data->children("http://webservice.nada.com/");
echo (string)$details->getDefaultVehicleAndValueByVinResult->VehicleYear;
Parsing a SOAP response with PHP in different ways
$doc = new DOMDocument();
$doc->loadXML( $yourxmlresponse );
$LoginResults = $doc->getElementsByTagName( "LoginResult" );
$LoginResult = $LoginResults->item(0)->nodeValue;
var_export( $LoginResult );
Soap Response not XML but String
I had some difficulties, if simple_xml has to deal with namespace prefix.
Try to remove
$clean_xml = str_ireplace(['SOAP-ENV:', 'SOAP:', 'ns2:'], '', $xml);
$xmlArray = simplexml_load_string($clean_xml);
$json = json_encode($xmlArray);
$array = json_decode($json,TRUE);
$value = $array['foo']['bar'];
The $array should contain the SOAP Result as an array
PHP Parse SOAP XML Response
xpath is your friend:
xpath('//Row');
Full example:
$soap = <<< LOL
<?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>
<ns3:GetDistrictByAddressResponse xmlns:ns3="http://il/co/bar/webservices/getdistrictbyaddress">
<TimeFrameTable>
<CustomerNumber>250</CustomerNumber>
<Row>
<WindowDate>10052016</WindowDate>
<WeekDay>Sunday</WeekDay>
<FromHour>1130</FromHour>
<ToHour>1430</ToHour>
</Row>
<Row>
<WindowDate>10052016</WindowDate>
<WeekDay>Sunday</WeekDay>
<FromHour>1430</FromHour>
<ToHour>1730</ToHour>
</Row>
</TimeFrameTable>
</ns3:GetDistrictByAddressResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
LOL;
$xml = simplexml_load_string($soap);
foreach ($xml->xpath('//Row') as $item)
{
print_r($item);
}
Output:
SimpleXMLElement Object
(
[WindowDate] => 10052016
[WeekDay] => Sunday
[FromHour] => 1130
[ToHour] => 1430
)
SimpleXMLElement Object
(
[WindowDate] => 10052016
[WeekDay] => Sunday
[FromHour] => 1430
[ToHour] => 1730
)
Ideone Demo
Related Topics
PHP Password_Verify Not Working with Database
How to Find Error Log Files for PHP
How to Swap Colors in Image Using Gd Library in PHP
Differencebetween Get_Result() and Store_Result() in PHP
Installing Imagemagick Extension with PHP/Windows
Subtracting a Certain Number of Hours, Days, Months or Years from Date
Inet_Aton() and Inet_Ntoa() in PHP
Laravel Error: Method Illuminate\View\View::_Tostring() Must Not Throw an Exception
Search and Replace Value in PHP Array
How to Check If a File Exists on a Remote Server Using PHP
Concat Columns with Laravel 5 Eloquent
Laravel 5 Socialite - Curl Error 77: Error Setting Certificate Verify Locations
How to Get All Checkbox Variables Even If Not Checked from HTML to PHP
How to Remove All Numbers from String
Flat PHP Array to Hierarchy Tree