How to attach files to soap envelopes using DIME and PHP

Enhancing the native PHP SOAP support for DIME requests

As you may be aware by now, PHP native SOAP implementation does not support DIME transfers (this implies that the one from Zend Framework(tm) does not support them either). Also, the PEAR soap implementation lacks enough documentation to make it work (at least, I couldn't from the client side ;))

In this article I'll show you my own solution, which is a little dirty but does the job. What I did is to use PEAR Net_Dime to create the message, and then post it via curl. Obviously this can be refined a lot, but it may help you in dire, time-lacking situations ;)

A sample DIME envelope request with attachments

So let's say you want to send a soap request with zero or more attachments, using DIME as the transport (I had this requirement when dealing with WoodWing(tm)). You will first need to get the text of the soap envelope request, like:

The plan, is to send this message using curl, with the following code:

And this is the code that put the pieces together and actually sends the message with the attachments:

Using the code to send DIME requests with attachments

Now, how to use it:

After running the request, and if everything went ok, you will have a SimpleXMLElement with the soap response. The request consists of a DIME message with the first attachment being the soap request itself, and the following are the attachment themselves.

Now you can upload your files using PHP and DIME

Quick, dirty, a little low-level, but effective. Lot to improve, but that's the subject of another article ;) This will let you quickly implement and/or test your dime client. Let's hope that php gets a little better soap support sometime.