++ed by:

1 PAUSE user(s)

Randy J Ray


RPC::XML::Client - An XML-RPC client class


    require RPC::XML;
    require RPC::XML::Client;

    $cli = RPC::XML::Client->new('http://www.localhost.net/RPCSERV');
    $resp = $cli->send_request('system.listMethods');

    print (ref $resp) ? join(', ', @{$resp->value}) : "Error: $resp";


This is an XML-RPC client built upon the RPC::XML data classes, and using LWP::UserAgent and HTTP::Request for the communication layer. This client supports the full XML-RPC specification.


The following methods are available:

new (URI [, ARGS])

Creates a new client object that will route its requests to the URL provided. The constructor creates a HTTP::Request object and a LWP::UserAgent object, which are stored on the client object. When requests are made, these objects are ready to go, with the headers set appropriately. The return value of this method is a reference to the new object. The URI argument may be a string or an object from the URI class from CPAN.

Any additional arguments are treated as key-value pairs. Most are attached to the object itself without change. The following are recognized by new and treated specially:


If passed, the value must be a code reference that will be invoked when a request results in a transport-level error. The closure will receive a single argument, the text of the error message from the failed communication attempt. It is expected to return a single value (assuming it returns at all).


If passed, the value must be a code reference. This one is invoked when a request results in a fault response from the server. The closure will receive a single argument, a RPC::XML::fault instance that can be used to retrieve the code and text-string of the fault. It is expected to return a single value (if it returns at all).


If this parameter is specified, it too must have a code reference as a value. It is installed as the handler for both faults and errors. Should either of the other parameters be passed in addition to this one, they will take precedence over this (more-specific wins out over less). As a combined handler, the closure will get a string (non-reference) in cases of errors, and an instance of RPC::XML::fault in cases of faults. This allows the developer to install a simple default handler, while later providing a more specific one by means of the methods listed below.

See the section on the effects of callbacks on return values, below.

uri ([URI])

Returns the URI that the invoking object is set to communicate with for requests. If a string or URI class object is passed as an argument, then the URI is set to the new value. In either case, the pre-existing value is returned.


Returns the LWP::UserAgent object instance stored on the client object. It is not possible to assign a new such object, though direct access to it should allow for any header modifications or other needed operations.


Returns the HTTP::Request object. As with the above, it is not allowed to assign a new object, but access to this value should allow for any needed operations.

simple_request (ARGS)

This is a somewhat friendlier wrapper around the next routine (send_request) that returns Perl-level data rather than an object reference. The arguments may be the same as one would pass to the RPC::XML::request constructor, or there may be a single request object as an argument. The return value will be a native Perl value. If the return value is undef, an error has occurred and simple_request has placed the error message in the global variable $RPC::XML::ERROR.

send_request (ARGS)

Sends a request to the server and attempts to parse the returned data. The argument may be an object of the RPC::XML::request class, or it may be the arguments to the constructor for the request class. The return value will be either an error string or a data-type object. If the error encountered was a run-time error within the RPC request itself, then the call will return a RPC::XML::fault value rather than an error string.

If the return value from send_request is not a reference, then it can only mean an error on the client-side (a local problem with the arguments and/or syntax, or a transport problem). All data-type classes now support a method called is_fault that may be easily used to determine if the "successful" return value is actually a RPC::XML::fault without the need to use UNIVERSAL::ISA.


These accessor methods get (and possibly set, if CODEREF is passed) the specified callback/handler. The return value is always the current handler, even when setting a new one (allowing for later restoration, if desired).

Support for Content Compression

The RPC::XML::Server class supports compression of requests and responses via the Compress::Zlib module available from CPAN. Accordingly, this class also supports compression. The methods used for communicating compression support should be compatible with the server and client classes from the XMLRPC::Lite class that is a part of the SOAP::Lite package (also available from CPAN).

Compression support is enabled (or not) behind the scenes; if the Perl installation has Compress::Zlib, then RPC::XML::Client can deal with compressed responses. However, since outgoing messages are sent before a client generally has the chance to see if a server supports compression, these are not compressed by default.


If a client is communicating with a server that is known to support compressed messages, this method can be used to tell the client object to compress any outgoing messages that are longer than the threshhold setting in bytes.


With no arguments, returns the current compression threshhold; messages smaller than this number of bytes will not be compressed, regardless of the above method setting. If a number is passed, this is set to the new lower-limit. The default value is 4096.

Callbacks and Return Values

If a callback is installed for errors or faults, it will be called before either of send_request or simple_request return. If the callback calls die or otherwise interrupts execution, then there is no need to worry about the effect on return values. Otherwise, the return value of the callback becomes the return value of the original method (send_request or simple_request). Thus, all callbacks are expected, if they return at all, to return exactly one value. It is recommended that any callback return values conform to the expected return values. That is, an error callback would return a string, a fault callback would return the fault object.


All methods return some type of reference on success, or an error string on failure. Non-reference return values should always be interpreted as errors, except in the case of simple_request.


This began as a reference implementation in which clarity of process and readability of the code took precedence over general efficiency. It is now being maintained as production code, but may still have parts that could be written more efficiently.


The XML-RPC standard is Copyright (c) 1998-2001, UserLand Software, Inc. See <http://www.xmlrpc.com> for more information about the XML-RPC specification.


This module is licensed under the terms of the Artistic License that covers Perl. See <http://language.perl.com/misc/Artistic.html> for the license itself.


RPC::XML, RPC::XML::Server


Randy J. Ray <rjray@blackperl.com>