Search code examples
phpsimplexml

PHP SimpleXML obtaining data


I have a response in the following format

<?xml version='1.0'?>
<SOAP-ENV:Envelope xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:ns='urn:xtk:myQyery' xmlns:SOAP-ENV='http://schemas.xmlsoap.org/soap/envelope/'>
<SOAP-ENV:Body>
    <ExecuteQueryResponse xmlns='urn:xtk:myQyery' SOAP-ENV:encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'>
        <pOutput xsi:type='ns:Element' SOAP-ENV:encodingStyle='http://xml.apache.org/xml-soap/literalxml'>
            <recipient email="[email protected]"/>
        </pOutput>
    </ExecuteQueryResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

So first thing I do is load the string

public function checkResponse($serverResponse) {
    $xml = simplexml_load_string($serverResponse);

    if($xml->children('SOAP-ENV', true)->Body->Fault) {
        return false;
    }
    else {
        return true;
    }
}

The first thing I do is look for a fault, and if there is one, return false. If everything is ok, I need to return the email address from the xml. I have tried the following without success

return $xml->children('SOAP-ENV', true)->Body->ExecuteQueryResponse->pOutput->recipient;

How would I go about getting the email address?

Thanks


Solution

  • email is an attribute of the 'recipient' node.

    You can try this:

    return $xml->children('SOAP-ENV', true)->Body->ExecuteQueryResponse->pOutput->recipient->attributes()->email
    

    http://php.net/simplexmlelement.attributes