Zend_Gdata_Photos.xml 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.gdata.photos">
  4. <title>Using Picasa Web Albums</title>
  5. <para>
  6. Picasa Web Albums is a service which allows users to maintain albums of
  7. their own pictures, and browse the albums and pictures of others.
  8. The <acronym>API</acronym> offers a programmatic interface to this service, allowing
  9. users to add to, update, and remove from their albums, as well as
  10. providing the ability to tag and comment on photos.
  11. </para>
  12. <para>
  13. Access to public albums and photos is not restricted by account,
  14. however, a user must be logged in for non-read-only access.
  15. </para>
  16. <para>
  17. For more information on the <acronym>API</acronym>, including
  18. instructions for enabling <acronym>API</acronym> access, refer to the <ulink
  19. url="http://code.google.com/apis/picasaweb/overview.html">Picasa
  20. Web Albums Data <acronym>API</acronym> Overview</ulink>.
  21. </para>
  22. <note>
  23. <title>Authentication</title>
  24. <para>
  25. The <acronym>API</acronym> provides authentication via AuthSub (recommended)
  26. and ClientAuth. <acronym>HTTP</acronym> connections must be authenticated for write
  27. support, but non-authenticated connections have read-only access.
  28. </para>
  29. </note>
  30. <sect2 id="zend.gdata.photos.connecting">
  31. <title>Connecting To The Service</title>
  32. <para>
  33. The Picasa Web Albums <acronym>API</acronym>, like all GData <acronym>API</acronym>s, is
  34. based off of the Atom Publishing Protocol (APP), an <acronym>XML</acronym> based format
  35. for managing web-based resources. Traffic between a client and the servers occurs over
  36. <acronym>HTTP</acronym> and allows for both authenticated and unauthenticated
  37. connections.
  38. </para>
  39. <para>
  40. Before any transactions can occur, this connection needs to be made. Creating a
  41. connection to the Picasa servers involves two steps: creating an <acronym>HTTP</acronym>
  42. client and binding a <classname>Zend_Gdata_Photos</classname>
  43. service instance to that client.
  44. </para>
  45. <sect3 id="zend.gdata.photos.connecting.authentication">
  46. <title>Authentication</title>
  47. <para>
  48. The Google Picasa <acronym>API</acronym> allows access to both public and private
  49. photo feeds. Public feeds do not require authentication, but are read-only and offer
  50. reduced functionality. Private feeds offers the most complete functionality but
  51. requires an authenticated connection to the Picasa servers. There are three
  52. authentication schemes that are supported by Google Picasa :
  53. </para>
  54. <itemizedlist>
  55. <listitem>
  56. <para>
  57. <firstterm>ClientAuth</firstterm>
  58. provides direct username/password authentication to the
  59. Picasa servers. Since this scheme requires that users
  60. provide your application with their password, this
  61. authentication is only recommended when other
  62. authentication schemes are insufficient.
  63. </para>
  64. </listitem>
  65. <listitem>
  66. <para>
  67. <firstterm>AuthSub</firstterm>
  68. allows authentication to the Picasa servers via a
  69. Google proxy server. This provides the same level of
  70. convenience as ClientAuth but without the security
  71. risk, making this an ideal choice for web-based
  72. applications.
  73. </para>
  74. </listitem>
  75. </itemizedlist>
  76. <para>
  77. The
  78. <classname>Zend_Gdata</classname>
  79. library provides support for both authentication schemes.
  80. The rest of this chapter will assume that you are familiar the
  81. authentication schemes available and how to create an
  82. appropriate authenticated connection. For more information,
  83. please see section the
  84. <link linkend="zend.gdata.introduction.authentication">Authentication section</link>
  85. of this manual or the <ulink
  86. url="http://code.google.com/apis/gdata/auth.html">Authentication Overview in the
  87. Google Data <acronym>API</acronym> Developer's Guide</ulink>.
  88. </para>
  89. </sect3>
  90. <sect3 id="zend.gdata.photos.connecting.service">
  91. <title>Creating A Service Instance</title>
  92. <para>
  93. In order to interact with the servers, this library provides the
  94. <classname>Zend_Gdata_Photos</classname> service class. This class provides a common
  95. interface to the Google Data and Atom Publishing Protocol models and assists in
  96. marshaling requests to and from the servers.
  97. </para>
  98. <para>
  99. Once deciding on an authentication scheme, the next step is to create an instance of
  100. <classname>Zend_Gdata_Photos</classname>. The class constructor takes an instance of
  101. <classname>Zend_Http_Client</classname> as a single argument. This provides an
  102. interface for AuthSub and ClientAuth authentication, as both of these require
  103. creation of a special authenticated <acronym>HTTP</acronym> client. If no arguments
  104. are provided, an unauthenticated instance of <classname>Zend_Http_Client</classname>
  105. will be automatically created.
  106. </para>
  107. <para>
  108. The example below shows how to create a service class using ClientAuth
  109. authentication:
  110. </para>
  111. <programlisting language="php"><![CDATA[
  112. // Parameters for ClientAuth authentication
  113. $service = Zend_Gdata_Photos::AUTH_SERVICE_NAME;
  114. $user = "sample.user@gmail.com";
  115. $pass = "pa$$w0rd";
  116. // Create an authenticated HTTP client
  117. $client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service);
  118. // Create an instance of the service
  119. $service = new Zend_Gdata_Photos($client);
  120. ]]></programlisting>
  121. <para>
  122. A service instance using AuthSub can be created in a similar, though slightly more
  123. lengthy fashion:
  124. </para>
  125. <programlisting language="php"><![CDATA[
  126. session_start();
  127. /**
  128. * Returns the full URL of the current page, based upon env variables
  129. *
  130. * Env variables used:
  131. * $_SERVER['HTTPS'] = (on|off|)
  132. * $_SERVER['HTTP_HOST'] = value of the Host: header
  133. * $_SERVER['SERVER_PORT'] = port number (only used if not http/80,https/443)
  134. * $_SERVER['REQUEST_URI'] = the URI after the method of the HTTP request
  135. *
  136. * @return string Current URL
  137. */
  138. function getCurrentUrl()
  139. {
  140. global $_SERVER;
  141. /**
  142. * Filter php_self to avoid a security vulnerability.
  143. */
  144. $php_request_uri = htmlentities(substr($_SERVER['REQUEST_URI'], 0,
  145. strcspn($_SERVER['REQUEST_URI'], "\n\r")), ENT_QUOTES);
  146. if (isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on') {
  147. $protocol = 'https://';
  148. } else {
  149. $protocol = 'http://';
  150. }
  151. $host = $_SERVER['HTTP_HOST'];
  152. if ($_SERVER['SERVER_PORT'] != '' &&
  153. (($protocol == 'http://' && $_SERVER['SERVER_PORT'] != '80') ||
  154. ($protocol == 'https://' && $_SERVER['SERVER_PORT'] != '443'))) {
  155. $port = ':' . $_SERVER['SERVER_PORT'];
  156. } else {
  157. $port = '';
  158. }
  159. return $protocol . $host . $port . $php_request_uri;
  160. }
  161. /**
  162. * Returns the AuthSub URL which the user must visit to authenticate requests
  163. * from this application.
  164. *
  165. * Uses getCurrentUrl() to get the next URL which the user will be redirected
  166. * to after successfully authenticating with the Google service.
  167. *
  168. * @return string AuthSub URL
  169. */
  170. function getAuthSubUrl()
  171. {
  172. $next = getCurrentUrl();
  173. $scope = 'http://picasaweb.google.com/data';
  174. $secure = false;
  175. $session = true;
  176. return Zend_Gdata_AuthSub::getAuthSubTokenUri($next, $scope, $secure,
  177. $session);
  178. }
  179. /**
  180. * Returns a HTTP client object with the appropriate headers for communicating
  181. * with Google using AuthSub authentication.
  182. *
  183. * Uses the $_SESSION['sessionToken'] to store the AuthSub session token after
  184. * it is obtained. The single use token supplied in the URL when redirected
  185. * after the user succesfully authenticated to Google is retrieved from the
  186. * $_GET['token'] variable.
  187. *
  188. * @return Zend_Http_Client
  189. */
  190. function getAuthSubHttpClient()
  191. {
  192. global $_SESSION, $_GET;
  193. if (!isset($_SESSION['sessionToken']) && isset($_GET['token'])) {
  194. $_SESSION['sessionToken'] =
  195. Zend_Gdata_AuthSub::getAuthSubSessionToken($_GET['token']);
  196. }
  197. $client = Zend_Gdata_AuthSub::getHttpClient($_SESSION['sessionToken']);
  198. return $client;
  199. }
  200. /**
  201. * Create a new instance of the service, redirecting the user
  202. * to the AuthSub server if necessary.
  203. */
  204. $service = new Zend_Gdata_Photos(getAuthSubHttpClient());
  205. ]]></programlisting>
  206. <para>
  207. Finally, an unauthenticated server can be created for use with public feeds:
  208. </para>
  209. <programlisting language="php"><![CDATA[
  210. // Create an instance of the service using an unauthenticated HTTP client
  211. $service = new Zend_Gdata_Photos();
  212. ]]></programlisting>
  213. </sect3>
  214. </sect2>
  215. <sect2 id="zend.gdata.photos.queries">
  216. <title>Understanding and Constructing Queries</title>
  217. <para>
  218. The primary method to request data from the service is by constructing a query. There
  219. are query classes for each of the following types:
  220. </para>
  221. <itemizedlist>
  222. <listitem>
  223. <para>
  224. <firstterm>User</firstterm> is used to specify the user whose data is being
  225. searched for, and is specified as a username. if no user is provided, "default"
  226. will be used instead to indicate the currently authenticated user (if
  227. authenticated).
  228. </para>
  229. </listitem>
  230. <listitem>
  231. <para>
  232. <firstterm>Album</firstterm> is used to specify the album which is being
  233. searched for, and is specified as either an id, or an album name.
  234. </para>
  235. </listitem>
  236. <listitem>
  237. <para>
  238. <firstterm>Photo</firstterm> is used to specify the photo which is being
  239. searched for, and is specified as an id.
  240. </para>
  241. </listitem>
  242. </itemizedlist>
  243. <para>A new <classname>UserQuery</classname> can be constructed as followed:</para>
  244. <programlisting language="php"><![CDATA[
  245. $service = Zend_Gdata_Photos::AUTH_SERVICE_NAME;
  246. $client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service);
  247. $service = new Zend_Gdata_Photos($client);
  248. $query = new Zend_Gdata_Photos_UserQuery();
  249. $query->setUser("sample.user");
  250. ]]></programlisting>
  251. <para>
  252. for each query, a number of parameters limiting the search can be requested, or
  253. specified, with get(Parameter) and set(Parameter), respectively. They are as follows:
  254. </para>
  255. <itemizedlist>
  256. <listitem>
  257. <para>
  258. <firstterm>Projection</firstterm> sets the format of the data returned in the
  259. feed, as either "api" or "base". Normally, "api" is desired. The default is
  260. "api".
  261. </para>
  262. </listitem>
  263. <listitem>
  264. <para>
  265. <firstterm>Type</firstterm> sets the type of element to be returned, as either
  266. "feed" or "entry". The default is "feed".
  267. </para>
  268. </listitem>
  269. <listitem>
  270. <para>
  271. <firstterm>Access</firstterm> sets the visibility of items to be returned, as
  272. "all", "public", or "private". The default is "all". Non-public elements will
  273. only be returned if the query is searching for the authenticated user.
  274. </para>
  275. </listitem>
  276. <listitem>
  277. <para>
  278. <firstterm>Tag</firstterm> sets a tag filter for returned items. When a tag is
  279. set, only items tagged with this value will return.
  280. </para>
  281. </listitem>
  282. <listitem>
  283. <para>
  284. <firstterm>Kind</firstterm> sets the kind of elements to return. When kind is
  285. specified, only entries that match this value will be returned.
  286. </para>
  287. </listitem>
  288. <listitem>
  289. <para>
  290. <firstterm>ImgMax</firstterm> sets the maximum image size for entries returned.
  291. Only image entries smaller than this value will be returned.
  292. </para>
  293. </listitem>
  294. <listitem>
  295. <para>
  296. <firstterm>Thumbsize</firstterm> sets the thumbsize of entries that are
  297. returned. Any retrieved entry will have a thumbsize equal to this value.
  298. </para>
  299. </listitem>
  300. <listitem>
  301. <para>
  302. <firstterm>User</firstterm> sets the user whose data is being searched for. The
  303. default is "default".
  304. </para>
  305. </listitem>
  306. <listitem>
  307. <para>
  308. <firstterm>AlbumId</firstterm> sets the id of the album being searched for. This
  309. element only applies to album and photo queries. In the case of photo queries,
  310. this specifies the album that contains the requested photo. The album id is
  311. mutually exclusive with the album's name. Setting one unsets the other.
  312. </para>
  313. </listitem>
  314. <listitem>
  315. <para>
  316. <firstterm>AlbumName</firstterm> sets the name of the album being searched for.
  317. This element only applies to the album and photo queries. In the case of photo
  318. queries, this specifies the album that contains the requested photo. The album
  319. name is mutually exclusive with the album's id. Setting one unsets the other.
  320. </para>
  321. </listitem>
  322. <listitem>
  323. <para>
  324. <firstterm>PhotoId</firstterm> sets the id of the photo being searched for. This
  325. element only applies to photo queries.
  326. </para>
  327. </listitem>
  328. </itemizedlist>
  329. </sect2>
  330. <sect2 id="zend.gdata.photos.retrieval">
  331. <title>Retrieving Feeds And Entries</title>
  332. <para>
  333. The service has functions to retrieve a feed, or individual entries, for users, albums,
  334. and individual photos.
  335. </para>
  336. <sect3 id="zend.gdata.photos.user_retrieval">
  337. <title>Retrieving A User</title>
  338. <para>
  339. The service supports retrieving a user feed and list of the user's content. If the
  340. requested user is also the authenticated user, entries marked as
  341. "<emphasis>hidden</emphasis>" will also be returned.
  342. </para>
  343. <para>
  344. The user feed can be accessed by passing the username to the
  345. <methodname>getUserFeed()</methodname> method:
  346. </para>
  347. <programlisting language="php"><![CDATA[
  348. $service = Zend_Gdata_Photos::AUTH_SERVICE_NAME;
  349. $client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service);
  350. $service = new Zend_Gdata_Photos($client);
  351. try {
  352. $userFeed = $service->getUserFeed("sample.user");
  353. } catch (Zend_Gdata_App_Exception $e) {
  354. echo "Error: " . $e->getMessage();
  355. }
  356. ]]></programlisting>
  357. <para>Or, the feed can be accessed by constructing a query, first:</para>
  358. <programlisting language="php"><![CDATA[
  359. $service = Zend_Gdata_Photos::AUTH_SERVICE_NAME;
  360. $client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service);
  361. $service = new Zend_Gdata_Photos($client);
  362. $query = new Zend_Gdata_Photos_UserQuery();
  363. $query->setUser("sample.user");
  364. try {
  365. $userFeed = $service->getUserFeed(null, $query);
  366. } catch (Zend_Gdata_App_Exception $e) {
  367. echo "Error: " . $e->getMessage();
  368. }
  369. ]]></programlisting>
  370. <para>
  371. Constructing a query also provides the ability to request a user entry object:
  372. </para>
  373. <programlisting language="php"><![CDATA[
  374. $service = Zend_Gdata_Photos::AUTH_SERVICE_NAME;
  375. $client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service);
  376. $service = new Zend_Gdata_Photos($client);
  377. $query = new Zend_Gdata_Photos_UserQuery();
  378. $query->setUser("sample.user");
  379. $query->setType("entry");
  380. try {
  381. $userEntry = $service->getUserEntry($query);
  382. } catch (Zend_Gdata_App_Exception $e) {
  383. echo "Error: " . $e->getMessage();
  384. }
  385. ]]></programlisting>
  386. </sect3>
  387. <sect3 id="zend.gdata.photos.album_retrieval">
  388. <title>Retrieving An Album</title>
  389. <para>
  390. The service supports retrieving an album feed and a list of the album's content.
  391. </para>
  392. <para>
  393. The album feed is accessed by constructing a query object and passing it to
  394. <methodname>getAlbumFeed()</methodname>:
  395. </para>
  396. <programlisting language="php"><![CDATA[
  397. $service = Zend_Gdata_Photos::AUTH_SERVICE_NAME;
  398. $client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service);
  399. $service = new Zend_Gdata_Photos($client);
  400. $query = new Zend_Gdata_Photos_AlbumQuery();
  401. $query->setUser("sample.user");
  402. $query->setAlbumId("1");
  403. try {
  404. $albumFeed = $service->getAlbumFeed($query);
  405. } catch (Zend_Gdata_App_Exception $e) {
  406. echo "Error: " . $e->getMessage();
  407. }
  408. ]]></programlisting>
  409. <para>
  410. Alternatively, the query object can be given an album name with
  411. <methodname>setAlbumName()</methodname>. Setting the album name is mutually
  412. exclusive with setting the album id, and setting one will unset the other.
  413. </para>
  414. <para>
  415. Constructing a query also provides the ability to request an album entry object:
  416. </para>
  417. <programlisting language="php"><![CDATA[
  418. $service = Zend_Gdata_Photos::AUTH_SERVICE_NAME;
  419. $client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service);
  420. $service = new Zend_Gdata_Photos($client);
  421. $query = new Zend_Gdata_Photos_AlbumQuery();
  422. $query->setUser("sample.user");
  423. $query->setAlbumId("1");
  424. $query->setType("entry");
  425. try {
  426. $albumEntry = $service->getAlbumEntry($query);
  427. } catch (Zend_Gdata_App_Exception $e) {
  428. echo "Error: " . $e->getMessage();
  429. }
  430. ]]></programlisting>
  431. </sect3>
  432. <sect3 id="zend.gdata.photos.photo_retrieval">
  433. <title>Retrieving A Photo</title>
  434. <para>
  435. The service supports retrieving a photo feed and a list of associated comments and
  436. tags.
  437. </para>
  438. <para>
  439. The photo feed is accessed by constructing a query object and passing it to
  440. <methodname>getPhotoFeed()</methodname>:
  441. </para>
  442. <programlisting language="php"><![CDATA[
  443. $service = Zend_Gdata_Photos::AUTH_SERVICE_NAME;
  444. $client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service);
  445. $service = new Zend_Gdata_Photos($client);
  446. $query = new Zend_Gdata_Photos_PhotoQuery();
  447. $query->setUser("sample.user");
  448. $query->setAlbumId("1");
  449. $query->setPhotoId("100");
  450. try {
  451. $photoFeed = $service->getPhotoFeed($query);
  452. } catch (Zend_Gdata_App_Exception $e) {
  453. echo "Error: " . $e->getMessage();
  454. }
  455. ]]></programlisting>
  456. <para>
  457. Constructing a query also provides the ability to request a photo entry object:
  458. </para>
  459. <programlisting language="php"><![CDATA[
  460. $service = Zend_Gdata_Photos::AUTH_SERVICE_NAME;
  461. $client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service);
  462. $service = new Zend_Gdata_Photos($client);
  463. $query = new Zend_Gdata_Photos_PhotoQuery();
  464. $query->setUser("sample.user");
  465. $query->setAlbumId("1");
  466. $query->setPhotoId("100");
  467. $query->setType("entry");
  468. try {
  469. $photoEntry = $service->getPhotoEntry($query);
  470. } catch (Zend_Gdata_App_Exception $e) {
  471. echo "Error: " . $e->getMessage();
  472. }
  473. ]]></programlisting>
  474. </sect3>
  475. <sect3 id="zend.gdata.photos.comment_retrieval">
  476. <title>Retrieving A Comment</title>
  477. <para>
  478. The service supports retrieving comments from a feed of a different type. By setting
  479. a query to return a kind of "comment", a feed request can return comments associated
  480. with a specific user, album, or photo.
  481. </para>
  482. <para>
  483. Performing an action on each of the comments on a given photo can be accomplished
  484. as follows:
  485. </para>
  486. <programlisting language="php"><![CDATA[
  487. $service = Zend_Gdata_Photos::AUTH_SERVICE_NAME;
  488. $client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service);
  489. $service = new Zend_Gdata_Photos($client);
  490. $query = new Zend_Gdata_Photos_PhotoQuery();
  491. $query->setUser("sample.user");
  492. $query->setAlbumId("1");
  493. $query->setPhotoId("100");
  494. $query->setKind("comment");
  495. try {
  496. $photoFeed = $service->getPhotoFeed($query);
  497. foreach ($photoFeed as $entry) {
  498. if ($entry instanceof Zend_Gdata_Photos_CommentEntry) {
  499. // Do something with the comment
  500. }
  501. }
  502. } catch (Zend_Gdata_App_Exception $e) {
  503. echo "Error: " . $e->getMessage();
  504. }
  505. ]]></programlisting>
  506. </sect3>
  507. <sect3 id="zend.gdata.photos.tag_retrieval">
  508. <title>Retrieving A Tag</title>
  509. <para>
  510. The service supports retrieving tags from a feed of a different type. By setting a
  511. query to return a kind of "tag", a feed request can return tags associated with a
  512. specific photo.
  513. </para>
  514. <para>
  515. Performing an action on each of the tags on a given photo can be accomplished as
  516. follows:
  517. </para>
  518. <programlisting language="php"><![CDATA[
  519. $service = Zend_Gdata_Photos::AUTH_SERVICE_NAME;
  520. $client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service);
  521. $service = new Zend_Gdata_Photos($client);
  522. $query = new Zend_Gdata_Photos_PhotoQuery();
  523. $query->setUser("sample.user");
  524. $query->setAlbumId("1");
  525. $query->setPhotoId("100");
  526. $query->setKind("tag");
  527. try {
  528. $photoFeed = $service->getPhotoFeed($query);
  529. foreach ($photoFeed as $entry) {
  530. if ($entry instanceof Zend_Gdata_Photos_TagEntry) {
  531. // Do something with the tag
  532. }
  533. }
  534. } catch (Zend_Gdata_App_Exception $e) {
  535. echo "Error: " . $e->getMessage();
  536. }
  537. ]]></programlisting>
  538. </sect3>
  539. </sect2>
  540. <sect2 id="zend.gdata.photos.creation">
  541. <title>Creating Entries</title>
  542. <para>The service has functions to create albums, photos, comments, and tags.</para>
  543. <sect3 id="zend.gdata.photos.album_creation">
  544. <title>Creating An Album</title>
  545. <para>The service supports creating a new album for an authenticated user:</para>
  546. <programlisting language="php"><![CDATA[
  547. $service = Zend_Gdata_Photos::AUTH_SERVICE_NAME;
  548. $client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service);
  549. $service = new Zend_Gdata_Photos($client);
  550. $entry = new Zend_Gdata_Photos_AlbumEntry();
  551. $entry->setTitle($service->newTitle("test album"));
  552. $service->insertAlbumEntry($entry);
  553. ]]></programlisting>
  554. </sect3>
  555. <sect3 id="zend.gdata.photos.photo_creation">
  556. <title>Creating A Photo</title>
  557. <para>The service supports creating a new photo for an authenticated user:</para>
  558. <programlisting language="php"><![CDATA[
  559. $service = Zend_Gdata_Photos::AUTH_SERVICE_NAME;
  560. $client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service);
  561. $service = new Zend_Gdata_Photos($client);
  562. // $photo is the name of a file uploaded via an HTML form
  563. $fd = $service->newMediaFileSource($photo["tmp_name"]);
  564. $fd->setContentType($photo["type"]);
  565. $entry = new Zend_Gdata_Photos_PhotoEntry();
  566. $entry->setMediaSource($fd);
  567. $entry->setTitle($service->newTitle($photo["name"]));
  568. $albumQuery = new Zend_Gdata_Photos_AlbumQuery;
  569. $albumQuery->setUser("sample.user");
  570. $albumQuery->setAlbumId("1");
  571. $albumEntry = $service->getAlbumEntry($albumQuery);
  572. $service->insertPhotoEntry($entry, $albumEntry);
  573. ]]></programlisting>
  574. </sect3>
  575. <sect3 id="zend.gdata.photos.comment_creation">
  576. <title>Creating A Comment</title>
  577. <para>The service supports creating a new comment for a photo:</para>
  578. <programlisting language="php"><![CDATA[
  579. $service = Zend_Gdata_Photos::AUTH_SERVICE_NAME;
  580. $client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service);
  581. $service = new Zend_Gdata_Photos($client);
  582. $entry = new Zend_Gdata_Photos_CommentEntry();
  583. $entry->setTitle($service->newTitle("comment"));
  584. $entry->setContent($service->newContent("comment"));
  585. $photoQuery = new Zend_Gdata_Photos_PhotoQuery;
  586. $photoQuery->setUser("sample.user");
  587. $photoQuery->setAlbumId("1");
  588. $photoQuery->setPhotoId("100");
  589. $photoQuery->setType('entry');
  590. $photoEntry = $service->getPhotoEntry($photoQuery);
  591. $service->insertCommentEntry($entry, $photoEntry);
  592. ]]></programlisting>
  593. </sect3>
  594. <sect3 id="zend.gdata.photos.tag_creation">
  595. <title>Creating A Tag</title>
  596. <para>The service supports creating a new tag for a photo:</para>
  597. <programlisting language="php"><![CDATA[
  598. $service = Zend_Gdata_Photos::AUTH_SERVICE_NAME;
  599. $client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service);
  600. $service = new Zend_Gdata_Photos($client);
  601. $entry = new Zend_Gdata_Photos_TagEntry();
  602. $entry->setTitle($service->newTitle("tag"));
  603. $photoQuery = new Zend_Gdata_Photos_PhotoQuery;
  604. $photoQuery->setUser("sample.user");
  605. $photoQuery->setAlbumId("1");
  606. $photoQuery->setPhotoId("100");
  607. $photoQuery->setType('entry');
  608. $photoEntry = $service->getPhotoEntry($photoQuery);
  609. $service->insertTagEntry($entry, $photoEntry);
  610. ]]></programlisting>
  611. </sect3>
  612. </sect2>
  613. <sect2 id="zend.gdata.photos.deletion">
  614. <title>Deleting Entries</title>
  615. <para>The service has functions to delete albums, photos, comments, and tags.</para>
  616. <sect3 id="zend.gdata.photos.album_deletion">
  617. <title>Deleting An Album</title>
  618. <para>The service supports deleting an album for an authenticated user:</para>
  619. <programlisting language="php"><![CDATA[
  620. $service = Zend_Gdata_Photos::AUTH_SERVICE_NAME;
  621. $client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service);
  622. $service = new Zend_Gdata_Photos($client);
  623. $albumQuery = new Zend_Gdata_Photos_AlbumQuery;
  624. $albumQuery->setUser("sample.user");
  625. $albumQuery->setAlbumId("1");
  626. $albumQuery->setType('entry');
  627. $entry = $service->getAlbumEntry($albumQuery);
  628. $service->deleteAlbumEntry($entry, true);
  629. ]]></programlisting>
  630. </sect3>
  631. <sect3 id="zend.gdata.photos.photo_deletion">
  632. <title>Deleting A Photo</title>
  633. <para>The service supports deleting a photo for an authenticated user:</para>
  634. <programlisting language="php"><![CDATA[
  635. $service = Zend_Gdata_Photos::AUTH_SERVICE_NAME;
  636. $client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service);
  637. $service = new Zend_Gdata_Photos($client);
  638. $photoQuery = new Zend_Gdata_Photos_PhotoQuery;
  639. $photoQuery->setUser("sample.user");
  640. $photoQuery->setAlbumId("1");
  641. $photoQuery->setPhotoId("100");
  642. $photoQuery->setType('entry');
  643. $entry = $service->getPhotoEntry($photoQuery);
  644. $service->deletePhotoEntry($entry, true);
  645. ]]></programlisting>
  646. </sect3>
  647. <sect3 id="zend.gdata.photos.comment_deletion">
  648. <title>Deleting A Comment</title>
  649. <para>The service supports deleting a comment for an authenticated user:</para>
  650. <programlisting language="php"><![CDATA[
  651. $service = Zend_Gdata_Photos::AUTH_SERVICE_NAME;
  652. $client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service);
  653. $service = new Zend_Gdata_Photos($client);
  654. $photoQuery = new Zend_Gdata_Photos_PhotoQuery;
  655. $photoQuery->setUser("sample.user");
  656. $photoQuery->setAlbumId("1");
  657. $photoQuery->setPhotoId("100");
  658. $photoQuery->setType('entry');
  659. $path = $photoQuery->getQueryUrl() . '/commentid/' . "1000";
  660. $entry = $service->getCommentEntry($path);
  661. $service->deleteCommentEntry($entry, true);
  662. ]]></programlisting>
  663. </sect3>
  664. <sect3 id="zend.gdata.photos.tag_deletion">
  665. <title>Deleting A Tag</title>
  666. <para>The service supports deleting a tag for an authenticated user:</para>
  667. <programlisting language="php"><![CDATA[
  668. $service = Zend_Gdata_Photos::AUTH_SERVICE_NAME;
  669. $client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service);
  670. $service = new Zend_Gdata_Photos($client);
  671. $photoQuery = new Zend_Gdata_Photos_PhotoQuery;
  672. $photoQuery->setUser("sample.user");
  673. $photoQuery->setAlbumId("1");
  674. $photoQuery->setPhotoId("100");
  675. $photoQuery->setKind("tag");
  676. $query = $photoQuery->getQueryUrl();
  677. $photoFeed = $service->getPhotoFeed($query);
  678. foreach ($photoFeed as $entry) {
  679. if ($entry instanceof Zend_Gdata_Photos_TagEntry) {
  680. if ($entry->getContent() == $tagContent) {
  681. $tagEntry = $entry;
  682. }
  683. }
  684. }
  685. $service->deleteTagEntry($tagEntry, true);
  686. ]]></programlisting>
  687. </sect3>
  688. <sect3 id="zend.gdata.photos.optimistic_concurrenty">
  689. <title>Optimistic Concurrency (Notes On Deletion)</title>
  690. <para>
  691. GData feeds, including those of the Picasa Web Albums service, implement optimistic
  692. concurrency, a versioning system that prevents users from overwriting changes,
  693. inadvertently. When deleting a entry through the service class, if the entry has
  694. been modified since it was last fetched, an exception will be thrown, unless
  695. explicitly set otherwise (in which case the deletion is retried on the updated
  696. entry).
  697. </para>
  698. <para>
  699. An example of how to handle versioning during a deletion is shown by
  700. <methodname>deleteAlbumEntry()</methodname>:
  701. </para>
  702. <programlisting language="php"><![CDATA[
  703. // $album is the albumEntry to be deleted
  704. try {
  705. $this->delete($album);
  706. } catch (Zend_Gdata_App_HttpException $e) {
  707. if ($e->getMessage()->getStatus() === 409) {
  708. $entry =
  709. new Zend_Gdata_Photos_AlbumEntry($e->getMessage()->getBody());
  710. $this->delete($entry->getLink('edit')->href);
  711. } else {
  712. throw $e;
  713. }
  714. }
  715. ]]></programlisting>
  716. </sect3>
  717. </sect2>
  718. </sect1>