Exemple de web service permettant de controler l'orthographe d'un texte (issu d'un article récent de DDJ) :
#!/usr/local/bin/perl -w
use SOAP::Transport::HTTP;
my $daemon = SOAP::Transport::HTTP::Daemon
-> new (LocalAddr => 'localhost', LocalPort => 81)
-> objects_by_reference(qw(Spelling))
-> dispatch_to('Spelling');
print "Contact to SOAP server at ", $daemon->url, "\n";
$daemon->handle;
package Spelling;
sub check
{
my ($self, $text) = @_;
use Lingua::Ispell;
$Lingua::Ispell::path = "/usr/bin/ispell";
$dummy = '__TERM__';
for $word (Lingua::Ispell::spellcheck ($text))
{
if ($word->{type} eq 'miss')
{
# First replace any terms found inside HTML or Embperl
# brackets with dummy term
$text =~ s{([\<\[])([^\>\]]*?)(\b$word->{term}\b)}{$1$2$dummy}g;
# Now mark up the remaining terms with red font
$text =~ s{(^|[^\>\&])(\b$word->{term}\b)}{$1\<FONT COLOR=\"red\"\>$2\<\/FONT\>}g;
# Restore the dummy terms
$text =~ s{$dummy}{$word->{term}}g;
}
}
return $text;
}
1;
Le client SOAP :
sub check_spelling
{
my ($self, $textref) = @_;
# Check spelling
use SOAP::Lite;
my $soap = SOAP::Lite
-> uri('http://localhost/Spelling')
-> proxy('http://localhost:81')
-> on_fault
(sub {undef $result});
$result = $soap->check($$textref);
$$textref = $result ? $result->result()
: $$textref;
}
Client permettant d'interroger le web service de Google :
#!/usr/bin/perl -w
# GoogleSearch.pl
# 11apr2002 - matt@interconnected.org http://interconnected.org/home/
# Demonstrates use of the doGoogleSearch method on the Google API.
# See http://www.google.com/apis/ to get your key and download the WSDL
#(which this script expects to find in its directory).
use strict;
use SOAP::Lite;
# Configuration
my $key = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; # <-- PUT YOUR KEY HERE
my $query = $ARGV[0] || "google api"; # either type on the command line,
# or it defaults to 'google api'
# Redefine how the default deserializer handles booleans.
# Workaround because the 1999 schema implementation incorrectly doesn't
# accept "true" and "false" for boolean values.
# See http://groups.yahoo.com/group/soaplite/message/895
*SOAP::XMLSchema1999::Deserializer::as_boolean =
*SOAP::XMLSchemaSOAP1_1::Deserializer::as_boolean =
\&SOAP::XMLSchema2001::Deserializer::as_boolean;
# Initialise with local SOAP::Lite file
my $service = SOAP::Lite
-> service('file:./GoogleSearch.wsdl');
my $result = $service
-> doGoogleSearch(
$key, # key
$query, # search query
0, # start results
10, # max results
"false", # filter: boolean
"", # restrict (string)
"false", # safeSearch: boolean
"", # lr
"latin1", # ie
"latin1" # oe
);
# $result is hash of the return structure. Each result is an element in the
# array keyed by 'resultElements'. See the WSDL for more details.
if(defined($result->{resultElements})) {
my @results = @$result->{resultElements};
my $res;
foreach $res (@results) {
print join "\n",
"Found:",
$res->{title},
$res->{URL} . "\n"
}
}
# nb:
# - The two booleans in the search above must be "false" or "true" (not 1 or
# 0). Previously this script used 'SOAP::Data->type(boolean => "false")'
# which came out as '0' in the SOAP message, but wasn't understood by the
# Google interface.
# - I understand that the Schema definition workaround above isn't needed if
# you're using SOAP::Lite 0.52 or above. I've been using 0.51.
Ressources: