SOAP::Lite - Perl's Web Services Toolkit
| Use Case | Command | Description |
|---|---|---|
| đĻ Create a SOAP client | my $soap = SOAP::Lite->new( proxy => $url ); | đ§ Instantiate a new client with a proxy endpoint |
| đ Set namespace | $soap->default_ns('urn:HelloWorld'); | đ Set default namespace for request elements |
| đ Call a remote method | my $som = $soap->call('sayHello', 'Kutter', 'Martin'); | đ˛ Invoke a method with positional parameters |
| đ Use WSDL service | my $soap = SOAP::Lite->service("file:service.wsdl"); | đ Load WSDL and generate stubs |
| đ Send attachments | $soap->parts([ $mime_entity ]); | đ Attach MIME entities to request |
| đ Enable debug output | use SOAP::Lite +trace; | đ Turn on tracing for debugging |
| âī¸ Set custom SOAPAction | $soap->on_action( sub { join '/', @_ } ); | đ¯ Override default SOAPAction header |
| đĻ Create a server | SOAP::Transport::HTTP::CGI->dispatch_to('MyModule')->handle; | đĨī¸ Deploy a CGI-based SOAP server |
SOAP::Lite is a collection of Perl modules which provides a simple and lightweight interface to the Simple Object Access Protocol (SOAP) both on client and server side.
As of version 1.05, no perl versions before 5.8 will be supported. SOAP::Lite 0.71 will be the last version running on perl 5.005. Future versions will require at least perl 5.6.0. If you have not had the time to upgrade your perl, you should consider this now.
All accessor methods return the current value when called with no arguments, while returning the object reference itself when called with a new value (chaining).
$client = SOAP::Lite->new(proxy => $endpoint)
đ¨ Constructor. Many accessor methods may be initialized at creation by providing their name as a key, followed by the desired value.
$transp = $client->transport( );
đ Gets or sets the transport object for sending/receiving SOAP messages. See SOAP::Transport.
$serial = $client->serializer( )
đ Gets or sets the serializer object for creating XML messages. See SOAP::Serializer.
$packager = $client->packager( )
đĻ Provides access to the SOAP::Packager object for managing attachments. Default packager is MIME. See SOAP::Packager.
$client->proxy('http://soap.xml.info/ endPoint');
đ Sets the server endpoint. Alias to transport->proxy(...). Extra parameters can be passed:
cookie_jar, timeout.Example with timeout:
my $soap = SOAP::Lite
->uri($uri)
->proxy($proxyUrl, timeout => 5 );
$client->endpoint('http://soap.xml.info/ newPoint')
đ Change the endpoint without reloading transport code. Must have called proxy() first.
$client->service('http://svc.perl.org/Svc.wsdl');
đ Loads a WSDL schema and generates method stubs. Currently only WSDL support is in place.
$client->outputxml('true');
đ When set to true, returns raw XML instead of a SOAP::SOM object.
$client->autotype(0);
đĸ Shortcut for serializer->autotype(boolean). Disables automatic type deduction.
$client->readable(1);
đ Shortcut for serializer->readable(boolean). Adds whitespace for human-readable XML.
$obj->headerattr({ attr1 => 'value' });
đ§Š Sets arbitrary attributes on the SOAP header element. Attributes must be namespace-qualified if not native.
$obj->bodyattr({ attr1 => 'value' });
đ§Š Sets arbitrary attributes on the SOAP body element. See headerattr.
đ Sets the default namespace for the request. Elements are serialized without a prefix:
<soap:Envelope>
<soap:Body>
<myMethod xmlns="http://www.someuri.com">
<foo />
</myMethod>
</soap:Body>
</soap:Envelope>
Some .NET web services require this idiom.
đ Sets namespace URI and optional prefix. If prefix omitted, one is generated. Elements serialized with prefix:
<soap:Envelope>
<soap:Body>
<my:myMethod xmlns:my="http://www.someuri.com">
<my:foo />
</my:myMethod>
</soap:Body>
</soap:Envelope>
Shortcut for serializer->use_prefix(). When false, elements are serialized without prefix (useful for .NET interop).
$client->soapversion('1.2');
đ Gets or sets SOAP version (1.1 or 1.2).
$client->envprefix('env');
đˇī¸ Shortcut for serializer->envprefix(QName). Gets or sets namespace prefix for SOAP envelope (default: SOAP).
$client->encprefix('enc');
đˇī¸ Shortcut for serializer->encprefix(QName). Gets or sets namespace prefix for encoding (default: SOAP-ENC).
$client->encoding($soap_12_encoding_URN);
đĸ Shortcut for serializer->encoding(args). Sets the URN for encoding scheme.
$client->typelookup;
đ Shortcut for serializer->typelookup. Provides access to the type-lookup table.
$client->uri($service_uri);
â ī¸ Deprecated. Use ns() or default_ns(). Sets the service specifier/namespace for the request.
$client->multirefinplace(1);
đ Shortcut for serializer->multirefinplace(boolean). Controls where multi-referenced data is serialized (inline vs. separate).
đ Specifies an array of MIME::Entity's to attach to the transmitted SOAP message. Access returned attachments via SOAP::SOM::parts().
$ref = SOAP::Lite->self;
đ Returns reference to the default global object that processes arguments on the use line.
$client->call($method => @arguments);
đ Invokes a remote method with full control over details. Useful for methods with special characters or namespace control.
$client->on_action(sub { qq("$_[0]") });
đ¯ Triggered when setting SOAPAction header. Callback receives URI and method. .NET expects uri/method:
$client->on_action( sub { join '/', @_ } );
$client->on_fault(sub { popup_dialog($_[1]) });
â ī¸ Triggered when a fault response is received. Callback receives client object and fault object.
$client->on_nonserialized(sub { die "$_[0]?!?" });
â ī¸ Triggered when serializer encounters data it cannot serialize. Return value is used as fallback.
$client->on_debug(sub { print @_ });
đ Deprecated. Use global +trace facilities in SOAP::Trace.
This chapter guides you through writing a SOAP client by example, using a "Hello World" service that accepts name and givenName and returns "Hello $given_name $name".
Client using positional parameters:
use SOAP::Lite;
my $soap = SOAP::Lite->new( proxy => 'http://localhost:81/soap-wsdl-test/helloworld.pl');
$soap->default_ns('urn:HelloWorld');
my $som = $soap->call('sayHello', 'Kutter', 'Martin');
die $som->faultstring if ($som->fault);
print $som->result, "\n";
With WSDL and named parameters:
use SOAP::Lite;
my $soap = SOAP::Lite->service("file:say_hello_rpcenc.wsdl");
eval { my $result = $soap->sayHello('Kutter', 'Martin'); };
if ($@) { die $@; }
print $som->result();
One-liner:
perl -MSOAP::Lite -e 'print SOAP::Lite->service("file:say_hello_rpcenc.wsdl")->sayHello('Kutter', 'Martin'), "\n";'
Without service description, using SOAP::Data:
use SOAP::Lite;
my $soap = SOAP::Lite->new( proxy => 'http://localhost:81/soap-wsdl-test/helloworld.pl');
$soap->default_ns('urn:HelloWorld');
my $som = $soap->call('sayHello',
SOAP::Data->name('name')->value('Kutter'),
SOAP::Data->name('givenName')->value('Martin')
);
die $som->faultstring if ($som->fault);
print $som->result, "\n";
Client using SOAP::Data with a wrapper:
use SOAP::Lite +trace;
my $soap = SOAP::Lite->new( proxy => 'http://localhost:80/helloworld.pl');
$soap->on_action( sub { "urn:HelloWorld#sayHello" });
$soap->autotype(0)->readable(1);
$soap->default_ns('urn:HelloWorld');
my $som = $soap->call('sayHello', SOAP::Data->name('parameters')->value(
\SOAP::Data->value([
SOAP::Data->name('name')->value( 'Kutter' ),
SOAP::Data->name('givenName')->value('Martin'),
]))
);
die $som->fault->{ faultstring } if ($som->fault);
print $som->result, "\n";
use SOAP::Lite;
my $soap = SOAP::Lite->new( proxy => 'http://localhost:80/helloworld.pl');
$soap->on_action( sub { "urn:HelloWorld#sayHello" });
$soap->autotype(0);
$soap->default_ns('urn:HelloWorld');
my $som = $soap->call("sayHello",
SOAP::Data->name('name')->value( 'Kutter' ),
SOAP::Data->name('givenName')->value('Martin'),
);
die $som->fault->{ faultstring } if ($som->fault);
print $som->result, "\n";
From SOAP::Lite's point of view, the only difference between rpc/literal and document/literal is that parameters are always named. In rpc/encoded, the example already used named parameters via WSDL messages.
Note the idiom for passing a list of named parameters in rpc/literal:
my $som = $soap->call('sayHello', SOAP::Data->name('parameters')->value(
\SOAP::Data->value([
SOAP::Data->name('name')->value( 'Kutter' ),
SOAP::Data->name('givenName')->value('Martin'),
]))
);
While SOAP::Data provides full control, passing hash-like structures requires additional coding.
See SOAP::Server and SOAP::Transport for details.
Supports SOAP with Attachments specification (MIME only, DIME not fully functional).
use SOAP::Lite;
use MIME::Entity;
my $ent = build MIME::Entity
Type => "image/gif",
Encoding => "base64",
Path => "somefile.gif",
Filename => "saveme.gif",
Disposition => "attachment";
my $som = SOAP::Lite
->uri($SOME_NAMESPACE)
->parts([ $ent ])
->proxy($SOME_HOST)
->some_method(SOAP::Data->name("foo" => "bar"));
use SOAP::Lite;
use MIME::Entity;
my $soap = SOAP::Lite
->uri($NS)
->proxy($HOST);
my $som = $soap->foo();
foreach my $part (${$som->parts}) {
print $part->stringify;
}
package Attachment;
use SOAP::Lite;
use MIME::Entity;
use strict;
use vars qw(@ISA);
@ISA = qw(SOAP::Server::Parameters);
sub someMethod {
my $self = shift;
my $envelope = pop;
foreach my $part (@{$envelope->parts}) {
print "AttachmentService: attachment found! (".ref($part).")\n";
}
# do something
}
package Attachment;
use SOAP::Lite;
use MIME::Entity;
use strict;
use vars qw(@ISA);
@ISA = qw(SOAP::Server::Parameters);
sub someMethod {
my $self = shift;
my $envelope = pop;
my $ent = build MIME::Entity
'Id' => "<1234>",
'Type' => "text/xml",
'Path' => "some.xml",
'Filename' => "some.xml",
'Disposition' => "attachment";
return SOAP::Data->name("foo" => "blah blah blah"),$ent;
}
You can specify default settings for all SOAP::Lite objects using use SOAP::Lite ...:
use SOAP::Lite
proxy => 'http://localhost/cgi-bin/soap.cgi',
uri => 'http://my.own.com/My/Examples';
my $soap1 = new SOAP::Lite; # inherits proxy/uri
my $soap2 = SOAP::Lite->new; # same
my $soap3 = SOAP::Lite->proxy('http://localhost/'); # overrides
You can also set event handlers globally:
use SOAP::Lite
on_action => sub {sprintf '%s#%s', @_};
To change global settings at runtime: SOAP::Lite->self->proxy(...).
â ī¸ Note: use is executed at compile time. Use eval for runtime.
use SOAP::Transport::HTTP;
use MIME::Entity;
$SOAP::Constants::MAX_CONTENT_SIZE = 10000;
SOAP::Transport::HTTP::CGI
->dispatch_to('TemperatureService')
->handle;
Parameters are accessible via result() and paramsout(). Autobinding maps output parameters with same signature back to input.
Example: If server returns return (1,2,3), result is 1, out parameters are 2 and 3. If server returns return [1,2,3], result is an array reference, paramsout is undef.
Autobinding example:
# Server code
sub mymethod {
shift; my $param1 = shift;
my $param2 = SOAP::Data->name('myparam' => shift() * 2);
return $param1, $param2;
}
# Client code
$a = 10;
$b = SOAP::Data->name('myparam' => 12);
$result = $soap->mymethod($a, $b);
# After: $result == 10, $b->value == 24
See the PingPong example for object autobinding.
Static deployment: Preload modules and use dispatch_to('MODULE').
use SOAP::Transport::HTTP;
use My::Examples;
SOAP::Transport::HTTP::CGI
-> dispatch_to('My::Examples')
-> handle;
Dynamic deployment: Modules loaded on demand from specified paths.
use SOAP::Transport::HTTP;
SOAP::Transport::HTTP::CGI
-> dispatch_to('/Your/Path/To/Deployed/Modules', 'My::Examples')
-> handle;
dispatch_with (experimental): Bind URL or SOAPAction to a module/object.
dispatch_with({
URI => MODULE,
SOAPAction => MODULE,
URI => object,
})
URI is checked before SOAPAction. dispatch_with has higher precedence than dispatch_to.
Transparent compression over HTTP. Set compress_threshold in kilobytes.
Client:
print SOAP::Lite
->uri('http://localhost/My/Parameters')
->proxy('http://localhost/', options => {compress_threshold => 10000})
->echo(1 x 10000)
->result;
Server:
my $server = SOAP::Transport::HTTP::CGI
->dispatch_to('My::Parameters')
->options({compress_threshold => 10000})
->handle;
With dynamic deployment, @INC is disabled for security. Options to access other modules:
use MODULE; $server->dispatch_to('MODULE');use to require (path available during execution).use in eval.BEGIN { @INC = qw(my_directory); use MODULE }.Use fully qualified names for return values:
return SOAP::Data->name('myname')
->type('string')
->uri($MY_NAMESPACE)
->value($output);
on_action( sub { 'http://www.myuri.com/WebService.aspx#someMethod'; } )$SOAP::Constants::DO_NOT_USE_CHARSET = 1;SOAP::Data->name(Query => 'biztalk')->uri('http://tempuri.org/')SOAP::Data->name('add')->attr({xmlns => 'http://tempuri.org/'})$soap->use_prefix(0)[SoapRpcService] attribute.Special thanks to Petr Janata, Stefan Pharies, Brian Jepson, and others for .NET interop details.
SOAP::Data->type('string') or modify typelookup.dispatch_from instead.Lite.dll with regsvr32; ensure correct Perl path.PassEnv LD_LIBRARY_PATH to httpd.conf.--disable-rule=EXPAT or use system malloc..pl extension.ParserAdapter; attribute order not significant per XML spec.Processing XML-encoded fragments can be slow due to expat's character callback. For large XML strings, consider encoding as base64:
SOAP::Data->type(base64 => $string)
To globally change string encoding to base64:
*SOAP::Serializer::as_string = \&SOAP::XMLSchema2001::Serializer::as_base64Binary;
Information and compiled XML::Parser for MacPerl available at:
Download from CPAN: http://search.cpan.org/search?dist=SOAP-Lite
Special thanks to Randy J. Ray, O'Reilly publishing, and all developers who contributed patches, ideas, and help.
Latest development on GitHub: git@github.com:redhotpenguin/perl-soaplite.git. See HACKING file. Actively recruiting maintainers.
Please use rt.cpan.org or GitHub. Pull requests preferred.
Copyright (C) 2000-2007 Paul Kulchenko. All rights reserved.
Copyright (C) 2007-2008 Martin Kutter
Copyright (C) 2013 Fred Moyer
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. Documentation available under Creative Commons Attribution-NoDerivs 2.0 license: http://creativecommons.org/licenses/by-nd/2.0/
Generated by phpman v4.9.26-1-g511901d · Markdown · JSON · MCP Author: Che Dong Under GNU General Public License
2026-08-08 03:44 @2600:1f28:365:80b0:6814:a048:8015:f4ec
CrawledBy CCBot/2.0 (https://commoncrawl.org/faq/)
Enhanced by LLM: deepseek-v4-flash / taotoken.net / www.chedong.com - original format