|
Programming webservices in PHP, I met the library known as NuSOAP [1]. Even though, I'm not an expert in the field, it turned out to be very helpful in building webservice. In brief, given a set of already existing methods implementing some logic of your program, the library allows you to concentrate on the few missing steps in order to publish those methods and let them to be reached as webservices. Those steps are: - describe the input and output data for each method
- register the name with which the methods will be exposed, as webservices
- bind the names defined above with actual PHP (already defined) functions or methods
- given all the above descriptions, automatically generate and export the WSDL interface for the webservice
Nevertheless you may need an extra functionality that NuSOAP does not provide (as far as I know): you may want to encode the data just before sending them to the webservice as input, and decode them as soon as they are returned as output of the webservice itself. In my opinion, this functionality should be parameterized: the developer should be able to define its own encoder/decoder, pass it to the client or the server he/she is developing, and expect that such encoder/decoder will be used. As the object oriented paradigm teach us, we just need to extend the existing library in a new library that allow such functionality. Let us see how to do that. Preparing the encoders and decodersInitially, we create a type (i.e. a class) for the encoders and decoders. We shall define two basic classes, for the encoders and the decoders, respectively.Successively we shall extend them and realize the actual classes that will encode and decode the data following the base-64 standard. // your path for nusoap.php may be different require_once 'nusoap/nusoap.php';
class simple_soap_encoder { function encode(&$value) { // do nothing } }
class simple_soap_decoder { function decode(&$value) { // do nothing } }
class base64_soap_encoder extends simple_soap_encoder { function encode(&$data) { if (is_string($data) || is_int($data)) { $data = base64_encode($data); } else if (is_array($data)) { $encFunction = create_function('$value,$key', '$value = base64_encode($value);' ); array_walk($data, $encFunction); } else { throw new Exception("base64_soap_encoder::encode : ". "only 'string', 'int' ". "and 'array' types are accepted"); } } } class base64_soap_decoder extends simple_soap_decoder { function decode(&$data) { if (is_string($data) || is_int($data)) { $data = base64_decode($data); } else if (is_array($data)) { $decFunction = create_function('$value,$key', '$value = base64_decode($value);' ); array_walk($data, $decFunction); } else { throw new Exception("base64_soap_decoder::decode : ". "only 'string', 'int' ". "and 'array' types are accepted"); } } } Extending the library Our purpose is not to modify the existing code of the NuSOAP library. Still, we read it carefully in order to understand what to modify. Our purpose is to wrapper the existing library in a sort of shell. In our case, it turned out that this goal can be reached extending two classes: nusoap_client and nusoap_server, thus we'll have: class extended_client extends nusoap_client { private $field_encoder; private $field_decoder;
public function __construct() { $this->field_encoder = new simple_soap_encoder(); $this->field_decoder = new simple_soap_decoder(); parent::nusoap_client(); }
public function set_field_encoder(simple_soap_encoder $field_encoder) { $this->field_encoder = $field_encoder; }
public function set_field_decoder(simple_soap_decoder $field_decoder) { $this->field_decoder = $field_decoder; } ... }
class extended_server extends nusoap_server { private $field_encoder; private $field_decoder;
public function __construct() { $this->field_encoder = new simple_soap_encoder(); $this->field_decoder = new simple_soap_decoder(); parent::nusoap_server(); }
public function set_field_encoder(simple_soap_encoder $field_encoder) { $this->field_encoder = $field_encoder; }
public function set_field_decoder(simple_soap_decoder $field_decoder) { $this->field_decoder = $field_decoder; } ... } Appendix - The librarySoon you will be able to download the library I've written for implementing the encode/decode functionality.
Aggiungi questa pagina al tuo Social Network preferito
|
un saluto a tutti mi chiamo claudia ho 40 anni e 3 figli ,sono circa due anni che mi hanno diagnosticato il cushing ( che dicono probabilmente io ho da circa vent'anni)dopo molte analisi (urine ,sa...
Sono sicuro che sarete di grande conforto ed aiuto a tante persone. Auguri per questa utile e disinteressata iniziativa Giorgio Arnaldi
Ciao Angelica all'interno del complesso del San Raffaele c'è un albergo. Dovresti controllare sul loro sito. Io ho controllato qualche settimana fa, trovi anche i numeri telefonici. Ero lì un mes...
ciao riccardo, sono contento per il tuo interesse, e` bello confrontarsi anche con qualcuno che di medicina ne sa
sono interessato alla Vs. newsletter come medico