Recipe 18.14 Writing a SOAP Client

18.14.1 Problem

You want to write a client for a SOAP web service.

18.14.2 Solution

Use 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 methods with the uri parameter. The hostname in the argument is irrelevant; only the path portion (the class name) matters. For example, these two URIs are equivalent:

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 implementations. Also see Recipe 18.11; Recipe 18.13