The Code
Type the following code into your preferred plain-text
editor—be it Notepad, TextEdit, or a command-line editor like
vi or Emacs—and save it to a file named
googly.pl. Remember to replace
insert key here with your Google API key,
as explained in "Using Your Google API
Key" earlier in this chapter.
TIP
In addition to the Google API Developer's Kit,
you'll need the SOAP::Lite Perl module installed
before running this
hack.
#!/usr/local/bin/perl
# googly.pl
# A typical Google Web API Perl script.
# Usage: perl googly.pl <query>
# Your Google API developer's key.
my $google_key='insert key here';
# Location of the GoogleSearch WSDL file.
my $google_wdsl = "./GoogleSearch.wsdl";
use strict;
# Use the SOAP::Lite Perl module.
use SOAP::Lite;
# Take the query from the command line.
my $query = shift @ARGV or die "Usage: perl googly.pl <query>\n";
# Create a new SOAP::Lite instance, feeding it GoogleSearch.wsdl.
my $google_search = SOAP::Lite->service("file:$google_wdsl");
# Query Google.
my $results = $google_search ->
doGoogleSearch(
$google_key, $query, 0, 10, "false", "", "false",
"", "latin1", "latin1"
);
# No results?
@{$results->{resultElements}} or exit;
# Loop through the results.
foreach my $result (@{$results->{resultElements}}) {
# Print out the main bits of each result
print
join "\n",
$result->Program Google in Perl || "no title",
$result->{URL},
$result->{snippet} || 'no snippet',
"\n";
}
According to google's pages it's easy to do .. and some HTTP proxies break SOAP when passing it. Any ideas ?
Thanks.