#! /usr/bin/perl # # A sample script accessing Insuma SOAP service. # (C) 2003 Insuma GmbH # uncomment the following line and comment out 'use strict' to see raw # XML data passed between the client and the server # use SOAP::Lite +trace => 'debug'; use strict; use SOAP::Lite; # Create a proxy object for accessing the Insuma service. my $server = SOAP::Lite->service('http://www.insuma.de/wsdl/demo.wsdl'); # Make it pretty-format the XML it sends so the debug output (if enabled) # is human-readable $server->readable(1); # This function does the main job of talking SOAP sub query_soap { my $query = shift; # SOAP::Lite does not escape strings in an XML-safe manner. We have # to do it by hand here. $query =~ s/&/&/g; $query =~ s//>/g; my $result = $server->search_xml_str($query); # result is hash return $result; } sub main { my @argv = @_; # the default query my $query = < EOF if (scalar @argv > 1) { print "Usage: soap_example.pl [file_with_XML_query]\n"; return 2; } elsif (scalar @argv == 0) { print <) or declarations # (like ). # The content of the file will be read and send over the SOAP # interface as is. open(IN, $argv[0]) or die "Could not open $argv[0].\n"; $query = ""; # read the file into the variable $query line by line $query .= $_ while (); } print $query, "\n"; my $result = query_soap($query); # the returned result is a hash reference. The hash contains the # following keys: # * 'hits' -- the number of returned results, integer # * 'total_hits' -- the total number of hits found, integer # * 'total_hits_exact' -- 0 or 1. 0 means that total_hits is an # estimate and the actual number of hits # may be different # * 'documents' -- an array reference. The array contains # records describing all returned hits. print "Found documents: ", $result->{hits}, "\n\n"; # loop over the found documents for (my $i = 0; $i < $result->{hits}; $i++) { my $doc = $result->{documents}[$i]; # each document is a hash reference. The keys of the hash are: # * 'url' -- the URL of the found document # * 'properties' -- a list of name-value pairs describing the # document, an array reference. print $i+1, '. ', $doc->{url}, "\n"; # loop over the properties my @properties = @{$doc->{properties}}; foreach my $property(@properties) { # each element of the properties list is a hash reference. # The hash has two self-explanatory keys, 'name' and 'value'. # Note that some attributes can be multivalue. In this case # there will be several name-value pairs in the properties # list with the same name. E.g., a paper written by three # authors may have 3 entries in the properties list. Each # entry will have the same $property->{name}, "author". # The same behaviour can happen in simpler cases, e.g., some # HTML pages may have more than one element. print $property->{name}, ": ", $property->{value}, "\n"; } print "\n"; } return 0; } main(@ARGV); # vi: set sw=4: