How to use the Google API inMovable Type
Created by Josh Cooper

This is my third Hack for Movable Type.

I found out today when I got home from work that Google had implemented an API. I then found this
perl example
of it being used, at Scripting.com. That is where I got most of the code and changed it to
make it work with MovableType.

There are 2 Files that will need to be edited for this. As always backup your files first.

/lib/MT/Util.pm
/lib/MT/Template/Context.pm

You will also need to sign up at Google for and account and download the API development Kit here.

Once you have done that, upload GoogleSearch.wsdl to the lib/MT/ dir.

Open Util.pm in your text editor of choice and find the lines:

is_valid_email encode_php);

Now change it to this:

is_valid_email encode_php google_search);

Now add the following Sub Routine some where before the end.

sub google_search {
use SOAP::Lite;
# Configuration
my $key = 'XXXXXXXX'; # <-- PUT YOUR KEY HERE
my $query = 'google API'; # Type in your search term here
my $max = 10; # Max number of results
# 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://home/fullpathhere/mt/lib/MT/GoogleSearch.wsdl');
my $result = $service
-> doGoogleSearch(
$key, # key
$query, # search query
0, # start results
$max, # max results
SOAP::Data->type(boolean => 'false'), # filter: boolean
"", # restrict (string)
SOAP::Data->type(boolean => '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.
my $list = "";
if(defined($result->{resultElements})) {
my $i = 0;
while ($i <= $max -1) {
$list = "$list<a href=\"$result->{resultElements}->[$i]->{URL}\">$result->{resultElements}->[$i]->{title}<\/a><br>";
$i++;
}
}
$list;
}

Now Save and Close that file.

Now open Context.pm

Now find this line:

spam_protect encode_php);

And edit it to look like this::

spam_protect encode_php google_search);

Now in the init_default_handlers subroutine Add the following before the bottom

$ctx->register_handler(GoogleSearch => \&_hdlr_google_search);

It should now look like this:

$ctx->register_handler(CategoryCount => \&_hdlr_category_count);


$ctx->register_handler(GoogleSearch => \&_hdlr_google_search);

Next Add the subroutine before the bottom :

sub _hdlr_google_search {
my $gs = google_search;
}

Now save and exit this file.

To use this in your Template simple use this tag: <$MTGoogleSearch$>

The next time you rebuild your template you should see something simular to this, depending on which
search term you used.