BACK TO BLOG

MTOM SoapClient PHP package

Published on July 16, 2020

I created my first PHP package in 2016, after developing the SoNice Étiquetage module for PrestaShop, that make it easier to work with MTOM response with SoapClient.

Indeed, the module send orders information to an API then fetch the generated delivery label (and some other documents). I had an issue at the time, the API used MTOM and SoapClient for PHP does not deal with it so I had to parse the raw response from the API to extract the raw PDF source code and the XML response.

That's how I got the idea of creating a package to share it to the world as at the time there was no real solution to deal with that.

The package is available on GitHub and Packagist, its name: mtomsoapclient.

How does it work ?

I believe the GitHub summary is quite self explanatory:

This class overrides SoapClient::__doRequest() method to implement MTOM for PHP. It decodes XML and integrate attachments in the XML response.

It behave the same as Decorator. It simply get the response from parent::__doRequest() method then parse the XML response to replace the <xop> elements by the actual base64 encoded contents.

Simple yet really convenient. It means this example response:

<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">
   <soap:Body>
      <ns2:myApiResponse xmlns:ns2=\"http://sls.ws.webservice.fr\">
         <label>
             <xop:Include href=\"cid:983c41d7-d699-4373-b8da-4815099ef250-3880@cxf.apache.org\" xmlns:xop=\"http://www.w3.org/2004/08/xop/include\"/>
        </label>
        <parcelNumber>6A11353659111</parcelNumber>
      </ns2:myApiResponse>
   </soap:Body>
</soap:Envelope>

Is replaced by this:

<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">
   <soap:Body>
      <ns2:myApiResponse xmlns:ns2=\"http://sls.ws.webservice.fr\">
        <label>EENUfn5DRCx+Q0[... base64 encoded content ...]</label>
        <parcelNumber>6A11353659111</parcelNumber>
      </ns2:myApiResponse>
   </soap:Body>
</soap:Envelope>

In your code, you can then get the content of <label> like so :

// No need to base64_decode, SoapClient does it automatically for you.
$label_raw_pdf = $response->Body->myApiResponse->label;

Statistics

I am happily surprised that after 4 years, the package has been downloaded more than 17,000 time on Packagist.

It did not have really much success until mid 2018 where the number of download started to rise.

It is pleasant to see that one of your package has been used in thousands of projects. If you used it in yours, I hope it made your life easier ;) .

Stats

Installation

Thanks to composer, installation is as simple as:

composer require debuss-a/mtomsoapclient:^1