Home » Php » php – Making SOAP Clients more fault tolerant when using class mapping

php – Making SOAP Clients more fault tolerant when using class mapping

Posted by: admin July 12, 2020 Leave a comment

Questions:

I’m writing a PHP wrapper for a SOAP service and using the class map option so I can work with objects instead of arrays. This works great until the provider of the SOAP service decides to add a new property to one of their objects (which they seem to like doing without telling anyone) at which point the SOAP client breaks with the error SOAP-ERROR: Encoding: object has no 'NewlyAddedProperty' property as there’s now a property in the wsdl that isn’t in the class.

I’ve tried to work around it by using a magic getter and returning things like false, null etc but as they’re not valid values according to the wsdl it still throws up a fatal error.

In an ideal world the client would just set that new property on the object even if there isn’t a property defined on the class, is that possible? If not just suppressing the error would also be fine.

How to&Answers:

You can remove SoapClient‘s class mapping and use another one, eg jsonmapper.

If your classes are annotated correctly, it’s as simple as this example:

class noBisnodePersonClient extends SoapClient {

    // ...

    /**
     * sokPerson
     * @param noBisnodePersonSok $PersonSokReq
     * @return noBisnodePersonSokResponse
     */
    public function sokPerson(noBisnodePersonSok $PersonSokReq){
        $return = $this->__soapCall(
            'sokPerson',
            array($PersonSokReq),
            array('uri'=>'http://dbonline.no/webservices/wsdl/PersonInfo')
        );
        $mapper = new JsonMapper();
        $object = $mapper->map($return, new noBisnodePersonSokResponse);
        return $object;
    }
}

Answer:

SOAP means XML over HTTP – the owner of the SOAP service cannot and should not know all the systems calling it.

This brittleness is built into the SOAP interface. The service should validate incoming requests against its schema. It’ll fail unless they make that path optional. There’s no ‘duck typing’ built in.