Monday, May 21, 2007

A Perl Web Service Client With SOAP

A perl WebServices client with SOAP,call XMethods temperature service.
#!/usr/bin/perl -w
#$file: ws_client.pl - a perl WebServices client with SOAP, call XMethods #temperature service. 
#

use strict;
use SOAP::EnvelopeMaker;
use SOAP::Parser;
use SOAP::Struct;
use SOAP::Transport::HTTP::Client;

my $zipcode = shift; $zipcode =~ /^\d+$/ or die "USAGE: $0 U.Szipcode\n";
my ($server, $port, $endpoint, $soapaction, $method, $method_urn,
        $message, $envelope, $response, $parser);

# service address
$server = 'services.xmethods.net';
$port = 80;
$endpoint = '/soap/servlet/rpcrouter';

$soapaction ='';
$method = 'getTemp';
# operator namespace
$method_urn = 'urn:xmethods-Temperature';

# create SOAP envelope
$envelope = SOAP::EnvelopeMaker->new(\$message);

# set SOAP body,zipcode is the parameter name
$envelope->set_body($method_urn, $method, 0,SOAP::Struct->new(zipcode=>$zipcode));
# create client call
$response = SOAP::Transport::HTTP::Client->new()->send_receive($server, $port, $endpoint,
        $method_urn, $method,$message);

# parser SOAP response message
$parser = SOAP::Parser->new;
$parser->parsestring($response);
# get SOAP body
$response = $parser->get_body;

if (exists $response->{return}) {
        print "$zipcode Temperature: $response->{return}\n";
} else {
        print "A fault ($response->{faultcode}) occurred: " .
                "$response->{faultstring}\n";
}
exit;

0 comments:

 
Blogger Template Layout Design by [ METAMUSE ] : Code Name Gadget 1.1 Power By freecode-frecode.blogger.com & blogger.com Programming Blogs - BlogCatalog Blog Directory