Previous Next

Using Picasa Web Albums

Picasa Web Albums is a service which allows users to maintain albums of their own pictures, and browse the albums and pictures of others. The API offers a programatic interface to this service, allowing users to add to, update, and remove from their albums, as well as providing the ability to tag and comment on photos.

Access to public albums and photos is not restricted by account, however, a user must be logged in for non-read-only access.

For more information on the API, including instructions for enabling API access, refer to the » Picasa Web Albums Data API Overview.

Note: Authentication

The API provides authentication via AuthSub (recommended) and ClientAuth. HTTP connections must be authenticated for write support, but non-authenticated connections have read-only access.

Connecting To The Service

The Picasa Web Albums API, like all GData APIs, is based off of the Atom Publishing Protocol (APP), an XML based format for managing web-based resources. Traffic between a client and the servers occurs over HTTP and allows for both authenticated and unauthenticated connections.

Before any transactions can occur, this connection needs to be made. Creating a connection to the Picasa servers involves two steps: creating an HTTP client and binding a Zend_Gdata_Photos service instance to that client.

Authentication

The Google Picasa API allows access to both public and private photo feeds. Public feeds do not require authentication, but are read-only and offer reduced functionality. Private feeds offers the most complete functionality but requires an authenticated connection to the Picasa servers. There are three authentication schemes that are supported by Google Picasa :

  • ClientAuth provides direct username/password authentication to the Picasa servers. Since this scheme requires that users provide your application with their password, this authentication is only recommended when other authentication schemes are insufficient.

  • AuthSub allows authentication to the Picasa servers via a Google proxy server. This provides the same level of convenience as ClientAuth but without the security risk, making this an ideal choice for web-based applications.

The Zend_Gdata library provides support for both authentication schemes. The rest of this chapter will assume that you are familiar the authentication schemes available and how to create an appropriate authenticated connection. For more information, please see section the Authentication section of this manual or the » Authentication Overview in the Google Data API Developer's Guide.

Creating A Service Instance

In order to interact with the servers, this library provides the Zend_Gdata_Photos service class. This class provides a common interface to the Google Data and Atom Publishing Protocol models and assists in marshaling requests to and from the servers.

Once deciding on an authentication scheme, the next step is to create an instance of Zend_Gdata_Photos. The class constructor takes an instance of Zend_Http_Client as a single argument. This provides an interface for AuthSub and ClientAuth authentication, as both of these require creation of a special authenticated HTTP client. If no arguments are provided, an unauthenticated instance of Zend_Http_Client will be automatically created.

The example below shows how to create a service class using ClientAuth authentication:



            
            

A service instance using AuthSub can be created in a similar, though slightly more lengthy fashion:



            
            

Finally, an unauthenticated server can be created for use with public feeds:



        

Understanding and Constructing Queries

The primary method to request data from the service is by constructing a query. There are query classes for each of the following types:

  • User is used to specify the user whose data is being searched for, and is specified as a username. If no user is provided, "default" will be used instead to indicate the currently authenticated user (if authenticated).

  • Album is used to specify the album which is being searched for, and is specified as either an id, or an album name.

  • Photo is used to specify the photo which is being searched for, and is specified as an id.

A new UserQuery can be constructed as followed:

setUser("sample.user");

For each query, a number of parameters limiting the search can be requested, or specified, with get(Parameter) and set(Parameter), respectively. They are as follows:

  • Projection sets the format of the data returned in the feed, as either "api" or "base". Normally, "api" is desired. The default is "api".

  • Type sets the type of element to be returned, as either "feed" or "entry". The default is "feed".

  • Access sets the visibility of items to be returned, as "all", "public", or "private". The default is "all". Non-public elements will only be returned if the query is searching for the authenticated user.

  • Tag sets a tag filter for returned items. When a tag is set, only items tagged with this value will return.

  • Kind sets the kind of elements to return. When kind is specified, only entries that match this value will be returned.

  • ImgMax sets the maximum image size for entries returned. Only image entries smaller than this value will be returned.

  • Thumbsize sets the thumbsize of entries that are returned. Any retrieved entry will have a thumbsize equal to this value.

  • User sets the user whose data is being searched for. The default is "default".

  • AlbumId sets the id of the album being searched for. This element only applies to album and photo queries. In the case of photo queries, this specifies the album that contains the requested photo. The album id is mutually exclusive with the album's name. Setting one unsets the other.

  • AlbumName sets the name of the album being searched for. This element only applies to the album and photo queries. In the case of photo queries, this specifies the album that contains the requested photo. The album name is mutually exclusive with the album's id. Setting one unsets the other.

  • PhotoId sets the id of the photo being searched for. This element only applies to photo queries.

Retrieving Feeds And Entries

The service has functions to retrieve a feed, or individual entries, for users, albums, and individual photos.

Retrieving A User

The service supports retrieving a user feed and list of the user's content. If the requested user is also the authenticated user, entries marked as "hidden" will also be returned.

The user feed can be accessed by passing the username to the getUserFeed method:

getUserFeed("sample.user");
    } catch (Zend_Gdata_App_Exception $e) {
        echo "Error: " . $e->getResponse();
    }

Or, the feed can be accessed by constructing a query, first:

setUser("sample.user");

    try {
        $userFeed = $service->getUserFeed(null, $query);
    } catch (Zend_Gdata_App_Exception $e) {
        echo "Error: " . $e->getResponse();
    }

Constructing a query also provides the ability to request a user entry object:

setUser("sample.user");
    $query->setType("entry");

    try {
        $userEntry = $service->getUserEntry($query);
    } catch (Zend_Gdata_App_Exception $e) {
        echo "Error: " . $e->getResponse();
    }

Retrieving An Album

The service supports retrieving an album feed and a list of the album's content.

The album feed is accessed by constructing a query object and passing it to getAlbumFeed:

setUser("sample.user");
    $query->setAlbumId("1");

    try {
        $albumFeed = $service->getAlbumFeed($query);
    } catch (Zend_Gdata_App_Exception $e) {
        echo "Error: " . $e->getResponse();
    }

Alternatively, the query object can be given an album name with setAlbumName. Setting the album name is mutually exclusive with setting the album id, and setting one will unset the other.

Constructing a query also provides the ability to request an album entry object:

setUser("sample.user");
    $query->setAlbumId("1");
    $query->setType("entry");

    try {
        $albumEntry = $service->getAlbumEntry($query);
    } catch (Zend_Gdata_App_Exception $e) {
        echo "Error: " . $e->getResponse();
    }

Retrieving A Photo

The service supports retrieving a photo feed and a list of associated comments and tags.

The photo feed is accessed by constructing a query object and passing it to getPhotoFeed:

setUser("sample.user");
    $query->setAlbumId("1");
    $query->setPhotoId("100");

    try {
        $photoFeed = $service->getPhotoFeed($query);
    } catch (Zend_Gdata_App_Exception $e) {
        echo "Error: " . $e->getResponse();
    }

Constructing a query also provides the ability to request a photo entry object:

setUser("sample.user");
    $query->setAlbumId("1");
    $query->setPhotoId("100");
    $query->setType("entry");

    try {
        $photoEntry = $service->getPhotoEntry($query);
    } catch (Zend_Gdata_App_Exception $e) {
        echo "Error: " . $e->getResponse();
    }

Retrieving A Comment

The service supports retrieving comments from a feed of a different type. By setting a query to return a kind of "comment", a feed request can return comments associated with a specific user, album, or photo.

Performing an action on each of the comments on a given photo can be accomplished as follows:

setUser("sample.user");
    $query->setAlbumId("1");
    $query->setPhotoId("100");
    $query->setKind("comment");

    try {
        $photoFeed = $service->getPhotoFeed($query);
    
        foreach ($photoFeed as $entry) {
            if ($entry instanceof Zend_Gdata_Photos_CommentEntry) {
                // Do something with the comment
            }
        }
    } catch (Zend_Gdata_App_Exception $e) {
        echo "Error: " . $e->getResponse();
    }

Retrieving A Tag

The service supports retrieving tags from a feed of a different type. By setting a query to return a kind of "tag", a feed request can return tags associated with a specific photo.

Performing an action on each of the tags on a given photo can be accomplished as follows:

setUser("sample.user");
    $query->setAlbumId("1");
    $query->setPhotoId("100");
    $query->setKind("tag");

    try {
        $photoFeed = $service->getPhotoFeed($query);
    
        foreach ($photoFeed as $entry) {
            if ($entry instanceof Zend_Gdata_Photos_TagEntry) {
                // Do something with the tag
            }
        }
    } catch (Zend_Gdata_App_Exception $e) {
        echo "Error: " . $e->getResponse();
    }

Creating Entries

The service has functions to create albums, photos, comments, and tags.

Creating An Album

The service supports creating a new album for an authenticated user:

setTitle($service->newTitle("test album"));

    $service->insertAlbumEntry($entry);

Creating A Photo

The service supports creating a new photo for an authenticated user:

newMediaFileSource($photo["tmp_name"]);
    $fd->setContentType($photo["type"]);

    $entry = new Zend_Gdata_Photos_PhotoEntry();
    $entry->setMediaSource($fd);
    $entry->setTitle($service->newTitle($photo["name"]));

    $albumQuery = new Zend_Gdata_Photos_AlbumQuery;
    $albumQuery->setUser("sample.user");
    $albumQuery->setAlbumId("1");

    $albumEntry = $service->getAlbumEntry($albumQuery);

    $service->insertPhotoEntry($entry, $albumEntry);

Creating A Comment

The service supports creating a new comment for a photo:

setTitle($service->newTitle("comment"));
    $entry->setContent($service->newContent("comment"));

    $photoQuery = new Zend_Gdata_Photos_PhotoQuery;
    $photoQuery->setUser("sample.user");
    $photoQuery->setAlbumId("1");
    $photoQuery->setPhotoId("100");
    $photoQuery->setType('entry');

    $photoEntry = $service->getPhotoEntry($photoQuery);

    $service->insertCommentEntry($entry, $photoEntry);

Creating A Tag

The service supports creating a new tag for a photo:

setTitle($service->newTitle("tag"));

    $photoQuery = new Zend_Gdata_Photos_PhotoQuery;
    $photoQuery->setUser("sample.user");
    $photoQuery->setAlbumId("1");
    $photoQuery->setPhotoId("100");
    $photoQuery->setType('entry');

    $photoEntry = $service->getPhotoEntry($photoQuery);

    $service->insertTagEntry($entry, $photoEntry);

Deleting Entries

The service has functions to delete albums, photos, comments, and tags.

Deleting An Album

The service supports deleting an album for an authenticated user:

setUser("sample.user");
    $albumQuery->setAlbumId("1");
    $albumQuery->setType('entry');

    $entry = $service->getAlbumEntry($albumQuery);

    $service->deleteAlbumEntry($entry, true);

Deleting A Photo

The service supports deleting a photo for an authenticated user:

setUser("sample.user");
    $photoQuery->setAlbumId("1");
    $photoQuery->setPhotoId("100");
    $photoQuery->setType('entry');

    $entry = $service->getPhotoEntry($photoQuery);

    $service->deletePhotoEntry($entry, true);

Deleting A Comment

The service supports deleting a comment for an authenticated user:

setUser("sample.user");
    $photoQuery->setAlbumId("1");
    $photoQuery->setPhotoId("100");
    $photoQuery->setType('entry');

    $path = $photoQuery->getQueryUrl() . '/commentid/' . "1000";

    $entry = $service->getCommentEntry($path);

    $service->deleteCommentEntry($entry, true);

Deleting A Tag

The service supports deleting a tag for an authenticated user:

setUser("sample.user");
    $photoQuery->setAlbumId("1");
    $photoQuery->setPhotoId("100");
    $photoQuery->setKind("tag");
    $query = $photoQuery->getQueryUrl();

    $photoFeed = $service->getPhotoFeed($query);

    foreach ($photoFeed as $entry) {
        if ($entry instanceof Zend_Gdata_Photos_TagEntry) {
            if ($entry->getContent() == $tagContent) {
                $tagEntry = $entry;
            }
        }
    }

    $service->deleteTagEntry($tagEntry, true);

Optimistic Concurrency (Notes On Deletion)

GData feeds, including those of the Picasa Web Albums service, implement optimistic concurrency, a versioning system that prevents users from overwriting changes, inadvertently. When deleting a entry through the service class, if the entry has been modified since it was last fetched, an exception will be thrown, unless explicitly set otherwise (in which case the deletion is retried on the updated entry).

An example of how to handle versioning during a deletion is shown by deleteAlbumEntry:

delete($album);
    } catch (Zend_Gdata_App_HttpException $e) {
        if ($e->getResponse->getStatus() === 409) {
            $entry = new Zend_Gdata_Photos_AlbumEntry($e->getResponse()->getBody());
            $this->delete($entry->getLink('edit')->href);
        } else {
            throw $e;
        }
    }
Previous Next
Introduction to Zend Framework
Overview
Installation
Zend_Acl
Introduction
Refining Access Controls
Advanced Use
Zend_Auth
Introduction
Database Table Authentication
Digest Authentication
HTTP Authentication Adapter
LDAP Authentication
Open ID Authentication
Zend_Cache
Introduction
The theory of caching
Zend_Cache frontends
Zend_Cache backends
Zend_Captcha
Introduction
Captcha Operation
Captcha Adapters
Zend_Config
Introduction
Theory of Operation
Zend_Config_Ini
Zend_Config_Xml
Zend_Console_Getopt
Introduction to Getopt
Declaring Getopt Rules
Fetching Options and Arguments
Configuring Zend_Console_Getopt
Zend_Controller
Zend_Controller Quick Start
Zend_Controller Basics
The Front Controller
The Request Object
The Standard Router: Zend_Controller_Router_Rewrite
The Dispatcher
Action Controllers
Action Helpers
The Response Object
Plugins
Using a Conventional Modular Directory Structure
MVC Exceptions
Migrating from Previous Versions
Zend_Currency
Introduction to Zend_Currency
How to work with currencies
Migrating from Previous Versions
Zend_Date
Introduction
Theory of Operation
Basic Methods
Zend_Date API Overview
Creation of dates
Constants for General Date Functions
Working examples
Zend_Db
Zend_Db_Adapter
Zend_Db_Statement
Zend_Db_Profiler
Zend_Db_Select
Zend_Db_Table
Zend_Db_Table_Row
Zend_Db_Table_Rowset
Zend_Db_Table Relationships
Zend_Debug
Dumping Variables
Zend_Dojo
Introduction
Zend_Dojo_Data: dojo.data Envelopes
Dojo View Helpers
Dojo Form Elements and Decorators
Zend_Dom
Introduction
Zend_Dom_Query
Zend_Exception
Using Exceptions
Zend_Feed
Introduction
Importing Feeds
Retrieving Feeds from Web Pages
Consuming an RSS Feed
Consuming an Atom Feed
Consuming a Single Atom Entry
Modifying Feed and Entry structures
Custom Feed and Entry Classes
Zend_File
Zend_File_Transfer
Validators for Zend_File_Transfer
Zend_Filter
Introduction
Standard Filter Classes
Filter Chains
Writing Filters
Zend_Filter_Input
Zend_Filter_Inflector
Zend_Form
Zend_Form
Zend_Form Quick Start
Creating Form Elements Using Zend_Form_Element
Creating Forms Using Zend_Form
Creating Custom Form Markup Using Zend_Form_Decorator
Standard Form Elements Shipped With Zend Framework
Standard Form Decorators Shipped With Zend Framework
Internationalization of Zend_Form
Advanced Zend_Form Usage
Zend_Gdata
Introduction to Gdata
Authenticating with AuthSub
Authenticating with ClientLogin
Using Google Calendar
Using Google Documents List Data API
Using Google Spreadsheets
Using Google Apps Provisioning
Using Google Base
Using the YouTube Data API
Using Picasa Web Albums
Catching Gdata Exceptions
Zend_Http
Zend_Http_Client - Introduction
Zend_Http_Client - Advanced Usage
Zend_Http_Client - Connection Adapters
Zend_Http_Cookie and Zend_Http_CookieJar
Zend_Http_Response
Zend_InfoCard
Introduction
Zend_Json
Introduction
Basic Usage
JSON Objects
XML to JSON conversion
Zend_Json_Server - JSON-RPC server
Zend_Layout
Introduction
Zend_Layout Quick Start
Zend_Layout Configuration Options
Zend_Layout Advanced Usage
Zend_Ldap
Introduction
Zend_Loader
Loading Files and Classes Dynamically
Loading Plugins
Zend_Locale
Introduction
Using Zend_Locale
Normalization and Localization
Working with Dates and Times
Supported Languages for Locales
Supported Regions for Locales
Zend_Log
Overview
Writers
Formatters
Filters
Zend_Mail
Introduction
Sending via SMTP
Sending Multiple Mails per SMTP Connection
Using Different Transports
HTML E-Mail
Attachments
Adding Recipients
Controlling the MIME Boundary
Additional Headers
Character Sets
Encoding
SMTP Authentication
Securing SMTP Transport
Reading Mail Messages
Zend_Measure
Introduction
Creation of Measurements
Outputting measurements
Manipulating Measurements
Types of measurements
Zend_Memory
Overview
Memory Manager
Memory Objects
Zend_Mime
Zend_Mime
Zend_Mime_Message
Zend_Mime_Part
Zend_OpenId
Introduction
Zend_OpenId_Consumer Basics
Zend_OpenId_Provider
Zend_Paginator
Introduction
Usage
Configuration
Advanced usage
Zend_Pdf
Introduction.
Creating and loading PDF documents.
Save changes to the PDF document.
Document pages.
Drawing.
Document Info and Metadata.
Zend_Pdf module usage example.
Zend_Registry
Using the Registry
Zend_Rest
Introduction
Zend_Rest_Client
Zend_Rest_Server
Zend_Search_Lucene
Overview
Building Indexes
Searching an Index
Query Language
Query Construction API
Character Set
Extensibility
Interoperating with Java Lucene
Advanced
Best Practices
Zend_Server
Introduction
Zend_Server_Reflection
Zend_Service
Introduction
Zend_Service_Akismet
Zend_Service_Amazon
Zend_Service_Audioscrobbler
Zend_Service_Delicious
Zend_Service_Flickr
Zend_Service_Nirvanix
Zend_Service_ReCaptcha
Zend_Service_Simpy
Introduction
Zend_Service_StrikeIron
Zend_Service_StrikeIron: Bundled Services
Zend_Service_StrikeIron: Advanced Uses
Zend_Service_Technorati
Zend_Service_Yahoo
Zend_Session
Introduction
Basic Usage
Advanced Usage
Global Session Management
Zend_Session_SaveHandler_DbTable
Zend_Soap
Zend_Soap_Server
Zend_Soap_Client
WSDL Accessor
AutoDiscovery. Introduction
Class autodiscovering.
Functions autodiscovering.
Autodiscovering. Datatypes.
Zend_Test
Introduction
Zend_Test_PHPUnit
Zend_Text
Zend_Text_Figlet
Zend_TimeSync
Introduction
Working with Zend_TimeSync
Zend_Translate
Introduction
Adapters for Zend_Translate
Using Translation Adapters
Zend_Uri
Zend_Uri
Zend_Validate
Introduction
Standard Validation Classes
Validator Chains
Writing Validators
Zend_Version
Reading the Zend Framework Version
Zend_View
Introduction
Controller Scripts
View Scripts
View Helpers
Zend_View_Abstract
Zend_Wildfire
Zend_Wildfire
Zend_XmlRpc
Introduction
Zend_XmlRpc_Client
Zend_XmlRpc_Server
Zend Framework Requirements
PHP Version
PHP Extensions
Zend Framework Components
Zend Framework Dependencies
Zend Framework Coding Standard for PHP
Overview
PHP File Formatting
Naming Conventions
Coding Style
Copyright Information