I have this xml bellow :
"<?xml version='1.0' encoding='UTF-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><soapenv:Fault><faultcode>soapenv:Server</faultcode><faultstring>For input string: ""</faultstring><detail /></soapenv:Fault></soapenv:Body></soapenv:Envelope>"
I want to get the content of Body > Fault > faultcode, faultstring
But idk why doens’t work
My source code :
$xml = "<?xml version='1.0' encoding='UTF-8'?><soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'><soapenv:Body><soapenv:Fault><faultcode>soapenv:Server</faultcode><faultstring>For input string: '</faultstring><detail /></soapenv:Fault></soapenv:Body></soapenv:Envelope>";
$xmlObject = simplexml_load_string($xml, "SimpleXMLElement", LIBXML_NOCDATA);
$namespaces = $xmlObject->getNamespaces(true);
$body = $xmlObject->children($namespaces['soapenv'])->Body->Fault->faultcode;
dd($body);
output :
SimpleXMLElement {#1890}
2
Answers
I think your mistake is how you get the "faultstring" property. Remember that you are setting the "soapenv" namespace and the "faultcode" property does not have this namespace. This is the example I was trying:
This is the result:
Screenshot of result
I also leave you an example of how I think it would be easier to obtain the value you need from the "faultstring" property:
This is the result:
Screenshot of result
There is no
<faultcode>
in thesoapenv
namespace so you need to switch namespaces:Full working example (with damaged XML corrected):
On a side note, there is a dedicated SOAP extension though, admittedly, it isn’t as intuitive as it could.