14.4. Using Google Base

The Google Data API interface to Google Base allows developers to write applications to perform search queries against the Google Base database of products, services, and events. You can also input and manage Google Base entries programmatically.

See http://code.google.com/apis/base/ for more information on the Google Base API.

14.4.1. Querying Base data

You can use the setQuery() method to specify full-text search terms for a Google Base query.

<?php
$gdataBase = new Zend_Gdata_Base();
$gdataBase->setQuery('digital camera');
$feed = $gdataBase->getBaseFeed();
?>

See the documentation for Zend_Feed for more information on how to use this object.

Google Base also supports attribute-based query terms. You can specify additional terms using the addAttribute() method. The parameters of this method are attribute name, attribute value, and an optional operator. The default operator is ':' which is interpreted as string equality.

Attribute queries can be combined with full-text queries.

<?php
$gdataBase = new Zend_Gdata_Base();
$gdataBase->setQuery('digital camera');
$gdataBase->addAttributeQuery('price', '50 USD', '<');
$feed = $gdataBase->getBaseFeed();
?>

You can add multiple attributes using the addAttributeQuery() method, even multiple attribute terms for the same attribute name.

You can clear attribute query terms using the unsetAttributeQuery() method. The parameter is the attribute name. If you have added multiple terms for the same attribute name, all of them are cleared. If you specify no attribute name in the parameter to this method, all attribute query terms are cleared.

The addAttribute() method does not support advanced expression syntax including parentheses, negation, and boolean operators. You can enter such expressions as a string, using the setQuery() method.

<?php
$gdataBase = new Zend_Gdata_Base();
$gdataBase->setQuery('digital camera ( [price < 50 USD] | [location: @"Mountain View, CA 94043"] )');
$feed = $gdataBase->getBaseFeed();
?>

You can sort the results of feeds by different criteria. You can specify other sorting criteria using the setOrderby() method. Typical values are modification-time, name, or relevancy. You can also specify advanced ranking logic, using the Ranking Language. By default, the order entries are returned is by relevancy.

14.4.2. Updating Base data

Updating data requires you to establish two things in your Zend_Gdata_Base object: an authenticated Http Client, and a developer key.

A developer key is typically a 54-character alphanumeric string. See http://code.google.com/apis/base/starting-out.html#authDev for instructions on acquiring a developer key.

You can set the developer key using the optional second parameter in the constructor for Zend_Gdata_Base. You can also set the developer key of an existing object, using the setDeveloperKey() method. Once you set the developer key for the Zend_Gdata_Base object, the key is sent automatically in subsequent requests.

<?php
$gdataBase = new Zend_Gdata_Base($authenticatedHttpClient);
$gdataBase->setDeveloperKey($myDeveloperKey);
?>

Posting enties to Google Base is done using the post() method that is common to Zend_Gdata classes. See the documentation for the Google Base Data API for details on the correct elements of the XML entries.

14.4.3. Querying Base metadata

You can query feeds of Google Base itemtypes using the getItemTypesFeed() method. The first parameter to this method is a locale, for instance "en_US". The second parameter is optional; it names an item type. If you specify an item type, the method returns a feed of attributes for that item type. If you do not specify an item type, the method returns a feed of all item types.

<?php
$gdataBase = new Zend_Gdata_Base();

// Get list of all item types
$itemTypes = $gdataBase->getItemTypesFeed('en_US');

// Get list of attributes for the 'jobs' item type.
$jobsAttributes = $gdataBase->getItemTypesFeed('en_US', 'jobs');
?>

You can query statistics about a given item type, such as the number of Google Base entries that use individual values for each attribute. Use the getItemTypeAttributesFeed() method for this type of query.

<?php
$gdataBase = new Zend_Gdata_Base();
$jobsAttributesStatistics = $gdataBase->getItemTypeAttributesFeed('jobs');
?>