8 * This source file is subject to the new BSD license that is bundled
9 * with this package in the file LICENSE.txt.
10 * It is also available through the world-wide-web at this URL:
11 * http://framework.zend.com/license/new-bsd
12 * If you did not receive a copy of the license and are unable to
13 * obtain it through the world-wide-web, please send an email
14 * to license@zend.com so we can send you a copy immediately.
17 * @package Zend_Service
19 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
20 * @license http://framework.zend.com/license/new-bsd New BSD License
25 * @see Zend_Http_Client
27 require_once 'Zend/Http/Client.php';
31 * @package Zend_Service
33 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
34 * @license http://framework.zend.com/license/new-bsd New BSD License
35 * @link http://www.simpy.com/doc/api/rest/
37 class Zend_Service_Simpy
40 * Base URI to which API methods and parameters will be appended
44 protected $_baseUri = 'http://simpy.com/simpy/api/rest/';
47 * HTTP client for use in making web service calls
49 * @var Zend_Http_Client
54 * Constructs a new Simpy (free) REST API Client
56 * @param string $username Username for the Simpy user account
57 * @param string $password Password for the Simpy user account
60 public function __construct($username, $password)
62 $this->_http
= new Zend_Http_Client
;
63 $this->_http
->setAuth($username, $password);
67 * Returns the HTTP client currently in use by this class for REST API
68 * calls, intended mainly for testing.
70 * @return Zend_Http_Client
72 public function getHttpClient()
78 * Sends a request to the REST API service and does initial processing
81 * @param string $op Name of the operation for the request
82 * @param array $query Query data for the request (optional)
83 * @throws Zend_Service_Exception
84 * @return DOMDocument Parsed XML response
86 protected function _makeRequest($op, $query = null)
89 $query = array_diff($query, array_filter($query, 'is_null'));
90 $query = '?' . http_build_query($query);
93 $this->_http
->setUri($this->_baseUri
. $op . '.do' . $query);
94 $response = $this->_http
->request('GET');
96 if ($response->isSuccessful()) {
97 $doc = new DOMDocument();
98 $doc->loadXML($response->getBody());
99 $xpath = new DOMXPath($doc);
100 $list = $xpath->query('/status/code');
102 if ($list->length
> 0) {
103 $code = $list->item(0)->nodeValue
;
106 $list = $xpath->query('/status/message');
107 $message = $list->item(0)->nodeValue
;
109 * @see Zend_Service_Exception
111 require_once 'Zend/Service/Exception.php';
112 throw new Zend_Service_Exception($message, $code);
120 * @see Zend_Service_Exception
122 require_once 'Zend/Service/Exception.php';
123 throw new Zend_Service_Exception($response->getMessage(), $response->getStatus());
127 * Returns a list of all tags and their counts, ordered by count in
130 * @param int $limit Limits the number of tags returned (optional)
131 * @link http://www.simpy.com/doc/api/rest/GetTags
132 * @throws Zend_Service_Exception
133 * @return Zend_Service_Simpy_TagSet
135 public function getTags($limit = null)
141 $doc = $this->_makeRequest('GetTags', $query);
144 * @see Zend_Service_Simpy_TagSet
146 require_once 'Zend/Service/Simpy/TagSet.php';
147 return new Zend_Service_Simpy_TagSet($doc);
153 * @param string $tag Tag to be removed
154 * @link http://www.simpy.com/doc/api/rest/RemoveTag
155 * @return Zend_Service_Simpy Provides a fluent interface
157 public function removeTag($tag)
163 $this->_makeRequest('RemoveTag', $query);
171 * @param string $fromTag Tag to be renamed
172 * @param string $toTag New tag name
173 * @link http://www.simpy.com/doc/api/rest/RenameTag
174 * @return Zend_Service_Simpy Provides a fluent interface
176 public function renameTag($fromTag, $toTag)
179 'fromTag' => $fromTag,
183 $this->_makeRequest('RenameTag', $query);
189 * Merges two tags into a new tag.
191 * @param string $fromTag1 First tag to merge.
192 * @param string $fromTag2 Second tag to merge.
193 * @param string $toTag Tag to merge the two tags into.
194 * @link http://www.simpy.com/doc/api/rest/MergeTags
195 * @return Zend_Service_Simpy Provides a fluent interface
197 public function mergeTags($fromTag1, $fromTag2, $toTag)
200 'fromTag1' => $fromTag1,
201 'fromTag2' => $fromTag2,
205 $this->_makeRequest('MergeTags', $query);
211 * Splits a single tag into two separate tags.
213 * @param string $tag Tag to split
214 * @param string $toTag1 First tag to split into
215 * @param string $toTag2 Second tag to split into
216 * @link http://www.simpy.com/doc/api/rest/SplitTag
217 * @return Zend_Service_Simpy Provides a fluent interface
219 public function splitTag($tag, $toTag1, $toTag2)
227 $this->_makeRequest('SplitTag', $query);
233 * Performs a query on existing links and returns the results or returns all
234 * links if no particular query is specified (which should be used sparingly
235 * to prevent overloading Simpy servers)
237 * @param Zend_Service_Simpy_LinkQuery $q Query object to use (optional)
238 * @return Zend_Service_Simpy_LinkSet
240 public function getLinks(Zend_Service_Simpy_LinkQuery
$q = null)
244 'q' => $q->getQueryString(),
245 'limit' => $q->getLimit(),
246 'date' => $q->getDate(),
247 'afterDate' => $q->getAfterDate(),
248 'beforeDate' => $q->getBeforeDate()
251 $doc = $this->_makeRequest('GetLinks', $query);
253 $doc = $this->_makeRequest('GetLinks');
257 * @see Zend_Service_Simpy_LinkSet
259 require_once 'Zend/Service/Simpy/LinkSet.php';
260 return new Zend_Service_Simpy_LinkSet($doc);
264 * Saves a given link.
266 * @param string $title Title of the page to save
267 * @param string $href URL of the page to save
268 * @param int $accessType ACCESSTYPE_PUBLIC or ACCESSTYPE_PRIVATE
269 * @param mixed $tags String containing a comma-separated list of
270 * tags or array of strings containing tags
272 * @param string $urlNickname Alternative custom title (optional)
273 * @param string $note Free text note (optional)
274 * @link Zend_Service_Simpy::ACCESSTYPE_PUBLIC
275 * @link Zend_Service_Simpy::ACCESSTYPE_PRIVATE
276 * @link http://www.simpy.com/doc/api/rest/SaveLink
277 * @return Zend_Service_Simpy Provides a fluent interface
279 public function saveLink($title, $href, $accessType, $tags = null, $urlNickname = null, $note = null)
281 if (is_array($tags)) {
282 $tags = implode(',', $tags);
288 'accessType' => $accessType,
290 'urlNickname' => $urlNickname,
294 $this->_makeRequest('SaveLink', $query);
300 * Deletes a given link.
302 * @param string $href URL of the bookmark to delete
303 * @link http://www.simpy.com/doc/api/rest/DeleteLink
304 * @return Zend_Service_Simpy Provides a fluent interface
306 public function deleteLink($href)
312 $this->_makeRequest('DeleteLink', $query);
318 * Return a list of watchlists and their meta-data, including the number
319 * of new links added to each watchlist since last login.
321 * @link http://www.simpy.com/doc/api/rest/GetWatchlists
322 * @return Zend_Service_Simpy_WatchlistSet
324 public function getWatchlists()
326 $doc = $this->_makeRequest('GetWatchlists');
329 * @see Zend_Service_Simpy_WatchlistSet
331 require_once 'Zend/Service/Simpy/WatchlistSet.php';
332 return new Zend_Service_Simpy_WatchlistSet($doc);
336 * Returns the meta-data for a given watchlist.
338 * @param int $watchlistId ID of the watchlist to retrieve
339 * @link http://www.simpy.com/doc/api/rest/GetWatchlist
340 * @return Zend_Service_Simpy_Watchlist
342 public function getWatchlist($watchlistId)
345 'watchlistId' => $watchlistId
348 $doc = $this->_makeRequest('GetWatchlist', $query);
351 * @see Zend_Service_Simpy_Watchlist
353 require_once 'Zend/Service/Simpy/Watchlist.php';
354 return new Zend_Service_Simpy_Watchlist($doc->documentElement
);
358 * Returns all notes in reverse chronological order by add date or by
361 * @param string $q Query string formatted using Simpy search syntax
362 * and search fields (optional)
363 * @param int $limit Limits the number notes returned (optional)
364 * @link http://www.simpy.com/doc/api/rest/GetNotes
365 * @link http://www.simpy.com/simpy/FAQ.do#searchSyntax
366 * @link http://www.simpy.com/simpy/FAQ.do#searchFieldsLinks
367 * @return Zend_Service_Simpy_NoteSet
369 public function getNotes($q = null, $limit = null)
376 $doc = $this->_makeRequest('GetNotes', $query);
379 * @see Zend_Service_Simpy_NoteSet
381 require_once 'Zend/Service/Simpy/NoteSet.php';
382 return new Zend_Service_Simpy_NoteSet($doc);
388 * @param string $title Title of the note
389 * @param mixed $tags String containing a comma-separated list of
390 * tags or array of strings containing tags
392 * @param string $description Free-text note (optional)
393 * @param int $noteId Unique identifier for an existing note to
395 * @link http://www.simpy.com/doc/api/rest/SaveNote
396 * @return Zend_Service_Simpy Provides a fluent interface
398 public function saveNote($title, $tags = null, $description = null, $noteId = null)
400 if (is_array($tags)) {
401 $tags = implode(',', $tags);
407 'description' => $description,
411 $this->_makeRequest('SaveNote', $query);
417 * Deletes a given note.
419 * @param int $noteId ID of the note to delete
420 * @link http://www.simpy.com/doc/api/rest/DeleteNote
421 * @return Zend_Service_Simpy Provides a fluent interface
423 public function deleteNote($noteId)
429 $this->_makeRequest('DeleteNote', $query);