Over a million developers have joined DZone.

Fixing Issue With PHPs SoapClient Overwriting Duplicate Attribute and Tag Names

DZone's Guide to

Fixing Issue With PHPs SoapClient Overwriting Duplicate Attribute and Tag Names

· Web Dev Zone ·
Free Resource

Read this guide to learn everything you need to know about RPA, and how it can help you manage and automate your processes.

The setting:

An SOAP request contains an Id attribute – and an element with the exact name in the response (directly beneath the element containing the attribute – an immediate child):

    <res z:Id="i123">

The problem is that the generated result object from the SoapClient (at least of PHP 5.2.12) contains the attribute value, and not the element value. In our case we could ignore the z:Id attribute, as it was simply an Id to identify the element in the response (this might be something that ASP.NET or some other .NET component does).

Our solution is to subclass the internal SoapClient and handle the __doRequest method, stripping out the part of the request that gives the wrong value for the Id field:

    class Provider_SoapClient extends SoapClient
        public function __doRequest($request, $location, $action, $version)
            $result = parent::__doRequest($request, $location, $action, $version);
            $result = preg_replace('/ z:Id="i[0-9]+"/', '', $result);
            return $result;

This removes the attribute from all the values (there is no danger that the string will be present in any other of the elements. If there is – be sure to adjust the regular expression). And voilá, it works!

Get the senior executive’s handbook of important trends, tips, and strategies to compete and win in the digital economy.


Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}