Stefan
Stefan

Reputation: 1258

PHP SOAP response contains an attribute with different xmlns and shows up empty

For the last few days I am cracking my brains on the following problem: I use the following code to send a request to the SOAP service:

$client = new SoapClient(WSDL,  array('soap_version'=> SOAP_1_2, 'trace' => 1));

$result = $client->getHumanResourceID(array(
    'cCode' => CLIENT_CODE,
    'hFilter' => array('deltaDatum' => '2010-01-01T00:00:00-00:00')
));

Partly var_dump($result) shows:

object(stdClass)#2 (1) {
  ["getHumanResourceIDResult"]=>
  object(stdClass)#3 (1) {
    ["EntityIdType"]=>
    array(4999) {
      [0]=>
      object(stdClass)#4 (2) {
        ["IdValue"]=>
        object(stdClass)#5 (0) {
        }
        ["idOwner"]=>
        string(8) "internal"
      }
      [1]=>
      object(stdClass)#6 (2) {
        ["IdValue"]=>
        object(stdClass)#7 (0) {
        }
        ["idOwner"]=>
        string(8) "internal"
      }

Something strange is happening in the objects of EntityType, it contains an object with idValue and idOwner. IdValue should contain other attributes or fields. On this moment I don't have any clue what to add or modify in order to receive those values.

A part of the raw SOAP response:

<?xml version="1.0" encoding="utf-8"?>
    <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <getHumanResourceIDResponse xmlns="http://soapWeb.org/">   
            <getHumanResourceIDResult>
                <EntityIdType idOwner="internal">
                    <IdValue xmlns="http://ns.hr-xml.org/2007-04-15">5429</IdValue>
                </EntityIdType>
            </getHumanResourceIDResult>
        </getHumanResourceIDResponse>
    </soap:Body>
</soap:Envelope>

What I noticed is the xmlns in the IdValue field and I can imagine that the returned object is null because the namespace is not included.

Any help and suggestions are more then appreciated!

Upvotes: 2

Views: 1174

Answers (1)

web-nomad
web-nomad

Reputation: 6003

Since the response contains multiple namespaces, you need to use registerXpathNamespaces (if using SimpleXML, for DOM, there are similar methods.) function to read all the values.

$xml = <<<XML
<?xml version="1.0" encoding="utf-8"?>
    <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <getHumanResourceIDResponse xmlns="http://soapWeb.org/">   
            <getHumanResourceIDResult>
                <EntityIdType idOwner="internal">
                    <IdValue xmlns="http://ns.hr-xml.org/2007-04-15">5429</IdValue>
                </EntityIdType>
            </getHumanResourceIDResult>
        </getHumanResourceIDResponse>
    </soap:Body>
</soap:Envelope>
XML;

$xml = simplexml_load_string( $xml );

$xml->registerXPathNamespace( 's', 'http://soapWeb.org/' );

$xpath = $xml->xpath( '//s:getHumanResourceIDResponse' );

foreach( $xpath as $node ) {
    $idValue = ( string ) $node->getHumanResourceIDResult->EntityIdType->IdValue;
    $idOwner = ( string ) $node->getHumanResourceIDResult->EntityIdType[ 'idOwner' ];

    echo 'IdValue : ' . $idValue . '<br />';
    echo 'IdOwner : ' . $idOwner;
}

Hope this helps.

Upvotes: 3

Related Questions