Recipe 18.12 Writing an XML-RPC Client18.12.1 ProblemYou want to write a client for an XML-RPC service. 18.12.2 SolutionUse the XMLRPC::Lite module from the SOAP-Lite distribution:
use XMLRPC::Lite;
$server = XMLRPC::Lite->proxy("http://server.example.com/path");
$result = $server->call('ClassName.handler', @ARGS);
die $call->faultstring if $call->fault;
print $call->result;
18.12.3 Discussion
A single XML-RPC server may run many services, differentiated by their method
The proxy is the actual URL of the server. If you're using a CGI server, the proxy method looks something like this:
$server->proxy("http://server.example.com/path/to/server.cgi")
There are three ways to invoke remote
$returned = $server
-> call("getRecordByNumber", 12, { format => "CSV" })
-> result;
The second way to invoke a remote method is to call that method on the XMLRPC::Lite object. This works only when the remote method name isn't the same as a method provided by the XMLRPC::Lite object. For example:
$returned = $server
-> getRecordByNumber(12, { format => "CSV" })
-> result;
The last way to invoke a remote method is with
autodispatch
, turning unrequired function calls and method invocations in your Perl program into XML-RPC
use XMLRPC::Lite +autodispatch =>
proxy => "http://server.example.com/path";
$returned = getRecordByNumber(12, { format => "CSV" });
A critical difference between autodispatch and the other styles is that autodispatch automatically decodes the result into a Perl value for you. When you use an XMLRPC::Lite object, you must explicitly invoke the result method to decode the XML-RPC response into a Perl value. 18.12.4 See AlsoRecipe 18.11; Recipe 18.14 |
Recipe 18.13 Writing a SOAP Server18.13.1 ProblemYou want to write a web service where SOAP is the transport. 18.13.2 SolutionUse the SOAP-Lite distribution from CPAN. Your server can be either standalone:
use SOAP::Transport::HTTP;
$daemon = SOAP::Transport::HTTP::Daemon
->new(LocalPort => $PORT)
->dispatch_to('ClassName')
->handle( );
or a CGI script:
use SOAP::Transport::HTTP;
$daemon = SOAP::Transport::HTTP::CGI
->dispatch_to('ClassName')
->handle( );
In both cases, the only
package ClassName;
sub handler {
my ($class, $arg_hash_ref) = @_;
# ...
}
18.13.3 DiscussionThe SOAP-Lite toolkit contains SOAP and XML-RPC modules. Writing a SOAP service is similar to writing an XML-RPC service. Control method dispatch in SOAP as in XML-RPC. See Recipe 18.11 for details. 18.13.4 See AlsoRecipe 18.14; Recipe 18.11 |
Recipe 18.14 Writing a SOAP Client18.14.1 ProblemYou want to write a client for a SOAP web service. 18.14.2 SolutionUse the SOAP::Lite module from the SOAP-Lite distribution:
use SOAP::Lite;
$server = SOAP::Lite
-> uri("http://localhost/Namespace")
-> proxy("http://server.example.com/path");
$result = $server->call('ClassName.handler', @ARGS);
die $call->faultstring if $call->fault;
print $call->result;
18.14.3 Discussion
A single SOAP server may offer remote access to the methods of many classes. A client identifies the class upon which it wishes to invoke
http://modacrylic.clue.com/GimpyMod http://weenies.mit.edu/GimpyMod As with XML-RPC, the proxy argument is the server's URL. For example, if your SOAP server is implemented as a CGI script, the proxy call looks like this:
$server->proxy("http://server.example.com/path/to/server.cgi");
Invoke remote methods as you do with XML-RPC, either with the call method:
$returned = $server
-> call("getRecordByNumber", 12, { format => "CSV" })
-> result;
or by invoking the method on a SOAP::Lite object directly:
$returned = $server
-> getRecordByNumber(12, { format => "CSV" })
-> result;
or using autodispatch :
use SOAP::Lite +autodispatch =>
uri => "http://identifier.example.com/Namespace",
proxy => "http://server.example.com/path";
$returned = getRecordByNumber(12, { format => "CSV" });
You can also use this with OO syntax:
$returned = Some::Remote::Module->getRecordByNumber(12, { format => "CSV" });
18.14.4 See Also
There's a
lot
more to SOAP than we can explain here. The books
Programming Web Services with SOAP
, by James Snell, Pavel Kulchenko, and Doug Tidwell (O'Reilly), and
Programming Web Services with Perl
, by Randy Ray and Pavel Kulchenko (O'Reilly), form a comprehensive guide to the standards and
|