Zend_Service_Delicious.xml 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.service.delicious">
  4. <title>Zend_Service_Delicious</title>
  5. <sect2 id="zend.service.delicious.introduction">
  6. <title>Introduction</title>
  7. <para>
  8. <classname>Zend_Service_Delicious</classname> is simple <acronym>API</acronym> for using
  9. <ulink url="http://del.icio.us">del.icio.us</ulink> <acronym>XML</acronym> and
  10. <acronym>JSON</acronym> web services. This component gives you read-write access to
  11. posts at del.icio.us if you provide credentials. It also allows read-only access to
  12. public data of all users.
  13. </para>
  14. <example id="zend.service.delicious.introduction.getAllPosts">
  15. <title>Get all posts</title>
  16. <programlisting language="php"><![CDATA[
  17. $delicious = new Zend_Service_Delicious('username', 'password');
  18. $posts = $delicious->getAllPosts();
  19. foreach ($posts as $post) {
  20. echo "--\n";
  21. echo "Title: {$post->getTitle()}\n";
  22. echo "Url: {$post->getUrl()}\n";
  23. }
  24. ]]></programlisting>
  25. </example>
  26. </sect2>
  27. <sect2 id="zend.service.delicious.retrieving_posts">
  28. <title>Retrieving posts</title>
  29. <para>
  30. <classname>Zend_Service_Delicious</classname> provides three methods for retrieving
  31. posts: <methodname>getPosts()</methodname>, <methodname>getRecentPosts()</methodname>
  32. and <methodname>getAllPosts()</methodname>. All of these methods return an instance of
  33. <classname>Zend_Service_Delicious_PostList</classname>, which holds all retrieved posts.
  34. </para>
  35. <programlisting language="php"><![CDATA[
  36. /**
  37. * Get posts matching the arguments. If no date or url is given,
  38. * most recent date will be used.
  39. *
  40. * @param string $tag Optional filtering by tag
  41. * @param Zend_Date $dt Optional filtering by date
  42. * @param string $url Optional filtering by url
  43. * @return Zend_Service_Delicious_PostList
  44. */
  45. public function getPosts($tag = null, $dt = null, $url = null);
  46. /**
  47. * Get recent posts
  48. *
  49. * @param string $tag Optional filtering by tag
  50. * @param string $count Maximal number of posts to be returned
  51. * (default 15)
  52. * @return Zend_Service_Delicious_PostList
  53. */
  54. public function getRecentPosts($tag = null, $count = 15);
  55. /**
  56. * Get all posts
  57. *
  58. * @param string $tag Optional filtering by tag
  59. * @return Zend_Service_Delicious_PostList
  60. */
  61. public function getAllPosts($tag = null);
  62. ]]></programlisting>
  63. </sect2>
  64. <sect2 id="zend.service.delicious.postlist">
  65. <title>Zend_Service_Delicious_PostList</title>
  66. <para>
  67. Instances of this class are returned by the <methodname>getPosts()</methodname>,
  68. <methodname>getAllPosts()</methodname>, <methodname>getRecentPosts()</methodname>, and
  69. <methodname>getUserPosts()</methodname> methods of
  70. <classname>Zend_Service_Delicious</classname>.
  71. </para>
  72. <para>
  73. For easier data access this class implements the <code>Countable</code>,
  74. <code>Iterator</code>, and <code>ArrayAccess</code> interfaces.
  75. </para>
  76. <example id="zend.service.delicious.postlist.accessing_post_lists">
  77. <title>Accessing post lists</title>
  78. <programlisting language="php"><![CDATA[
  79. $delicious = new Zend_Service_Delicious('username', 'password');
  80. $posts = $delicious->getAllPosts();
  81. // count posts
  82. echo count($posts);
  83. // iterate over posts
  84. foreach ($posts as $post) {
  85. echo "--\n";
  86. echo "Title: {$post->getTitle()}\n";
  87. echo "Url: {$post->getUrl()}\n";
  88. }
  89. // get post using array access
  90. echo $posts[0]->getTitle();
  91. ]]></programlisting>
  92. </example>
  93. <note>
  94. <para>
  95. The <methodname>ArrayAccess::offsetSet()</methodname> and
  96. <methodname>ArrayAccess::offsetUnset()</methodname> methods throw exceptions in this
  97. implementation. Thus, code like <code>unset($posts[0]);</code> and
  98. <code>$posts[0] = 'A';</code> will throw exceptions because these properties are
  99. read-only.
  100. </para>
  101. </note>
  102. <para>
  103. Post list objects have two built-in filtering capabilities. Post lists may be filtered
  104. by tags and by <acronym>URL</acronym>.
  105. </para>
  106. <example id="zend.service.delicious.postlist.example.withTags">
  107. <title>Filtering a Post List with Specific Tags</title>
  108. <para>
  109. Posts may be filtered by specific tags using <methodname>withTags()</methodname>. As
  110. a convenience, <methodname>withTag()</methodname> is also provided for when only a
  111. single tag needs to be specified.
  112. </para>
  113. <programlisting language="php"><![CDATA[
  114. $delicious = new Zend_Service_Delicious('username', 'password');
  115. $posts = $delicious->getAllPosts();
  116. // Print posts having "php" and "zend" tags
  117. foreach ($posts->withTags(array('php', 'zend')) as $post) {
  118. echo "Title: {$post->getTitle()}\n";
  119. echo "Url: {$post->getUrl()}\n";
  120. }
  121. ]]></programlisting>
  122. </example>
  123. <example id="zend.service.delicious.postlist.example.byUrl">
  124. <title>Filtering a Post List by URL</title>
  125. <para>
  126. Posts may be filtered by <acronym>URL</acronym> matching a specified regular
  127. expression using the <methodname>withUrl()</methodname> method:
  128. </para>
  129. <programlisting language="php"><![CDATA[
  130. $delicious = new Zend_Service_Delicious('username', 'password');
  131. $posts = $delicious->getAllPosts();
  132. // Print posts having "help" in the URL
  133. foreach ($posts->withUrl('/help/') as $post) {
  134. echo "Title: {$post->getTitle()}\n";
  135. echo "Url: {$post->getUrl()}\n";
  136. }
  137. ]]></programlisting>
  138. </example>
  139. </sect2>
  140. <sect2 id="zend.service.delicious.editing_posts">
  141. <title>Editing posts</title>
  142. <example id="zend.service.delicious.editing_posts.post_editing">
  143. <title>Post editing</title>
  144. <programlisting language="php"><![CDATA[
  145. $delicious = new Zend_Service_Delicious('username', 'password');
  146. $posts = $delicious->getPosts();
  147. // set title
  148. $posts[0]->setTitle('New title');
  149. // save changes
  150. $posts[0]->save();
  151. ]]></programlisting>
  152. </example>
  153. <example id="zend.service.delicious.editing_posts.method_call_chaining">
  154. <title>Method call chaining</title>
  155. <para>
  156. Every setter method returns the post object so that you can chain method calls using
  157. a fluent interface.
  158. </para>
  159. <programlisting language="php"><![CDATA[
  160. $delicious = new Zend_Service_Delicious('username', 'password');
  161. $posts = $delicious->getPosts();
  162. $posts[0]->setTitle('New title')
  163. ->setNotes('New notes')
  164. ->save();
  165. ]]></programlisting>
  166. </example>
  167. </sect2>
  168. <sect2 id="zend.service.delicious.deleting_posts">
  169. <title>Deleting posts</title>
  170. <para>
  171. There are two ways to delete a post, by specifying the post <acronym>URL</acronym> or by
  172. calling the <methodname>delete()</methodname> method upon a post object.
  173. </para>
  174. <example id="zend.service.delicious.deleting_posts.deleting_posts">
  175. <title>Deleting posts</title>
  176. <programlisting language="php"><![CDATA[
  177. $delicious = new Zend_Service_Delicious('username', 'password');
  178. // by specifying URL
  179. $delicious->deletePost('http://framework.zend.com');
  180. // or by calling the method upon a post object
  181. $posts = $delicious->getPosts();
  182. $posts[0]->delete();
  183. // another way of using deletePost()
  184. $delicious->deletePost($posts[0]->getUrl());
  185. ]]></programlisting>
  186. </example>
  187. </sect2>
  188. <sect2 id="zend.service.delicious.adding_posts">
  189. <title>Adding new posts</title>
  190. <para>
  191. To add a post you first need to call the <methodname>createNewPost()</methodname>
  192. method, which returns a <classname>Zend_Service_Delicious_Post</classname> object. When
  193. you edit the post, you need to save it to the del.icio.us database by calling the
  194. <methodname>save()</methodname> method.
  195. </para>
  196. <example id="zend.service.delicious.adding_posts.adding_a_post">
  197. <title>Adding a post</title>
  198. <programlisting language="php"><![CDATA[
  199. $delicious = new Zend_Service_Delicious('username', 'password');
  200. // create a new post and save it (with method call chaining)
  201. $delicious->createNewPost('Zend Framework', 'http://framework.zend.com')
  202. ->setNotes('Zend Framework Homepage')
  203. ->save();
  204. // create a new post and save it (without method call chaining)
  205. $newPost = $delicious->createNewPost('Zend Framework',
  206. 'http://framework.zend.com');
  207. $newPost->setNotes('Zend Framework Homepage');
  208. $newPost->save();
  209. ]]></programlisting>
  210. </example>
  211. </sect2>
  212. <sect2 id="zend.service.delicious.tags">
  213. <title>Tags</title>
  214. <example id="zend.service.delicious.tags.tags">
  215. <title>Tags</title>
  216. <programlisting language="php"><![CDATA[
  217. $delicious = new Zend_Service_Delicious('username', 'password');
  218. // get all tags
  219. print_r($delicious->getTags());
  220. // rename tag ZF to zendFramework
  221. $delicious->renameTag('ZF', 'zendFramework');
  222. ]]></programlisting>
  223. </example>
  224. </sect2>
  225. <sect2 id="zend.service.delicious.bundles">
  226. <title>Bundles</title>
  227. <example id="zend.service.delicious.bundles.example">
  228. <title>Bundles</title>
  229. <programlisting language="php"><![CDATA[
  230. $delicious = new Zend_Service_Delicious('username', 'password');
  231. // get all bundles
  232. print_r($delicious->getBundles());
  233. // delete bundle someBundle
  234. $delicious->deleteBundle('someBundle');
  235. // add bundle
  236. $delicious->addBundle('newBundle', array('tag1', 'tag2'));
  237. ]]></programlisting>
  238. </example>
  239. </sect2>
  240. <sect2 id="zend.service.delicious.public_data">
  241. <title>Public data</title>
  242. <para>
  243. The del.icio.us web <acronym>API</acronym> allows access to the public data of all
  244. users.
  245. </para>
  246. <table id="zend.service.delicious.public_data.functions_for_retrieving_public_data">
  247. <title>Methods for retrieving public data</title>
  248. <tgroup cols="3">
  249. <thead>
  250. <row>
  251. <entry>Name</entry>
  252. <entry>Description</entry>
  253. <entry>Return type</entry>
  254. </row>
  255. </thead>
  256. <tbody>
  257. <row>
  258. <entry><methodname>getUserFans()</methodname></entry>
  259. <entry>Retrieves fans of a user</entry>
  260. <entry>Array</entry>
  261. </row>
  262. <row>
  263. <entry><methodname>getUserNetwork()</methodname></entry>
  264. <entry>Retrieves network of a user</entry>
  265. <entry>Array</entry>
  266. </row>
  267. <row>
  268. <entry><methodname>getUserPosts()</methodname></entry>
  269. <entry>Retrieves posts of a user</entry>
  270. <entry>Zend_Service_Delicious_PostList</entry>
  271. </row>
  272. <row>
  273. <entry><methodname>getUserTags()</methodname></entry>
  274. <entry>Retrieves tags of a user</entry>
  275. <entry>Array</entry>
  276. </row>
  277. </tbody>
  278. </tgroup>
  279. </table>
  280. <note>
  281. <para>
  282. When using only these methods, a username and password combination is not required
  283. when constructing a new <classname>Zend_Service_Delicious</classname> object.
  284. </para>
  285. </note>
  286. <example id="zend.service.delicious.public_data.retrieving_public_data">
  287. <title>Retrieving public data</title>
  288. <programlisting language="php"><![CDATA[
  289. // username and password are not required
  290. $delicious = new Zend_Service_Delicious();
  291. // get fans of user someUser
  292. print_r($delicious->getUserFans('someUser'));
  293. // get network of user someUser
  294. print_r($delicious->getUserNetwork('someUser'));
  295. // get tags of user someUser
  296. print_r($delicious->getUserTags('someUser'));
  297. ]]></programlisting>
  298. </example>
  299. <sect3 id="zend.service.delicious.public_data.posts">
  300. <title>Public posts</title>
  301. <para>
  302. When retrieving public posts with the <methodname>getUserPosts()</methodname>
  303. method, a <classname>Zend_Service_Delicious_PostList</classname> object is returned,
  304. and it contains <classname>Zend_Service_Delicious_SimplePost</classname> objects,
  305. which contain basic information about the posts, including <acronym>URL</acronym>,
  306. title, notes, and tags.
  307. </para>
  308. <table id="zend.service.delicious.public_data.posts.SimplePost_methods">
  309. <title>Methods of the Zend_Service_Delicious_SimplePost class</title>
  310. <tgroup cols="3">
  311. <thead>
  312. <row>
  313. <entry>Name</entry>
  314. <entry>Description</entry>
  315. <entry>Return type</entry>
  316. </row>
  317. </thead>
  318. <tbody>
  319. <row>
  320. <entry><methodname>getNotes()</methodname></entry>
  321. <entry>Returns notes of a post</entry>
  322. <entry>String</entry>
  323. </row>
  324. <row>
  325. <entry><methodname>getTags()</methodname></entry>
  326. <entry>Returns tags of a post</entry>
  327. <entry>Array</entry>
  328. </row>
  329. <row>
  330. <entry><methodname>getTitle()</methodname></entry>
  331. <entry>Returns title of a post</entry>
  332. <entry>String</entry>
  333. </row>
  334. <row>
  335. <entry><methodname>getUrl()</methodname></entry>
  336. <entry>Returns <acronym>URL</acronym> of a post</entry>
  337. <entry>String</entry>
  338. </row>
  339. </tbody>
  340. </tgroup>
  341. </table>
  342. </sect3>
  343. </sect2>
  344. <sect2 id="zend.service.delicious.httpclient">
  345. <title>HTTP client</title>
  346. <para>
  347. <classname>Zend_Service_Delicious</classname> uses
  348. <classname>Zend_Rest_Client</classname> for making <acronym>HTTP</acronym> requests to
  349. the del.icio.us web service. To change which <acronym>HTTP</acronym> client
  350. <classname>Zend_Service_Delicious</classname> uses, you need to change the
  351. <acronym>HTTP</acronym> client of <classname>Zend_Rest_Client</classname>.
  352. </para>
  353. <example id="zend.service.delicious.httpclient.changing">
  354. <title>Changing the HTTP client of Zend_Rest_Client</title>
  355. <programlisting language="php"><![CDATA[
  356. $myHttpClient = new My_Http_Client();
  357. Zend_Rest_Client::setHttpClient($myHttpClient);
  358. ]]></programlisting>
  359. </example>
  360. <para>
  361. When you are making more than one request with
  362. <classname>Zend_Service_Delicious</classname> to speed your requests, it's better to
  363. configure your <acronym>HTTP</acronym> client to keep connections alive.
  364. </para>
  365. <example id="zend.service.delicious.httpclient.keepalive">
  366. <title>Configuring your HTTP client to keep connections alive</title>
  367. <programlisting language="php"><![CDATA[
  368. Zend_Rest_Client::getHttpClient()->setConfig(array(
  369. 'keepalive' => true
  370. ));
  371. ]]></programlisting>
  372. </example>
  373. <note>
  374. <para>
  375. When a <classname>Zend_Service_Delicious</classname> object is constructed, the
  376. <acronym>SSL</acronym> transport of <classname>Zend_Rest_Client</classname> is set
  377. to <code>'ssl'</code> rather than the default of <code>'ssl2'</code>. This is
  378. because del.icio.us has some problems with <code>'ssl2'</code>, such as requests
  379. taking a long time to complete (around 2 seconds).
  380. </para>
  381. </note>
  382. </sect2>
  383. </sect1>
  384. <!--
  385. vim:se ts=4 sw=4 et:
  386. -->