Have you ever needed to upload or download a file over SOAP? There’s a really easy way to handle it, packaging a base-64 encoded string representing the file as a simple string type. Simple, but what if you’re working on a SOAP client, and the server is using the SOAP with Attachments (SwA) paradigm to send and receive files? I recently had this experience with a PortaOne integration, which uses SOAP::Lite for Perl to provide SOAP services to the switch.
SOAP with Attachments
What is SwA anyway? I started Googling. SwA is a hybrid SOAP / MIME technology, that packages SOAP requests / responses inside a MIME document. MIME documents already have the ability to support attachments (binary even) and sections of text. The idea behind SwA is to create a Multipart/Related MIME document, one portion of the document containing the SOAP XML, and any number of attachments as embedded binary or base-64 encoded files.
SOAP with Attachements and PHP
Using PHP5, naturally, everything I had already written against the PortaOne service was implemented with SoapClient, and come to find out, the native PHP SoapClient class doesn’t have support for SwA! Searching for SwA and SoapClient, I found plenty of questions on php-general, Stack Overflow, etc. “Where is the SwA support”, people will ask. The most common suggestion is trying to base64 encode a file and pass it as a simple string type over SOAP. While this is technically a way to send files over SOAP, it is not the SwA paradigm; SwA uses MIME documents. The second most common suggestion is to use NuSOAP.
NuSOAP is a classic library for PHP that pre-dates the native SoapClient class which debuted with PHP 5. The library is written entirely in PHP and therefore has a number of common trade-offs.
SoapClient vs NuSOAP
|Built-in to PHP||Yes||No|
|Readable / Editable||No
(plain PHP code)
You’ll notice that SoapClient doesn’t support SwA, however, generally when running PHP5, SoapClient is preferable. My goal was to implement SwA with SoapClient for the Porta One integration. I sat down and read the W3C spec on SwA. Not bad really the big question though in terms of implementing SwA for SoapClient is how to deal with the MIME component. The main interest in SoapClient is that it’s native, meaning it’s compiled C code. As long as the SOAP extension is installed and enabled, there is no need for require_once calls etc to load PHP code. Compiled code also happens to be faster than userspace PHP code. In similar vein, to the main objective, I wanted to be able to work with MIME documents using PHP extensions.