<?xml version="1.0" encoding="UTF-8"?>
<!-- Reviewed: no -->
<sect1 id="zend.gdata.books">
    <title>Using the Book Search Data API</title>

    <para>
        The Google Book Search Data <acronym>API</acronym> allows client applications to view
        and update Book Search content in the form of Google Data <acronym>API</acronym> feeds.
    </para>

    <para>
        Your client application can use the Book Search Data <acronym>API</acronym> to issue
        full-text searches for books and to retrieve standard book information,
        ratings, and reviews. You can also access individual users'
        <ulink url="http://books.google.com/googlebooks/mylibrary/">library collections and
        public reviews</ulink>. Finally, your application can submit authenticated requests
        to enable users to create and modify library collections, ratings, labels,
        reviews, and other account-specific entities.
    </para>

    <para>
        For more information on the Book Search Data <acronym>API</acronym>, please refer to the official
        <ulink url="http://code.google.com/apis/books/gdata/developers_guide_php.html">PHP
        Developer's Guide</ulink> on code.google.com.
    </para>

    <sect2 id="zend.gdata.books.authentication">

        <title>Authenticating to the Book Search service</title>

        <para>
            You can access both public and private feeds using the Book Search
            Data <acronym>API</acronym>. Public feeds don't require any authentication, but they are
            read-only. If you want to modify user libraries, submit reviews or
            ratings, or add labels, then your client needs to authenticate before
            requesting private feeds. It can authenticate using either of two
            approaches: AuthSub proxy authentication or ClientLogin username/password
            authentication. Please refer to the <ulink
            url="http://code.google.com/apis/books/gdata/developers_guide_php.html#Authentication">
            Authentication section in the <acronym>PHP</acronym> Developer's Guide</ulink> for more detail.
        </para>

    </sect2>

    <sect2 id="zend.gdata.books.searching_for_books">

        <title>Searching for books</title>

        <para>
            The Book Search Data <acronym>API</acronym> provides a number of feeds that list collections of books.
        </para>

        <para>
            The most common action is to retrieve a list of books that match a
            search query. To do so you create a <code>VolumeQuery</code> object
            and pass it to the <code>Books::getVolumeFeed</code> method.
        </para>

        <para>
            For example, to perform a keyword query, with a filter on
            viewability to restrict the results to partial or full view books, use
            the <code>setMinViewability</code> and <code>setQuery</code> methods
            of the <code>VolumeQuery</code> object. The following code snippet prints
            the title and viewability of all volumes whose metadata or text matches
            the query term "domino":
        </para>

        <programlisting language="php"><![CDATA[
$books = new Zend_Gdata_Books();
$query = $books->newVolumeQuery();

$query->setQuery('domino');
$query->setMinViewability('partial_view');

$feed = $books->getVolumeFeed($query);

foreach ($feed as $entry) {
    echo $entry->getVolumeId();
    echo $entry->getTitle();
    echo $entry->getViewability();
}
]]></programlisting>

        <para>
            The <code>Query</code> class, and subclasses like
            <code>VolumeQuery</code>, are responsible for constructing feed
            <acronym>URL</acronym>s. The VolumeQuery shown above constructs a <acronym>URL</acronym> equivalent to the
            following:
        </para>

        <programlisting language="php"><![CDATA[
http://www.google.com/books/feeds/volumes?q=keyword&amp;min-viewability=partial
]]></programlisting>

        <para>
            Note: Since Book Search results are
            public, you can issue a Book Search query without authentication.
        </para>

        <para>
            Here are some of the most common <code>VolumeQuery</code>
            methods for setting search parameters:
        </para>

        <para>
            <code>setQuery:</code> Specifies a search
            query term. Book Search searches all book metadata and full text for
            books matching the term. Book metadata includes titles, keywords,
            descriptions, author names, and subjects.
            Note that any spaces, quotes or other punctuation in the parameter
            value must be <acronym>URL</acronym>-escaped. (Use a plus (<code>+</code>) for a space.)
            To search for an exact phrase, enclose the phrase in quotation marks.
            For example, to search for books matching the phrase "spy plane", set
            the <code>q</code> parameter to <code>%22spy+plane%22</code>.
            You can also use any of the <ulink url="http://books.google.com/advanced_book_search">
            advanced search operators</ulink> supported by Book Search. For example,
            <code>jane+austen+-inauthor:austen</code> returns matches that mention
            (but are not authored by) Jane Austen.
        </para>

        <para>
            <code>setStartIndex:</code> Specifies
            the index of the first matching result that should be included in the
            result set. This parameter uses a one-based index, meaning the first
            result is 1, the second result is 2 and so forth. This parameter works
            in conjunction with the max-results
            parameter to determine which results to return. For example, to
            request the third set of 10 results—results 21-30—set
            the <code>start-index</code> parameter to <code>21</code> and the
            max-results parameter to <code>10</code>.
            Note: This isn't a general cursoring
            mechanism. If you first send a query with
            <code>?start-index=1&amp;max-results=10</code> and then send another
            query with <code>?start-index=11&amp;max-results=10</code>, the
            service cannot guarantee that the results are equivalent to
            <code>?start-index=1&amp;max-results=20</code>, because insertions and
            deletions could have taken place in between the two queries.
        </para>

        <para>
            <code>setMaxResults:</code>
            Specifies the maximum number of results that should be included
            in the result set. This parameter works in conjunction with the
            start-index parameter to determine which
            results to return. The default value of this parameter is
            <code>10</code> and the maximum value is <code>20</code>.
        </para>

        <para>
            <code>setMinViewability:</code>
            Allows you to filter the results according to the books'
            <ulink url="http://code.google.com/apis/books/docs/dynamic-links.html#terminology">
            viewability status</ulink>.
            This parameter accepts one of three values: <code>'none'</code>
            (the default, returning all matching books regardless of
            viewability), <code>'partial_view'</code> (returning only books
            that the user can preview or view in their entirety), or
            <code>'full_view'</code> (returning only books that the user can
            view in their entirety).
        </para>

        <sect3 id="zend.gdata.books.partner_restrict">

            <title>Partner Co-Branded Search</title>

            <para>
                Google Book Search provides <ulink
                url="http://books.google.com/support/partner/bin/answer.py?hl=en&amp;answer=65113">
                Co-Branded Search</ulink>, which lets content partners provide full-text search of
                their books from their own websites.
            </para>

            <para>
                If you are a partner who wants to do Co-Branded Search using the
                Book Search Data <acronym>API</acronym>, you may do so by modifying the feed <acronym>URL</acronym> above to
                point to your Co-Branded Search implementation. If, for example, a
                Co-Branded Search is available at the following <acronym>URL</acronym>:
            </para>

            <programlisting language="php"><![CDATA[
http://www.google.com/books/p/PARTNER_COBRAND_ID?q=ball
]]></programlisting>

            <para>
                then you can obtain the same results using the Book Search Data <acronym>API</acronym> at the
                following <acronym>URL</acronym>:
            </para>

            <programlisting language="php"><![CDATA[
http://www.google.com/books/feeds/p/PARTNER_COBRAND_ID/volumes?q=ball+-soccer
]]></programlisting>

            <para>
                To specify an alternate <acronym>URL</acronym> when querying a volume feed, you can
                provide an extra parameter to <code>newVolumeQuery</code>
            </para>

            <programlisting language="php"><![CDATA[
$query =
    $books->newVolumeQuery('http://www.google.com/books/p/PARTNER_COBRAND_ID');
]]></programlisting>

            <para>
                For additional information or support, visit our
                <ulink url="http://books.google.com/support/partner/">Partner help center</ulink>.
            </para>

        </sect3>

    </sect2>

    <sect2 id="zend.gdata.books.community_features">

        <title>Using community features</title>

        <sect3 id="zend.gdata.books.adding_ratings">

            <title>Adding a rating</title>

            <para>
                A user can add a rating to a book. Book Search uses a 1-5
                rating system in which 1 is the lowest rating. Users cannot
                update or delete ratings.
            </para>

            <para>
                To add a rating, add a <code>Rating</code> object to a
                <code>VolumeEntry</code> and post it to the annotation feed.
                In the example below, we start from an empty <code>VolumeEntry</code> object.
            </para>

            <programlisting language="php"><![CDATA[
$entry = new Zend_Gdata_Books_VolumeEntry();
$entry->setId(new Zend_Gdata_App_Extension_Id(VOLUME_ID));
$entry->setRating(new Zend_Gdata_Extension_Rating(3, 1, 5, 1));
$books->insertVolume($entry, Zend_Gdata_Books::MY_ANNOTATION_FEED_URI);
]]></programlisting>

        </sect3>

        <sect3 id="zend.gdata.books.reviews">

            <title>Reviews</title>

            <para>
                In addition to ratings, authenticated users can submit reviews or
                edit their reviews. For information on how to request previously
                submitted reviews, see <ulink
                url="#zend.gdata.books.retrieving_annotations">Retrieving annotations</ulink>.
            </para>

            <sect4 id="zend.gdata.books.adding_review">

                <title>Adding a review</title>

                <para>
                    To add a review, add a <code>Review</code> object to a
                    <code>VolumeEntry</code> and post it to the annotation
                    feed. In the example below, we start from an existing
                    <code>VolumeEntry</code> object.
                </para>

                <programlisting language="php"><![CDATA[
$annotationUrl = $entry->getAnnotationLink()->href;
$review        = new Zend_Gdata_Books_Extension_Review();

$review->setText("This book is amazing!");
$entry->setReview($review);
$books->insertVolume($entry, $annotationUrl);
]]></programlisting>

            </sect4>

            <sect4 id="zend.gdata.books.editing_review">

                <title>Editing a review</title>

                <para>
                    To update an existing review, first you retrieve the
                    review you want to update, then you modify it, and
                    then you submit it to the annotation feed.
                </para>

                <programlisting language="php"><![CDATA[
$entryUrl = $entry->getId()->getText();
$review   = new Zend_Gdata_Books_Extension_Review();

$review->setText("This book is actually not that good!");
$entry->setReview($review);
$books->updateVolume($entry, $entryUrl);
]]></programlisting>

            </sect4>

        </sect3>

        <sect3 id="zend.gdata.books.labels">

            <title>Labels</title>

            <para>
                You can use the Book Search Data <acronym>API</acronym> to label volumes with
                keywords. A user can submit, retrieve and modify labels. See
                <ulink url="#zend.gdata.books.retrieving_annotations">Retrieving
                annotations</ulink> for how to read previously submitted labels.
            </para>

            <sect4 id="zend.gdata.books.submitting_labels">

                <title>Submitting a set of labels</title>

                <para>
                    To submit labels, add a <code>Category</code> object
                    with the scheme <constant>LABELS_SCHEME</constant> to a
                    <code>VolumeEntry</code> and post it to the annotation feed.
                </para>

                <programlisting language="php"><![CDATA[
$annotationUrl = $entry->getAnnotationLink()->href;
$category      = new Zend_Gdata_App_Extension_Category(
    'rated',
    'http://schemas.google.com/books/2008/labels');
$entry->setCategory(array($category));
$books->insertVolume($entry, Zend_Gdata_Books::MY_ANNOTATION_FEED_URI);
]]></programlisting>

            </sect4>

        </sect3>

        <sect3 id="zend.gdata.books.retrieving_annotations">

            <title>Retrieving annotations: reviews, ratings, and labels</title>

            <para>
                You can use the Book Search Data <acronym>API</acronym> to retrieve annotations
                submitted by a given user. Annotations include reviews, ratings, and
                labels. To retrieve any user's annotations, you can send an
                unauthenticated request that includes the user's user ID. To retrieve the
                authenticated user's annotations, use the value <code>me</code> as the user ID.
            </para>

            <programlisting language="php"><![CDATA[
$feed = $books->getVolumeFeed(
            'http://www.google.com/books/feeds/users/USER_ID/volumes');
<i>(or)</i>
$feed = $books->getUserAnnotationFeed();

// print title(s) and rating value
foreach ($feed as $entry) {
    foreach ($feed->getTitles() as $title) {
        echo $title;
    }
    if ($entry->getRating()) {
        echo 'Rating: ' . $entry->getRating()->getAverage();
    }
}
]]></programlisting>

            <para>
                For a list of the supported query parameters, see the
                <ulink url="#zend.gdata.books.query_parameters">query parameters</ulink>
                section.
            </para>

        </sect3>

        <sect3 id="zend.gdata.books.deleting_annotations">

            <title>Deleting Annotations</title>

            <para>
                If you retrieved an annotation entry containing ratings,
                reviews, and/or labels, you can remove all annotations
                by calling <code>deleteVolume</code> on that entry.
            </para>

            <programlisting language="php"><![CDATA[
$books->deleteVolume($entry);
]]></programlisting>

        </sect3>

    </sect2>

    <sect2 id="zend.gdata.books.sharing_with_my_library">

        <title>Book collections and My Library</title>

        <para>
            Google Book Search provides a number of user-specific
            book collections, each of which has its own feed.
        </para>

        <para>
            The most important collection is the user's My Library, which
            represents the books the user would like to remember, organize, and
            share with others. This is the collection the user sees when accessing
            his or her <ulink url="http://books.google.com/books?op=library">My Library
            page</ulink>.
        </para>

        <sect3 id="zend.gdata.books.retrieving_books_in_library">

            <title>Retrieving books in a user's library</title>

            <para>
                The following sections describe how to retrieve a list
                of books from a user's library, with or without query
                parameters.
            </para>

            <para>
                You can query a Book Search public feed without authentication.
            </para>

            <sect4 id="zend.gdata.books.retrieving_all_books_in_library">

                <title>Retrieving all books in a user's library</title>

                <para>
                    To retrieve the user's books, send a query to the
                    My Library feed. To get the library of the authenticated
                    user, use <code>me</code> in place of <constant>USER_ID</constant>.
                </para>

                <programlisting language="php"><![CDATA[
$feed = $books->getUserLibraryFeed();
]]></programlisting>

                <para>
                    Note: The feed may not contain all of the user's books, because
                    there's a default limit on the number of results returned. For
                    more information, see the <code>max-results</code> query parameter in
                    <ulink url="#zend.gdata.books.searching_for_books">Searching for books</ulink>.
                </para>

            </sect4>

            <sect4 id="zend.gdata.books.retrieving_books_in_library_with_query">

                <title>Searching for books in a user's library</title>

                <para>
                    Just as you can <ulink
                    url="#zend.gdata.books.searching_for_books">search across all books</ulink>,
                    you can do a full-text search over just the books in a
                    user's library. To do this, just set the appropriate
                    paramters on the <code>VolumeQuery</code> object.
                </para>

                <para>
                    For example, the following query returns all the books in
                    your library that contain the word "bear":
                </para>

                <programlisting language="php"><![CDATA[
$query = $books->newVolumeQuery(
    'http://www.google.com/books/feeds/users' .
    '/USER_ID/collections/library/volumes');
$query->setQuery('bear');
$feed = $books->getVolumeFeed($query);
]]></programlisting>

                <para>
                    For a list of the supported query parameters, see the
                    <ulink url="#zend.gdata.books.query_pParameters">query parameters</ulink>
                    section. In addition, you can search for books that have been
                    <ulink url="#zend.gdata.books.labels">labeled by the user</ulink>:
                </para>

                <programlisting language="php"><![CDATA[
$query = $books->newVolumeQuery(
    'http://www.google.com/books/feeds/users/' .
    'USER_ID/collections/library/volumes');
$query->setCategory(
$query->setCategory('favorites');
$feed = $books->getVolumeFeed($query);
]]></programlisting>

            </sect4>

        </sect3>

        <sect3 id="zend.gdata.books.updating_library">

            <title>Updating books in a user's library</title>

            <para>
                You can use the Book Search Data <acronym>API</acronym> to add a book to, or remove a
                book from, a user's library. Ratings, reviews, and labels are valid
                across all the collections of a user, and are thus edited using the
                annotation feed (see <ulink
                url="#zend.gdata.books.community_features">Using community features</ulink>).
            </para>

            <sect4 id="zend.gdata.books.library_book_add">

                <title>Adding a book to a library</title>

                <para>
                    After authenticating, you can add books to the current user's library.
                </para>

                <para>
                    You can either create an entry from scratch if you
                    know the volume ID, or insert an entry read from any feed.
                </para>

                <para>
                    The following example creates a new entry and adds it to the library:
                </para>

                <programlisting language="php"><![CDATA[
$entry = new Zend_Gdata_Books_VolumeEntry();
$entry->setId(new Zend_Gdata_App_Extension_Id(VOLUME_ID));
$books->insertVolume(
    $entry,
    Zend_Gdata_Books::MY_LIBRARY_FEED_URI
);
]]></programlisting>

                <para>
                    The following example adds an existing
                    <code>VolumeEntry</code> object to the library:
                </para>

                <programlisting language="php"><![CDATA[
$books->insertVolume(
    $entry,
    Zend_Gdata_Books::MY_LIBRARY_FEED_URI
);
]]></programlisting>

            </sect4>

            <sect4 id="zend.gdata.books.library_book_remove">

                <title>Removing a book from a library</title>

                <para>
                    To remove a book from a user's library, call
                    <code>deleteVolume</code> on the <code>VolumeEntry</code> object.
                </para>

                <programlisting language="php"><![CDATA[
$books->deleteVolume($entry);
]]></programlisting>

            </sect4>

        </sect3>

    </sect2>

</sect1>
