« 听李开复说话:想成为企业家需要具备哪些知识和素质 | Main | 今天终于决定把rss源换到了这里 »

俺也学习How to use the Google API inMovable Type

咳,不得不承认技术的重要性。没有学习到自己喜欢的计算机知识……算了,先把文章放到这,等以后再来读懂它吧!
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{resultElements}->[$i]->{URL}\">$result->{resultElements}->[$i]->{title}<\/a>
";
$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.


相关文章:

我看yahoo等美国互联网公司的失败 - Dec 11, 2006

网络牛皮癣开始滋生了 - Dec 09, 2006

GDrive成真,Google已经买了GDrive相关的域名 - Oct 29, 2006