Zend_Gdata_Photos.xml 31 KB

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