Writing a H2O web service client in PHP

Java and dot.Net are the programming language discussed most in the H2O discussion group, but this time, I would like to have it interact with something else. So, I headed to my second lover, PHP (my first lover is Ruby, btw). From there, I have a couple of choices, NuSOAP, Pear SOAP and PHP Extension. I have decided to use Pear SOAP, cause NuSOAP seems to be stopped from developing and PHP Extension is not something I am familiar with. With that said, let’s move forward and see how it works.

Installation, embrace beta packages

I am not drilling into the details on how to compile PHP with Pear support, google on this topic and you should get loads of responses. There are 2 things that I feel worth mentioning.
  1. Change the preferred_state to beta so that you can install the latest version of PEAR::SOAP.

  2. pear config-set preferred_state beta

  3. After that, install all the dependency for SOAP, including Mail_MIME and Mail_DIME, if NOT, you will not be able to send multipart message.

  4. pear install -alldeps SOAP

Let's do some coding now …

First of all, let's create a soap client and define the endpoint (i.e. the location of the H2O) that it will be connecting to.

$url=’http://localhost:8080/corvus/httpd/ebms/sender’; $soapclient = new SOAP_Client($url);

After that, prepare the parameter which you will be sending over in the SOAP message, this parameter will be the value of the partnership that you want to send / receive messages. You should encapsulate all the value in the class SOAP::Value and put them into an array. Notice on the last value that I am packaging, I am using the SOAP_Attachment object which is a child of the SOAP_VALUE.

$params =
array (new SOAP_VALUE(’cpaId’, ‘nonEmptyToken’, ‘cecid’),
……
new SOAP_Attachment(’SWA’,'text/xml’,’./dbhelper.xml’));

Along with the partnership value, you will also need to define on how the SOAP message should be delivered. This include the document type of the message, how the file should be sent over as, etc. All these settings will again be encapsulated in an array.

From the code below, notice that I am defining the attachment will be sent over as a MIME message, i.e. a multipart message.

$options =
array(‘namespace’ => “http://service.ebms.edi.cecid.hku.hk/”,
’soapaction’ => ”,
’style’ => ‘document’,
‘use’ => ‘literal’,
‘trace’ => 1,
‘timeout’ => 30000,
‘Attachments’=>’Mime’
);

With these, we can initiate the call!

$resp = $soapclient->call(’query’, $params, $options);

OK, now I know how to send, but what about receive?

Similar to the loopback test, you have to get a list of received message from H2O, and retrieve the payload in each of them by the message ID. The code needed in getting the receiver list is very similar to the send method. And the interesting part is how to get the payload from the SOAP message.

First, you will initiate a call to retrieve the SOAP message

$msg = $soapclient->call('query', $params_rec, $options);

And then, you can get the attachment by the code below. I am now displaying the content of the payload, you may want to use other handling method, like diverting it as a stream for the user to download as a file.

var_dump($soapclient->_soap_transport->attachments);

I have prepared a set of PHP in mimicking the send and receive action in the loopback test. You can get it from this location.



Posted by Ronnie Kwok on 07/17 at 03:09 PM