This section provides a tutorial example on how to use the SOAP::Data class to build the request parameter element with correct element name and a namespace. My first SOAP::Lite program works now with the GetSpeech Web service.
After going through various testing programs,
I finalized my GetSpeech program for SOAP 1.1 as, GetSpeech_SOAP_1_1.pl:
#- GetSpeech_SOAP_1_1.pl
#- Copyright (c) 2009 by Dr. Herong Yang, herongyang.com
#- All rights reserved
#
use SOAP::Lite;
my $proxy = 'http://www.xmlme.com/WSShakespeare.asmx';
my $uri = 'http://xmlme.com/WebServices';
my $method = "GetSpeech";
my $soapAction = sub {return "$uri/$method"};
my ($phrase) = @ARGV;
#- Building the parameter element
my $parameter = SOAP::Data
->name("Request") # set the element name
->value($phrase) # set the value
->uri($uri); # set namespace
#- Creating the SOAP client object
my $client = SOAP::Lite->new()
->proxy($proxy) # set the proxy URL
->on_action($soapAction) # set the SOAPAction
->uri($uri); # set the namespace
#- Calling the Web service
my $som = $client->call($method=>($parameter));
my $output = $som->result;
print $output . "\n";
Here is what I did in the final version:
The trace feature has been turned off on the SOAP::Lite module.
Some variables are created to store the proxy URL and other values.
The input phase is coming from the first command line argument.
The call back function for SOAPAction is revised with hard coded string.
When executing GetSpeech_SOAP_1_1.pl, I got this:
\herong>GetSpeech_SOAP_1_1.pl "To be, or not to be"
<SPEECH><PLAY>HAMLET</PLAY><SPEAKER>HAMLET</SPEAKER>
To be, or not to be: that is the question: Whether 'tis nobler
in the mind to suffer The slings and arrows of outrageous fortune,
...</SPEECH>
Now you can try it with different phrases from Shakespeare's plays.