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 error monitoring with Sentry closes the gap between the product team and your customers. With Sentry, you can focus on what you do best: building and scaling software that makes your users’ lives better.

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!

What’s the best way to boost the efficiency of your product team and ship with confidence? Check out this ebook to learn how Sentry's real-time error monitoring helps developers stay in their workflow to fix bugs before the user even knows there’s a problem.

Topics:

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}