IntroductionGoogle Data APIs provide programmatic interface to some of Google's online services. The Google data Protocol is based upon the » Atom Publishing Protocol and allows client applications to retrieve data matching queries, post data, update data and delete data using standard HTTP and the Atom syndication formation. The Zend_Gdata component is a PHP 5 interface for accessing Google Data from PHP. The Zend_Gdata component also supports accessing other services implementing the Atom Publishing Protocol. 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:
Structure of Zend_GdataZend_Gata is composed of several types of classes:
Interacting with Google ServicesGoogle data services are based upon the Atom Publishing Protocol (APP) and the Atom syndication format. To interact with APP or Google services using the Zend_Gdata component, you need to use the service classes such as Zend_Gdata_App, Zend_Gdata, Zend_Gdata_Spreadsheets, etc. These service classes provide methods to retrieve data from services as feeds, insert new entries into feeds, update entries, and delete entries. Note: A full example of working with Zend_Gdata is available in the demos/Zend/Gdata directory. This example is runnable from the command-line, but the methods contained within are easily portable to a web application. Obtaining instances of Zend_Gdata classesThe Zend Framework naming standards require that all classes be named based upon the directory structure in which they are located. For instance, extensions related to Spreadsheets are stored in: Zend/Gdata/Spreadsheets/Extension/... and, as a result of this, are named Zend_Gdata_Spreadsheets_Extension_.... This causes a lot of typing if you're trying to construct a new instance of a spreadsheet cell element! We've implemented a magic factory method in all service classes (such as Zend_Gdata_App, Zend_Gdata, Zend_Gdata_Spreadsheets) that should make constructing new instances of data model, query and other classes much easier. This magic factory is implemented by using the magic __call() method to intercept all attempts to call $service->newXXX(arg1, arg2, ...). Based off the value of XXX, a search is performed in all registered 'packages' for the desired class. Here's some examples:
Each service class in the inheritance tree is responsible for registering the appropriate 'packages' (directories) which are to be searched when calling the magic factory method. Google Data Client AuthenticationMost 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. DependenciesZend_Gdata makes use of Zend_Http_Client to send requests to google.com and fetch results. The response to most Google Data requests is returned as a subclass of the Zend_Gdata_App_Feed or Zend_Gdata_App_Entry classes. 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. Creating a new Gdata clientCreate a new object of class Zend_Gdata_App, Zend_Gdata, or one of the subclasses available that offer helper methods for service-specific behavior. The single optional parameter to the Zend_Gdata_App constructor is an instance of Zend_Http_Client. If you don't pass this parameter, Zend_Gdata creates a default Zend_Http_Client object, which will not have associated credentials to access private feeds. Specifying the Zend_Http_Client object also allows you to pass configuration options to that client object. Beginning with Zend Framework 1.7, support has been added for protocol versioning. This allows the client and server to support new features while maintaining backwards compatibility. While most services will manage this for you, if you create a Zend_Gdata instance directly (as opposed to one of its subclasses), you may need to specify the desired protocol version to access certain server functionality. Also see the sections on authentication for methods to create an authenticated Zend_Http_Client object. Common Query ParametersYou can specify parameters to customize queries with Zend_Gdata. Query parameters are specified using subclasses of Zend_Gdata_Query. The Zend_Gdata_Query class includes methods to set all query parameters used throughout GData services. Individual services, such as Spreadsheets, also provide query classes to defined parameters which are custom to the particular service and feeds. Spreadsheets includes a CellQuery class to query the Cell Feed and a ListQuery class to query the List Feed, as different query parameters are applicable to each of those feed types. The GData-wide parameters are described below.
There is a get*() function for each set*() function.
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 clear all parameters with the resetParameters() function. This is useful to do if you reuse a Zend_Gdata object for multiple queries.
Fetching a FeedUse the getFeed() function to retrieve a feed from a specified URI. This function returns an instance of class specified as the second argument to getFeed, which defaults to Zend_Gdata_Feed.
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. Working with Multi-page FeedsWhen retrieving a feed that contains a large number of entries, the feed may be broken up into many smaller "pages" of feeds. When this occurs, each page will contain a link to the next page in the series. This link can be accessed by calling getLink('next'). The following example shows how to retrieve the next page of a feed:
If you would prefer not to work with pages in your application, pass the first page of the feed into Zend_Gdata_App::retrieveAllEntriesForFeed(), which will consolidate all entries from each page into a single feed. This example shows how to use this function:
Keep in mind when calling this function that it may take a long time to complete on large feeds. You may need to increase PHP's execution time limit by calling set_time_limit(). Working with Data in Feeds and EntriesAfter retrieving a feed, you can read the data from the feed or the entries contained in the feed using either the accessors defined in each of the data model classes or the magic accessors. Here's an example:
Updating EntriesAfter retrieving an entry, you can update that entry and save changes back to the server. Here's an example:
Posting Entries to Google ServersThe Zend_Gdata object has a function insertEntry() with which you can upload data to save new entries to Google Data services. You can use the data model classes for each service to construct the appropriate entry to post to Google's services. The insertEntry() function will accept a child of Zend_Gdata_App_Entry as data to post to the service. The method returns a child of Zend_Gdata_App_Entry which represents the state of the entry as it was returned from the server. Alternatively, you could construct the XML structure for an entry as a string and pass the string to the insertEntry() function.
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. Deleting Entries on Google ServersOption 1: The Zend_Gdata object has a function delete() with which you can delete entries from Google Data services. Pass the edit URL value from a feed entry to the delete() method. Option 2: Alternatively, you can call $entry->delete() on an entry retrieved from a Google service.
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.
|
|