Wprowadzenie do Zend Framework

     Nauka Zend Framework


     Przewodnik po Zend Framework

  • Zend_Gdata
  • Zend_Http
  • Zend_InfoCard
  • Zend_Json
  • Zend_Layout
  • Zend_Ldap
  • Zend_Loader
  • Zend_Locale
  • Zend_Log
  • Zend_Mail
  • Zend_Markup
  • Zend_Measure
  • Zend_Memory
  • Zend_Mime
  • Zend_Navigation
  • Zend_Oauth
  • Zend_OpenId
  • Zend_Paginator
  • Zend_Pdf
  • Zend_ProgressBar
  • Zend_Queue
  • Zend_Reflection
  • Zend_Registry
  • Zend_Rest

  • Zend_Search_Lucene
  • Zend_Serializer
  • Zend_Server
  • Zend_Service
  • Zend_Session
  • Zend_Soap
  • Zend_Tag
  • Zend_Test
  • Zend_Text
  • Zend_TimeSync
  • Zend_Tool
  • Zend_Tool_Framework
  • Zend_Tool_Project
  • Zend_Translate
  • Zend_Uri
  • Zend_Validate
  • Zend_Version
  • Zend_View
  • Zend_Wildfire
  • Zend_XmlRpc
  • ZendX_Console_Process_Unix
  • ZendX_JQuery
  • Translation 21.3% Update 2011-11-16 - Revision 24356 - Version ZF 1.11.x

    Rozdział 34. Zend_Gdata

    Spis treści

    34.1. Introduction
    34.1.1. Structure of Zend_Gdata
    34.1.2. Interacting with Google Services
    34.1.3. Obtaining instances of Zend_Gdata classes
    34.1.4. Google Data Client Authentication
    34.1.5. Dependencies
    34.1.6. Creating a new Gdata client
    34.1.7. Common Query Parameters
    34.1.8. Fetching a Feed
    34.1.9. Working with Multi-page Feeds
    34.1.10. Working with Data in Feeds and Entries
    34.1.11. Updating Entries
    34.1.12. Posting Entries to Google Servers
    34.1.13. Deleting Entries on Google Servers
    34.2. Authenticating with AuthSub
    34.2.1. Creating an AuthSub authenticated Http Client
    34.2.2. Revoking AuthSub authentication
    34.3. Using the Book Search Data API
    34.3.1. Authenticating to the Book Search service
    34.3.2. Searching for books Partner Co-Branded Search
    34.3.3. Using community features Adding a rating Reviews Adding a review Editing a review Labels Submitting a set of labels Retrieving annotations: reviews, ratings, and labels Deleting Annotations
    34.3.4. Book collections and My Library Retrieving books in a user's library Retrieving all books in a user's library Searching for books in a user's library Updating books in a user's library Adding a book to a library Removing a book from a library
    34.4. Authenticating with ClientLogin
    34.4.1. Creating a ClientLogin authenticated Http Client
    34.4.2. Terminating a ClientLogin authenticated Http Client
    34.5. Using Google Calendar
    34.5.1. Connecting To The Calendar Service Authentication Creating A Service Instance
    34.5.2. Retrieving A Calendar List
    34.5.3. Retrieving Events Queries Retrieving Events In Order Of Start Time Retrieving Events In A Specified Date Range Retrieving Events By Fulltext Query Retrieving Individual Events
    34.5.4. Creating Events Creating Single-Occurrence Events Event Schedules and Reminders Creating Recurring Events Using QuickAdd
    34.5.5. Modifying Events
    34.5.6. Deleting Events
    34.5.7. Accessing Event Comments
    34.6. Using Google Documents List Data API
    34.6.1. Get a List of Documents
    34.6.2. Upload a Document
    34.6.3. Searching the documents feed Get a List of Word Processing Documents Get a List of Spreadsheets Performing a text query
    34.7. Using Google Health
    34.7.1. Connect To The Health Service Authentication Create A Health Service Instance
    34.7.2. Profile Feed Send a Structured Query Query The Feed Iterate Through The Profile Entries
    34.7.3. Profile List Feed Query The Feed
    34.7.4. Sending Notices to the Register Feed Sending a notice
    34.8. Using Google Spreadsheets
    34.8.1. Create a Spreadsheet
    34.8.2. Get a List of Spreadsheets
    34.8.3. Get a List of Worksheets
    34.8.4. Interacting With List-based Feeds Get a List-based Feed Reverse-sort Rows Send a Structured Query Add a Row Edit a Row Delete a Row
    34.8.5. Interacting With Cell-based Feeds Get a Cell-based Feed Send a Cell Range Query Change Contents of a Cell
    34.9. Using Google Apps Provisioning
    34.9.1. Setting the current domain Setting the domain for the service class Setting the domain for query classes
    34.9.2. Interacting with users Creating a user account Retrieving a user account Retrieving all users in a domain Updating a user account Resetting a user's password Forcing a user to change their password Suspending a user account Granting administrative rights Deleting user accounts
    34.9.3. Interacting with nicknames Creating a nickname Retrieving a nickname Retrieving all nicknames for a user Retrieving all nicknames in a domain Deleting a nickname
    34.9.4. Interacting with groups Creating a group Retrieving an individual group Retrieving all groups in a domain Deleting a group Updating a group Retrieving all groups to which a person is a member
    34.9.5. Interacting with group members Adding a member to a group Check to see if member belongs to group Removing a member from a group Retrieving the list of members to a group
    34.9.6. Interacting with group owners Adding an owner to a group Retrieving the list of the owner of a group Check to see if an email is the owner of a group Removing an owner from a group
    34.9.7. Interacting with email lists Creating an email list Retrieving all email lists to which a recipient is subscribed Retrieving all email lists in a domain Deleting an email list
    34.9.8. Interacting with email list recipients Adding a recipient to an email list Retrieving the list of subscribers to an email list Removing a recipient from an email list
    34.9.9. Handling errors
    34.10. Using Google Base
    34.10.1. Connect To The Base Service Authentication Create A Service Instance
    34.10.2. Retrieve Items Send a Structured Query Query Customer Items Feed Query Snippets Feed Iterate through the Items
    34.10.3. Insert, Update, and Delete Customer Items Insert an Item Modify an Item Delete an Item
    34.11. Using Picasa Web Albums
    34.11.1. Connecting To The Service Authentication Creating A Service Instance
    34.11.2. Understanding and Constructing Queries
    34.11.3. Retrieving Feeds And Entries Retrieving A User Retrieving An Album Retrieving A Photo Retrieving A Comment Retrieving A Tag
    34.11.4. Creating Entries Creating An Album Creating A Photo Creating A Comment Creating A Tag
    34.11.5. Deleting Entries Deleting An Album Deleting A Photo Deleting A Comment Deleting A Tag Optimistic Concurrency (Notes On Deletion)
    34.12. Using the YouTube Data API
    34.12.1. Authentication
    34.12.2. Developer Keys and Client ID
    34.12.3. Retrieving public video feeds Searching for videos by metadata Searching for videos by categories and tags/keywords Retrieving standard feeds Retrieving videos uploaded by a user Retrieving videos favorited by a user Retrieving video responses for a video
    34.12.4. Retrieving video comments
    34.12.5. Retrieving playlist feeds Retrieving the playlists of a user Retrieving a specific playlist
    34.12.6. Retrieving a list of a user's subscriptions
    34.12.7. Retrieving a user's profile
    34.12.8. Uploading Videos to YouTube
    34.12.9. Browser-based upload
    34.12.10. Checking upload status
    34.12.11. Other Functions
    34.13. Catching Gdata Exceptions

    34.1. Introduction

    Google 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:

    • Google Calendar is a popular online calendar application.

    • Google Spreadsheets provides an online collaborative spreadsheets tool which can be used as a simple data store for your applications.

    • Google Documents List provides an online list of all spreadsheets, word processing documents, and presentations stored in a Google account.

    • Google Provisioning provides the ability to create, retrieve, update, and delete user accounts, nicknames, groups, and email lists on a Google Apps hosted domain.

    • Google Base provides the ability to retrieve, post, update, and delete items in Google Base.

    • YouTube provides the ability to search and retrieve videos, comments, favorites, subscriptions, user profiles and more.

    • Picasa Web Albums provides an online photo sharing application.

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

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

    • Google Notebook allows you to view public Notebook content.

    [Notatka] Unsupported services

    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.

    34.1.1. Structure of Zend_Gdata

    Zend_Gata is composed of several types of classes:

    • Service classes - inheriting from Zend_Gdata_App. These also include other classes such as Zend_Gdata, Zend_Gdata_Spreadsheets, etc. These classes enable interacting with APP or GData services and provide the ability to retrieve feeds, retrieve entries, post entries, update entries and delete entries.

    • Query classes - inheriting from Zend_Gdata_Query. These also include other classes for specific services, such as Zend_Gdata_Spreadsheets_ListQuery and Zend_Gdata_Spreadsheets_CellQuery. Query classes provide methods used to construct a query for data to be retrieved from GData services. Methods include getters and setters like setUpdatedMin(), setStartIndex(), and getPublishedMin(). The query classes also have a method to generate a URL representing the constructed query -- getQueryUrl(). Alternatively, the query string component of the URL can be retrieved used the getQueryString() method.

    • Feed classes - inheriting from Zend_Gdata_App_Feed. These also include other classes such as Zend_Gdata_Feed, Zend_Gdata_Spreadsheets_SpreadsheetFeed, and Zend_Gdata_Spreadsheets_ListFeed. These classes represent feeds of entries retrieved from services. They are primarily used to retrieve data returned from services.

    • Entry classes - inheriting from Zend_Gdata_App_Entry. These also include other classes such as Zend_Gdata_Entry, and Zend_Gdata_Spreadsheets_ListEntry. These classes represent entries retrieved from services or used for constructing data to send to services. In addition to being able to set the properties of an entry (such as the spreadsheet cell value), you can use an entry object to send update or delete requests to a service. For example, you can call $entry->save() to save changes made to an entry back to service from which the entry initiated, or $entry->delete() to delete an entry from the server.

    • Other Data model classes - inheriting from Zend_Gdata_App_Extension. These include classes such as Zend_Gdata_App_Extension_Title (representing the atom:title XML element), Zend_Gdata_Extension_When (representing the gd:when XML element used by the GData Event "Kind"), and Zend_Gdata_Extension_Cell (representing the gs:cell XML element used by Google Spreadsheets). These classes are used purely to store the data retrieved back from services and for constructing data to be sent to services. These include getters and setters such as setText() to set the child text node of an element, getText() to retrieve the text node of an element, getStartTime() to retrieve the start time attribute of a When element, and other similiar methods. The data model classes also include methods such as getDOM() to retrieve a DOM representation of the element and all children and transferFromDOM() to construct a data model representation of a DOM tree.

    34.1.2. Interacting with Google Services

    Google 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.

    34.1.3. Obtaining instances of Zend_Gdata classes

    The 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:

    $ss = new Zend_Gdata_Spreadsheets();

    // creates a Zend_Gdata_App_Spreadsheets_CellEntry
    $entry $ss->newCellEntry();

    // creates a Zend_Gdata_App_Spreadsheets_Extension_Cell
    $cell $ss->newCell();
    $cell->setText('My cell value');
    $entry->cell $cell;

    // ... $entry can then be used to send an update to a Google Spreadsheet

    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.

    34.1.4. 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.

    34.1.5. Dependencies

    Zend_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.

    34.1.6. Creating a new Gdata client

    Create 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.

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

    $gdata = new Zend_Gdata($client);

    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.

    $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.

    34.1.7. Common Query Parameters

    You 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.

    • The q parameter specifies a full-text query. The value of the parameter is a string.

      Set this parameter with the setQuery() function.

    • 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. NOTE: Only the output of the atom feed format can be processed using Zend_Gdata. The Zend_Http_Client could be used to retrieve feeds in other formats, using query URLs generated by the Zend_Gdata_Query class and its subclasses.

      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.

    $query = new Zend_Gdata_Query();
    $query->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.

    $query = new Zend_Gdata_Query();
    $query->maxResults 10;
    $query->maxResults;        // returns 10

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

    $query = new Zend_Gdata_Query();
    $query->maxResults 10;
    // ...get feed...

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

    34.1.8. Fetching a Feed

    Use 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.

    $gdata = new Zend_Gdata();
    $query = new Zend_Gdata_Query(
    $feed $gdata->getFeed($query);

    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.

    34.1.9. Working with Multi-page Feeds

    When 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:

    function getNextPage($feed) {
    $nextURL $feed->getLink('next');
        if (
    $nextURL !== null) {
        } else {

    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:

    $gdata = new Zend_Gdata();
    $query = new Zend_Gdata_Query(
    $feed $gdata->retrieveAllEntriesForFeed($gdata->getFeed($query));

    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().

    34.1.10. Working with Data in Feeds and Entries

    After 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:

    $client Zend_Gdata_ClientLogin::getHttpClient($user$pass$service);
    $gdata = new Zend_Gdata($client);
    $query = new Zend_Gdata_Query(
    $feed $gdata->getFeed($query);
    foreach (
    $feed as $entry) {
    // using the magic accessor
    echo 'Title: ' $entry->title->text;
    // using the defined accessors
    echo 'Content: ' $entry->getContent()->getText();

    34.1.11. Updating Entries

    After retrieving an entry, you can update that entry and save changes back to the server. Here's an example:

    $client Zend_Gdata_ClientLogin::getHttpClient($user$pass$service);
    $gdata = new Zend_Gdata($client);
    $query = new Zend_Gdata_Query(
    $feed $gdata->getFeed($query);
    foreach (
    $feed as $entry) {
    // update the title to append 'NEW'
    echo 'Old Title: ' $entry->title->text;
    $entry->title->text $entry->title->text ' NEW';

    // update the entry on the server
    $newEntry $entry->save();
    'New Title: ' $newEntry->title->text;

    34.1.12. Posting Entries to Google Servers

    The 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.

    $gdata = new Zend_Gdata($authenticatedHttpClient);

    $entry $gdata->newEntry();
    $entry->title $gdata->newTitle('Playing football at the park');
    $content =
    $gdata->newContent('We will visit the park and play football');
    $entry->content $content;

    $entryResult $gdata->insertEntry($entry,

    'The <id> of the resulting entry is: ' $entryResult->id->text;

    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.

    34.1.13. Deleting Entries on Google Servers

    Option 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.

    $gdata = new Zend_Gdata($authenticatedHttpClient);
    // a Google Data feed
    $feedUri = ...;
    $feed $gdata->getFeed($feedUri);
    foreach (
    $feed as $feedEntry) {
    // Option 1 - delete the entry directly
    // Option 2 - delete the entry by passing the edit URL to
        // $gdata->delete()
        // $gdata->delete($feedEntry->getEditLink()->href);

    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.

    digg delicious meneame google twitter technorati facebook