skip to Main Content

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


  1. 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:

    <?php
    
    $xml = <<<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>
    XML;
    
    $xmlObject = simplexml_load_string($xml);
    $namespaces = $xmlObject->getNamespaces(true);
    $body = $xmlObject->children($namespaces['soapenv'])->Body->Fault;
    
    print_r($body->xpath('faultstring'));
    

    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:

    <?php
    
    try {
        $dom = new DOMDocument();
        $dom->loadXML($xml);
        $xpath = new DOMXPath($dom);
    
        // Execute XPath query to get "faultstring" property value
        $faultString = $xpath->evaluate('//soapenv:Fault/faultstring');
    
        var_dump($faultString->item(0)->nodeValue);
    } catch (Exception $e) {
        echo "Error: " . $e->getMessage();
    }
    

    This is the result:
    Screenshot of result

    Login or Signup to reply.
  2. There is no <faultcode> in the soapenv namespace so you need to switch namespaces:

    $xmlObject->children($namespaces['soapenv'])->Body->Fault->children()->faultcode
    

    Full working example (with damaged XML corrected):

    $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);
    $faultcode = $xmlObject->children($namespaces['soapenv'])->Body->Fault->children()->faultcode;
    var_dump((string)$faultcode);
    
    string(14) "soapenv:Server"
    

    On a side note, there is a dedicated SOAP extension though, admittedly, it isn’t as intuitive as it could.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search