Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

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

Learn how to build modern digital experience apps with Crafter CMS. Download this eBook now. Brought to you in partnership with Crafter Software

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">
      <Id>foobar</Id>
    </res>

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!

Crafter is a modern CMS platform for building modern websites and content-rich digital experiences. Download this eBook now. Brought to you in partnership with Crafter Software.

Topics:

Published at DZone with permission of Mats Lindh, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

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

{{ parent.tldr }}

{{ parent.urlSource.name }}