Chapter 14. Zend_Gdata

Table of Contents

14.1. Introduction to Gdata
14.1.1. Google Data Client Authentication
14.1.2. Dependencies
14.1.3. Creating a new Gdata client
14.1.4. Common query parameters
14.1.5. Fetching a feed
14.1.6. Posting entries to Google servers
14.1.7. Deleting entries on Google servers
14.2. Authenticating with AuthSub
14.2.1. Creating an AuthSub authenticated Http Client
14.2.2. Revoking AuthSub authentication
14.3. Authenticating with ClientLogin
14.3.1. Creating a ClientLogin authenticated Http Client
14.3.2. Terminating a ClientLogin authenticated Http Client
14.4. Using Google Base
14.4.1. Querying Base data
14.4.2. Updating Base data
14.4.3. Querying Base metadata
14.5. Using Google Blogger
14.5.1. Understanding Blogger version compatibility
14.5.2. Querying a blog
14.5.3. Posting to your blog
14.6. Using Google Calendar
14.6.1. Querying Google Calendar
14.6.2. Posting to Google Calendar
14.7. Using Google CodeSearch
14.8. Using the Gdata Data Helper
14.9. Catching Gdata Exceptions

14.1. Introduction to Gdata

Google Data APIs provide programmatic interface to some of Google's online services. Client applications submit queries and data via HTTP, and retrieve results. The Zend_Gdata component is a PHP 5 interface for accessing Google Data from PHP.

See http://code.google.com/apis/gdata/ for more information about Google Data API.

The services that are accessible by Zend_Gdata include the following:

  • Google Base provides a search tool for online products, jobs, events, personal ads, and other types of entries. People post items and describe them with commonly-named attributes.

  • Google Blogger is a popular internet provider of "push-button publishing" and syndication.

  • Google Calendar is an online scheduling and collaboration tool.

  • Google CodeSearch allows you to search public source code from many projects.

[Note] Unsupported services

Google Data API also supports Google Spreadsheets, but Zend_Gdata does not provide a helper class for Spreadsheets in this release.

Zend_Gdata does not provide an interface to any other Google service, such as Search, Gmail, Translation, or Maps. Only services that support the Google Data API are supported.

14.1.1. Google Data Client Authentication

Most Google Data services require client applications to authenticate against the Google server before accessing private data, or saving or deleting data. There are two implementations of authentication for Google Data: AuthSub and ClientLogin. Zend_Gdata offers class interfaces for both of these methods.

Most other types of queries against Google Data services do not require authentication.

14.1.2. Dependencies

Zend_Gdata makes use of Zend_Feed and Zend_Http_Client to send requests to google.com and fetch results. The response to most Google Data requests is returned as a Zend_Feed object in the format of an Atom or RSS feed.

Zend_Gdata assumes your PHP application is running on a host that has a direct connection to the internet. The Zend_Gdata client operates by contacting Google Data servers.

14.1.3. Creating a new Gdata client

Create a new object of class Zend_Gdata or one of the subclasses available that offer helper methods for service-specific behavior.

The single optional parameter to the Zend_Gdata constructor is an instance of Zend_Http_Client. If you don't pass this parameter, Zend_Gdata creates a default Zend_Http_Client object. Specifying the Zend_Http_Client object allows you to pass configuration options to that client object.

<?php
require_once 'Zend/Gdata.php';
require_once 'Zend/Http/Client.php';

$client = new Zend_Http_Client();
$client->setConfig( ...options... );

$gdata = new Zend_Gdata($client);
?>

Also see the sections on authentication for methods to create an authenticated Zend_Http_Client object.

14.1.4. Common query parameters

You can specify parameters to customize queries with Zend_Gdata. Some parameters apply only to individual Google Data services. These parameters are documented in later sections for each service. Some parameters apply in a common way to multiple Google Data services. These parameters are described below.

  • The alt parameter specifies the feed type. The value of the parameter can be atom, rss, json, or json-in-script. If you don't specify this parameter, the default feed type is atom.

    Set this parameter with the setAlt() function.

  • The maxResults parameter limits the number of entries in the feed. The value of the parameter is an integer. The number of entries returned in the feed will not exceed this value.

    Set this parameter with the setMaxResults() function.

  • The startIndex parameter specifies the ordinal number of the first entry returned in the feed. Entries before this number are skipped.

    Set this parameter with the setStartIndex() function.

  • The updatedMin and updatedMax parameters specify bounds on the entry date. If you specify a value for updatedMin, no entries that were updated earlier than the date you specify are included in the feed. Likewise no entries updated after the date specified by updatedMax are included.

    You can use numeric timestamps, or a variety of date/time string representations as the value for these parameters.

    Set this parameter with the setUpdatedMin() and setUpdatedMax() functions.

There is a get function for each set function.

<?php
$gdata = new Zend_Gdata();
$gdata->setMaxResults(10);
echo $gdata->getMaxResults();   // returns 10
?>

The Zend_Gdata class also implements "magic" getter and setter methods, so you can use the name of the parameter as a virtual member of the class. You can use isset() and unset() on these virtual members.

<?php
$gdata = new Zend_Gdata();
$gdata->maxResults = 10;
echo $gdata->maxResults;        // returns 10
echo isset($gdata->maxResults); // returns true
unset($gdata->maxResults);      // clears parameter
?>

You can clear all parameters with the resetParameters() function. This is useful to do if you reuse a Zend_Gdata object for multiple queries.

<?php
$gdata = new Zend_Gdata();
$gdata->maxResults = 10;
// ...get feed...

$gdata->resetParameters();      // clears all parameters
// ...get a different feed...
?>

14.1.5. Fetching a feed

Use the getFeed() function to retrieve a feed from a specified URI. This function returns an object of class Zend_Feed.

<?php
$gdata = new Zend_Gdata();
$gdata->setQuery('digital camera');
$gdata->setMaxResults(10);
$uri = 'http://www.google.com/base/feeds/snippets'
    . $gdata->getQueryString();
$feed = $gdata->getFeed($uri);
?>

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

See later sections for special functions in each helper class for Google Data services. These functions help you to get feeds from the URI that is appropriate for the respective service.

14.1.6. Posting entries to Google servers

The Zend_Gdata object has a function post() with which you can upload data to save new entries to Google Data services.

You are responsible for constructing a string containing the correct XML structure for an entry of the respective Google Data service you are using. You may use PHP class libraries such as DOM XML or SimpleXML.

<?php
$gdata = new Zend_Gdata($authenticatedHttpClient);

$xmlString = <<<XML
<entry xmlns='http://www.w3.org/2005/Atom'
    xmlns:gd='http://schemas.google.com/g/2005'>
  <category scheme='http://schemas.google.com/g/2005#kind'
    term='http://schemas.google.com/g/2005#event'></category>
  <title type='text'>Tennis with Beth</title>
  <content type='text'>Meet for a quick lesson.</content>
  <author>
    <name>Jo March</name>
    <email>jo@gmail.com</email>
  </author>
  <gd:transparency
    value='http://schemas.google.com/g/2005#event.opaque'>
  </gd:transparency>
  <gd:eventStatus
    value='http://schemas.google.com/g/2005#event.confirmed'>
  </gd:eventStatus>
  <gd:where valueString='Rolling Lawn Courts'></gd:where>
  <gd:when startTime='2006-04-17T15:00:00.000Z'
    endTime='2006-04-17T17:00:00.000Z'></gd:when>
</entry>
XML;

$xml = new SimpleXMLElement($xmlString);

$myCalendar = 'http://www.google.com/calendar/feeds/default/private/full';
$gdata->post($xml->asXML(), $myCalendar);
?>

To post entries, you must be using an authenticated Zend_Http_Client that you created using the Zend_Gdata_AuthSub or Zend_Gdata_ClientLogin classes.

14.1.7. Deleting entries on Google servers

The Zend_Gdata object has a function delete() with which you can delete entries from Google Data services. Pass the <id> value from a feed entry to the delete() method.

<?php
$gdata = new Zend_Gdata($authenticatedHttpClient);
// a Google Data feed
$feedUri = ...;
$feed = $gdata->getFeed($feedUri);
foreach ($feed as $feedEntry) {
    // extract the <id> element
    $id = $feedEntry->id();
    $gdata->delete($id);
}
?>

You can also use the delete() method without a feed object, if you pass a correct URI identifying an entry.

<?php
$gdata = new Zend_Gdata($authenticatedHttpClient);
$uri = 'http://www.google.com/calendar/feeds/default/private/entryID';
$gdata->delete($uri);
?>

In the above example, replace "entryID" with the ID value for the specific entry you want to delete.

To delete entries, you must be using an authenticated Zend_Http_Client that you created using the Zend_Gdata_AuthSub or Zend_Gdata_ClientLogin classes.