Table of Contents
API-ul Google Data oferă o interfaţă pragmatică cu câteva dintre serviciile online de la Google. Aplicaţiile client trimit cereri şi date prin HTTP şi primesc rezultate. Componenta Zend_Gdata este o interfaţă PHP 5 pentru acces la Google Data din PHP.
Vedeţi http://code.google.com/apis/gdata/ pentru mai multe informaţii despre API-ul Google Data.
Serviciile care sunt accesibile prin Zend_Gdata sunt următoarele:
Google Base oferă o unealtă de căutare pentru produse online, slujbe, evenimente, anunţuri personale şi alte tipuri de date. Oamenii postează date şi le descriu cu atribute cu nume comune.
Google Blogger este un furnizor popular de servicii de tip „publicare prin apăsarea butonului” şi sindicare.
Google Calendar este o unealtă online pentru manipulat calendare folosite pentru programare şi colaborare.
Google CodeSearch vă permite să căutaţi în codul sursă public al multor proiecte.
Servicii care nu sunt suportate | |
---|---|
Google Data API oferă de asemenea Google Spreadsheets, dar Zend_Gdata nu oferă o clasă pentru Spreadsheets în această ediţie. Zend_Gdata nu oferă o interfaţă pentru alte servicii Google cum ar fi Căutare (Search), Gmail, Traducere (Translation sau Hărţi (Maps). Sunt suportate doar serviciile incluse în API-ul Google Data. |
Multe servicii Google Data necesită ca aplicaţiile să se autentifice pe serverul Google înainte de a accesa, salva sau şterge date private. Există două implementări de autentificare pentru Google Data: AuthSub and ClientLogin. Zend_Gdata oferă interfeţe sub forma unor clase pentru ambele metode.
Majoritatea celorlalte tipuri de cereri către serviciile Google Data nu necesită autentificare.
Zend_Gdata utilizează Zend_Feed şi Zend_Http_Client pentru a trimite cereri către google.com şi pentru a prelua rezultatele. Răspunsul la majoritatea cererilor Google Data este primit ca un obiect Zend_Feed în format Atom sau RSS.
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.
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.
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... ?>
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.
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.
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.