The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Search::Google - search Google using the REST (aka AJAX) API

VERSION

This documentation refers to Search::Google version 1.0.4

SYNOPSIS

        use Search::Google;

        # you should provide a valid http referer address according
        # to Google AJAX Search API terms of use!
        Search::Google->http_referer('http://example.com');

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

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

        my $data = $res->responseData;

        my $cursor = $data->cursor;

        printf "pages: %s\n", $cursor->pages;
        printf "current page index: %s\n", $cursor->currentPageIndex;
        printf "estimated result count: %s\n", $cursor->estimatedResultCount;

        my @results = $data->results;

        foreach my $r (@results) {
                printf "\n";
                printf "title: %s\n", $r->title;
                printf "url: %s\n", $r->url;
        }

DESCRIPTION

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

METHODS

__PACKAGE__->service()

Get/set service to use.

The following table lists the URL used to access Google search services:

        service   address
        -------   ------------------------------------------------------
        WEB       http://ajax.googleapis.com/ajax/services/search/web
        VIDEO     http://ajax.googleapis.com/ajax/services/search/video
        NEWS      http://ajax.googleapis.com/ajax/services/search/news
        LOCAL     http://ajax.googleapis.com/ajax/services/search/local
        IMAGES    http://ajax.googleapis.com/ajax/services/search/images
        BOOKS     http://ajax.googleapis.com/ajax/services/search/books
        BLOGS     http://ajax.googleapis.com/ajax/services/search/blogs

Service constants are exported by default, so following lines are equal:

        Search::Google->service( 'http://ajax.googleapis.com/ajax/services/search/video' );
        Search::Google->service( VIDEO );

Default service is WEB.

__PACKAGE__->http_referer()

Get/set HTTP Referer header.

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

Note: Google says that you should supply a valid HTTP referer header each time you perform the search request using 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 Search API' documentation for complete list of arguments for service you're using. Example:

        my $res = Search::Google->new(
                q => 'Pamela Anderson',
                start => 4,
                hl => 'fr'
        );

If you're using the default (WEB) serice, the code above will perform a following HTTP GET request:

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

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

Search::Google objects are completely represent Google AJAX API search response objects and have the following structure:

        {
                "responseData" => {
                        "results" => [],
                        "cursor" => {}
                },
                "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 an object that provides OO access to Google responseData structure. This object has actually two methods: a results that returns the array of result objects for the given service and cursor method that return cursor object (if exists).

Please refer to 'Google AJAX Search API' documentation for response structure details.

CLASSES

Search::Google contains dedicated classes for each of supported services. You may use them instead of specifying certain service with service class method. E.g:

        use Search::Google::Blogs;
        # ...
        # set referer, etc
        my $r = Search::Google::Blogs->new( q => 'foobar' );

is the same as:

        use Search::Google;
        Search::Google->service( BLOGS );
        # ...
        # set referer, etc
        my $r = Search::Google->new( q => 'foobar' );

Available classes:

Search::Google::Web
Search::Google::Blogs
Search::Google::Books
Search::Google::Images
Search::Google::Local
Search::Google::Video
Search::Google::News

DEPENDENCIES

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

SEE ALSO

http://code.google.com/p/search-google/ - this project on Google code;

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

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

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.