A Web Service Client in PEAR SOAP
In the preparation of my next episode on Hermes 2, I have been struggling on how to develop a web service client in technology other than Java. My primary candidate is definitely ruby, but I have to give up on it after playing it for a while. I just don't know how to construct a MIME message in Soap4r.
So, I headed to my second lover, PHP. 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.
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.
OK ok ... First of all, create a new soap client and define the endpoint that it will be connecting to.
After that, prepare the parameter which you will be sending over in the SOAP message, this parameter should be those required by the web service server for program execution. 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.
Along with the program parameter, 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.
With these, we can initiate the call!
Yes, it doesn't work to me either, until I read this.
I modified my SOAP_Base::_makeMimeMessage as instructed. Notice about the double quote surrounding "text/xml"? Axis reject the SOAP request if it is un-quoted.
to
Ta-da, it works ok now!
So, I headed to my second lover, PHP. 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.
- Change the preferred_state to beta so that you can install the latest version of PEAR::SOAP.
- 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.
pear install -alldeps SOAP
pear config-set preferred_state beta
Hurray! Now show me some code ...
OK ok ... First of all, create a new soap client and define the endpoint that it will be connecting to.
$url='http://images.vsmile.com: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 should be those required by the web service server for program execution. 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 program parameter, 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);
Damn it! It doesn't work!
Yes, it doesn't work to me either, until I read this.
I modified my SOAP_Base::_makeMimeMessage as instructed. Notice about the double quote surrounding "text/xml"? Axis reject the SOAP request if it is un-quoted.
$params = array('content_type' => 'multipart/related;
type=text/xml');
to
$params = array('content_type' => 'multipart/related;
type="text/xml"');
Ta-da, it works ok now!