H
Hudson
#!/usr/bin/perl -w
use strict;
Hi all, today I went looking to do something with the google api's.
But...all the examples I found used soap::light and that is not
in my @INC on my server.
So I banged out this example...doesn't seem so hard. Is this all there
is too it, any advice/critiques, etc or feed back?
must name nph-somefile.cgi -> nonparsed headers
#!/usr/bin/perl -w
use CGI;
use CGI::Carp qw(fatalsToBrowser);
use IO::Socket;
my $sleep_time = 1;
my $query = new CGI;
print "HTTP/1.0 200 OK\n";
print "Content-Type: text/html\r\n\r\n";
print "<html><head><body>";
print "<hr noshade width=\"65%\">";
print "<center><h2>Google's 10 ten URL's via web services (SOAP)<\/h2>";
print "Enter one keyword or phrase per line (limit of 1,000 searches a day)";
&print_prompt($query);
&do_work($query);
print $query->end_html;
# --------------------------------------------------------- subroutines: cgi.pm
sub print_prompt {
my($query) = @_;
print $query->startform;
print $query->textarea(-name=>'domains',
-rows=>8,
-columns=>30);
print "<br><br>";
print $query->submit(-name=>'find those dirty dogs');
print $query->endform;
}
sub do_work {
unless ($query->param) {
return;
}
my $query_string = $query->param('domains');
my @domains = split(/\n/, $query_string);
metatags (@domains);
print "</body></html>";
}
# -------------------------------------------------------- subroutine: get data
sub metatags {
$|=1;
my @keywords = @_;
foreach my $keyword (@keywords) {
$keyword =~ s/\s+$//;
my $soap_request .= "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/1999/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/1999/XMLSchema\">\n";
$soap_request .= "<SOAP-ENV:Body>\n";
$soap_request .= "<ns1:doGoogleSearch xmlns:ns1=\"urn:GoogleSearch\" \n";
$soap_request .= "SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">\n";
$soap_request .= "<key xsi:type=\"xsd:string\">***_your_key_here_***</key>\n";
$soap_request .= "<q xsi:type=\"xsd:string\">$keyword</q>\n";
$soap_request .= "<start xsi:type=\"xsd:int\">0</start>\n";
$soap_request .= "<maxResults xsi:type=\"xsd:int\">10</maxResults>\n";
$soap_request .= "<filter xsi:type=\"xsd:boolean\">true</filter>\n";
$soap_request .= "<restrict xsi:type=\"xsd:string\"></restrict>\n";
$soap_request .= "<safeSearch xsi:type=\"xsd:boolean\">false</safeSearch>\n";
$soap_request .= "<lr xsi:type=\"xsd:string\"></lr>\n";
$soap_request .= "<ie xsi:type=\"xsd:string\">latin1</ie>\n";
$soap_request .= "<oe xsi:type=\"xsd:string\">latin1</oe>\n";
$soap_request .= "</ns1:doGoogleSearch>\n";
$soap_request .= "</SOAP-ENV:Body>\n";
$soap_request .= "</SOAP-ENV:Envelope>";
my $request = "POST /search/beta2 HTTP/1.1\n";
$request .= "Host: api.google.com\n";
$request .= "Accept-Encoding: identity\n";
$request .= "Content-length: ".length($soap_request)."\n";
$request .= "SOAPAction: \"urn:GoogleSearchAction\"\n";
$request .= "Content-Type: text/xml\; charset=utf-8\n\n";
$request .= "$soap_request";
my $socket = IO::Socket::INET->new(PeerAddr => "api.google.com",
PeerPort => 80,
Proto => "tcp",
Type => SOCK_STREAM,
Timeout => 5);
print $socket $request;
print "<b>$keyword:</b><br>";
while (<$socket>) {
if ($_ =~ /<URL xsi:type=\"xsd:string\">(.*)<\/URL>/) {
print "<a href=\"$1\">$1</a><br>";
}
}
print "<hr>";
close($socket);
sleep $sleep_time;
}
}
use strict;
Hi all, today I went looking to do something with the google api's.
But...all the examples I found used soap::light and that is not
in my @INC on my server.
So I banged out this example...doesn't seem so hard. Is this all there
is too it, any advice/critiques, etc or feed back?
must name nph-somefile.cgi -> nonparsed headers
#!/usr/bin/perl -w
use CGI;
use CGI::Carp qw(fatalsToBrowser);
use IO::Socket;
my $sleep_time = 1;
my $query = new CGI;
print "HTTP/1.0 200 OK\n";
print "Content-Type: text/html\r\n\r\n";
print "<html><head><body>";
print "<hr noshade width=\"65%\">";
print "<center><h2>Google's 10 ten URL's via web services (SOAP)<\/h2>";
print "Enter one keyword or phrase per line (limit of 1,000 searches a day)";
&print_prompt($query);
&do_work($query);
print $query->end_html;
# --------------------------------------------------------- subroutines: cgi.pm
sub print_prompt {
my($query) = @_;
print $query->startform;
print $query->textarea(-name=>'domains',
-rows=>8,
-columns=>30);
print "<br><br>";
print $query->submit(-name=>'find those dirty dogs');
print $query->endform;
}
sub do_work {
unless ($query->param) {
return;
}
my $query_string = $query->param('domains');
my @domains = split(/\n/, $query_string);
metatags (@domains);
print "</body></html>";
}
# -------------------------------------------------------- subroutine: get data
sub metatags {
$|=1;
my @keywords = @_;
foreach my $keyword (@keywords) {
$keyword =~ s/\s+$//;
my $soap_request .= "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/1999/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/1999/XMLSchema\">\n";
$soap_request .= "<SOAP-ENV:Body>\n";
$soap_request .= "<ns1:doGoogleSearch xmlns:ns1=\"urn:GoogleSearch\" \n";
$soap_request .= "SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">\n";
$soap_request .= "<key xsi:type=\"xsd:string\">***_your_key_here_***</key>\n";
$soap_request .= "<q xsi:type=\"xsd:string\">$keyword</q>\n";
$soap_request .= "<start xsi:type=\"xsd:int\">0</start>\n";
$soap_request .= "<maxResults xsi:type=\"xsd:int\">10</maxResults>\n";
$soap_request .= "<filter xsi:type=\"xsd:boolean\">true</filter>\n";
$soap_request .= "<restrict xsi:type=\"xsd:string\"></restrict>\n";
$soap_request .= "<safeSearch xsi:type=\"xsd:boolean\">false</safeSearch>\n";
$soap_request .= "<lr xsi:type=\"xsd:string\"></lr>\n";
$soap_request .= "<ie xsi:type=\"xsd:string\">latin1</ie>\n";
$soap_request .= "<oe xsi:type=\"xsd:string\">latin1</oe>\n";
$soap_request .= "</ns1:doGoogleSearch>\n";
$soap_request .= "</SOAP-ENV:Body>\n";
$soap_request .= "</SOAP-ENV:Envelope>";
my $request = "POST /search/beta2 HTTP/1.1\n";
$request .= "Host: api.google.com\n";
$request .= "Accept-Encoding: identity\n";
$request .= "Content-length: ".length($soap_request)."\n";
$request .= "SOAPAction: \"urn:GoogleSearchAction\"\n";
$request .= "Content-Type: text/xml\; charset=utf-8\n\n";
$request .= "$soap_request";
my $socket = IO::Socket::INET->new(PeerAddr => "api.google.com",
PeerPort => 80,
Proto => "tcp",
Type => SOCK_STREAM,
Timeout => 5);
print $socket $request;
print "<b>$keyword:</b><br>";
while (<$socket>) {
if ($_ =~ /<URL xsi:type=\"xsd:string\">(.*)<\/URL>/) {
print "<a href=\"$1\">$1</a><br>";
}
}
print "<hr>";
close($socket);
sleep $sleep_time;
}
}