All set? Now we're going to put together a real quick SOAP server that
will generate a (pseudo)random number between 1 and 10. All together
now, in Perl:
#!/usr/bin/perl -w
use SOAP::Transport::HTTP;
SOAP::Transport::HTTP::CGI
->dispatch_to('Random')
->handle;
package Random;
sub choose {
$random = int(rand(9))+1;
return $random;
}
The second half of that script generates and returns a random number
the first half, with dispatch_to and handle, wraps the
procedure in a SOAP bubble. In essence, it's telling the HTTP server
that any calls that come in for Random should be sent to SOAP::Lite, via
this script.
Save this script in your Web server's cgi-bin directory, set its
permissions so it's executable, and it's ready to go. Now let's write a
corresponding client script to use it.
#!/usr/bin/perl -w
use SOAP::Lite;
print SOAP::Lite
-> uri('http://myserver.com/Random')
-> proxy('http://myserver.com/cgi-bin/random.cgi')
-> Random()
-> result;
Quick, update your resume. You've built a complete Web service using
SOAP. Run that script, and voila! A random number, requested and
delivered via SOAP, just for you. Let's take a quick look at how it does
what it does.
next page»