The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

REST::Google - access Google REST (aka AJAX) API from Perl

SYNOPSIS

        use REST::Google;

        # set service to use
        REST::Google->service('http://ajax.googleapis.com/ajax/services/search/web');

        # provide a valid http referer
        REST::Google->http_referer('http://example.com');

        my $res = REST::Google->new(
                q => 'Larry Wall',
        );

        die "response status failure" if $res->responseStatus != 200;

        my $data = $res->responseData;

        use Data::Dumper;
        print Dumper( $data );

DESCRIPTION

REST::Google provides OO interface to Google REST (aka AJAX) API.

Note that this module provides low-level access to Google services API. Consider using REST::Google::Search, REST::Google::Translate or REST::Google::Feeds, which provide convenient access to service request results.

METHODS

__PACKAGE__->service()

Get/set service to use. You must set this to valid URL. E.g.:

        REST::Google->service('http://ajax.googleapis.com/ajax/services/search/web');

Normally, you don't need this, since REST::Google::Search, REST::Google::Translate or REST::Google::Feeds modules do this for you.

__PACKAGE__->http_referer()

Get/set HTTP Referer header.

        REST::Google->http_referer('http://example.org/search.html');

Note: Google says that you should supply a valid HTTP referer header each time you perform a request to their AJAX API, so new() raises warning unless referer is specified.

__PACKAGE__->new()

The constructor use it's arguments to build a valid HTTP GET request to given service, so it takes the same arguments as the given web service takes. Please refer to 'Google AJAX API' documentation for complete list of arguments for a service you're using. E.g.:

        my $res = REST::Google->new(
                q => 'Pamela Anderson',
        );

For example, if you're using the Web search service, the code above will perform a following HTTP GET request:

        http://ajax.googleapis.com/ajax/services/search/web?q=Pamela+Anderson&v=1.0

Note: You can left protocol version number unspecified since v=1.0 is passed by default.

For service specific arguments, check Google API documentation pages in the SEE ALSO section.

REST::Google object completely represents Google API response objects and has the following structure:

        {
                "responseData" => {},
                "responseDetails" => undef | string-on-error,
                "responseStatus" => 200 | error-code
        }
$res->responseStatus()

The responseStatus property contains a value of 200 on success and a non-200 HTTP error status code on failure. If there is a failure, responseDetails contains a diagnostic string.

$res->responseDetails()

Contain an error string if responseStatus is not 200.

$res->responseData()

Returns a responseData structure. Please refer to service API documentation for response structure details.

DEPENDENCIES

REST::Google uses JSON::Any for decoding Google AJAX Search API response and LWP for search request sending.

SEE ALSO

REST::Google::Search, REST::Google::Translate, REST::Google::Feeds child classes for Search, Translate and Feeds services correspondingly.

http://github.com/esobchenko/rest-google/ - this project on github;

http://code.google.com/apis/ajaxsearch/documentation/#fonje - brief information about Google Search AJAX API in non-Javascript environments;

http://code.google.com/apis/ajaxsearch/documentation/reference.html#_intro_fonje - Google Search AJAX API documentation;

http://code.google.com/apis/ajaxlanguage/documentation/#fonje - Google Translate AJAX API documentation;

http://code.google.com/apis/ajaxfeeds/documentation/#fonje - Google Feeds AJAX API documentation;

LICENSE AND COPYRIGHT

Copyright 2008, Eugen Sobchenko <ejs@cpan.org> and Sergey Sinkovskiy <glorybox@cpan.org>

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.