Twitter.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Service
  17. * @subpackage Twitter
  18. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @see Zend_Rest_Client
  24. */
  25. require_once 'Zend/Rest/Client.php';
  26. /**
  27. * @see Zend_Rest_Client_Result
  28. */
  29. require_once 'Zend/Rest/Client/Result.php';
  30. /**
  31. * @category Zend
  32. * @package Zend_Service
  33. * @subpackage Twitter
  34. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. */
  37. class Zend_Service_Twitter extends Zend_Rest_Client
  38. {
  39. /**
  40. * Whether or not authorization has been initialized for the current user.
  41. * @var bool
  42. */
  43. protected $_authInitialized = false;
  44. /**
  45. * @var Zend_Http_CookieJar
  46. */
  47. protected $_cookieJar;
  48. /**
  49. * Date format for 'since' strings
  50. * @var string
  51. */
  52. protected $_dateFormat = 'D, d M Y H:i:s T';
  53. /**
  54. * Username
  55. * @var string
  56. */
  57. protected $_username;
  58. /**
  59. * Password
  60. * @var string
  61. */
  62. protected $_password;
  63. /**
  64. * Current method type (for method proxying)
  65. * @var string
  66. */
  67. protected $_methodType;
  68. /**
  69. * Types of API methods
  70. * @var array
  71. */
  72. protected $_methodTypes = array('status' , 'user' , 'directMessage' , 'friendship' , 'account' , 'favorite');
  73. /**
  74. * Constructor
  75. *
  76. * @param string $username
  77. * @param string $password
  78. * @return void
  79. */
  80. public function __construct ($username, $password = null)
  81. {
  82. if (is_array($username) && is_null($password)) {
  83. if (isset($username['username']) && isset($username['password'])) {
  84. $this->setUsername($username['username']);
  85. $this->setPassword($username['password']);
  86. } elseif (isset($username[0]) && isset($username[1])) {
  87. $this->setUsername($username[0]);
  88. $this->setPassword($username[1]);
  89. }
  90. } else {
  91. $this->setUsername($username);
  92. $this->setPassword($password);
  93. }
  94. $this->setUri('http://twitter.com');
  95. $client = self::getHttpClient();
  96. $client->setHeaders('Accept-Charset', 'ISO-8859-1,utf-8');
  97. }
  98. /**
  99. * Retrieve username
  100. *
  101. * @return string
  102. */
  103. public function getUsername ()
  104. {
  105. return $this->_username;
  106. }
  107. /**
  108. * Set username
  109. *
  110. * @param string $value
  111. * @return Zend_Service_Twitter
  112. */
  113. public function setUsername ($value)
  114. {
  115. $this->_username = $value;
  116. $this->_authInitialized = false;
  117. return $this;
  118. }
  119. /**
  120. * Retrieve password
  121. *
  122. * @return string
  123. */
  124. public function getPassword ()
  125. {
  126. return $this->_password;
  127. }
  128. /**
  129. * Set password
  130. *
  131. * @param string $value
  132. * @return Zend_Service_Twitter
  133. */
  134. public function setPassword ($value)
  135. {
  136. $this->_password = $value;
  137. $this->_authInitialized = false;
  138. return $this;
  139. }
  140. /**
  141. * Proxy service methods
  142. *
  143. * @param string $type
  144. * @return Zend_Service_Twitter
  145. * @throws Zend_Service_Twitter_Exception if method is not in method types list
  146. */
  147. public function __get ($type)
  148. {
  149. if (! in_array($type, $this->_methodTypes)) {
  150. include_once 'Zend/Service/Twitter/Exception.php';
  151. throw new Zend_Service_Twitter_Exception('Invalid method type "' . $type . '"');
  152. }
  153. $this->_methodType = $type;
  154. return $this;
  155. }
  156. /**
  157. * Method overloading
  158. *
  159. * @param string $method
  160. * @param array $params
  161. * @return mixed
  162. * @throws Zend_Service_Twitter_Exception if unable to find method
  163. */
  164. public function __call ($method, $params)
  165. {
  166. if (empty($this->_methodType)) {
  167. include_once 'Zend/Service/Twitter/Exception.php';
  168. throw new Zend_Service_Twitter_Exception('Invalid method "' . $method . '"');
  169. }
  170. $test = $this->_methodType . ucfirst($method);
  171. if (! method_exists($this, $test)) {
  172. include_once 'Zend/Service/Twitter/Exception.php';
  173. throw new Zend_Service_Twitter_Exception('Invalid method "' . $test . '"');
  174. }
  175. return call_user_func_array(array($this , $test), $params);
  176. }
  177. /**
  178. * Initialize HTTP authentication
  179. *
  180. * @return void
  181. */
  182. protected function _init ()
  183. {
  184. $client = self::getHttpClient();
  185. $client->resetParameters();
  186. if (null == $this->_cookieJar) {
  187. $client->setCookieJar();
  188. $this->_cookieJar = $client->getCookieJar();
  189. } else {
  190. $client->setCookieJar($this->_cookieJar);
  191. }
  192. if (! $this->_authInitialized) {
  193. $client->setAuth($this->getUsername(), $this->getPassword());
  194. $this->_authInitialized = true;
  195. }
  196. }
  197. /**
  198. * Set date header
  199. *
  200. * @param int|string $value
  201. * @return void
  202. */
  203. protected function _setDate ($value)
  204. {
  205. if (is_int($value)) {
  206. $date = date($this->_dateFormat, $value);
  207. } else {
  208. $date = date($this->_dateFormat, strtotime($value));
  209. }
  210. self::getHttpClient()->setHeaders('If-Modified-Since', $date);
  211. }
  212. /**
  213. * Public Timeline status
  214. *
  215. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  216. * @return Zend_Rest_Client_Result
  217. */
  218. public function statusPublicTimeline ()
  219. {
  220. $this->_init();
  221. $path = '/statuses/public_timeline.xml';
  222. $response = $this->restGet($path);
  223. return new Zend_Rest_Client_Result($response->getBody());
  224. }
  225. /**
  226. * Friend Timeline Status
  227. *
  228. * $params may include one or more of the following keys
  229. * - id: ID of a friend whose timeline you wish to receive
  230. * - count: how many statuses to return
  231. * - since: return results only after the date specified
  232. * - since_id: return results only after the specific tweet
  233. * - page: return page X of results
  234. *
  235. * @param array $params
  236. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  237. * @return void
  238. */
  239. public function statusFriendsTimeline (array $params = array())
  240. {
  241. $this->_init();
  242. $path = '/statuses/friends_timeline';
  243. $_params = array();
  244. foreach ($params as $key => $value) {
  245. switch (strtolower($key)) {
  246. case 'count':
  247. $count = (int) $value;
  248. if (0 >= $count) {
  249. $count = 1;
  250. } elseif (200 < $count) {
  251. $count = 200;
  252. }
  253. $_params['count'] = (int) $count;
  254. break;
  255. case 'since_id':
  256. $_params['since_id'] = $this->_validInteger($value);
  257. break;
  258. case 'since':
  259. $this->_setDate($value);
  260. break;
  261. case 'page':
  262. $_params['page'] = (int) $value;
  263. break;
  264. default:
  265. break;
  266. }
  267. }
  268. $path .= '.xml';
  269. $response = $this->restGet($path, $_params);
  270. return new Zend_Rest_Client_Result($response->getBody());
  271. }
  272. /**
  273. * User Timeline status
  274. *
  275. * $params may include one or more of the following keys
  276. * - id: ID of a friend whose timeline you wish to receive
  277. * - since: return results only after the date specified
  278. * - page: return page X of results
  279. * - count: how many statuses to return
  280. *
  281. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  282. * @return Zend_Rest_Client_Result
  283. */
  284. public function statusUserTimeline (array $params = array())
  285. {
  286. $this->_init();
  287. $path = '/statuses/user_timeline';
  288. $_params = array();
  289. foreach ($params as $key => $value) {
  290. switch (strtolower($key)) {
  291. case 'id':
  292. $path .= '/' . $this->_validInteger($value);
  293. break;
  294. case 'since':
  295. $this->_setDate($value);
  296. break;
  297. case 'page':
  298. $_params['page'] = (int) $value;
  299. break;
  300. case 'count':
  301. $count = (int) $value;
  302. if (0 >= $count) {
  303. $count = 1;
  304. } elseif (200 < $count) {
  305. $count = 200;
  306. }
  307. $_params['count'] = $count;
  308. break;
  309. default:
  310. break;
  311. }
  312. }
  313. $path .= '.xml';
  314. $response = $this->restGet($path, $_params);
  315. return new Zend_Rest_Client_Result($response->getBody());
  316. }
  317. /**
  318. * Show a single status
  319. *
  320. * @param int $id Id of status to show
  321. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  322. * @return Zend_Rest_Client_Result
  323. */
  324. public function statusShow ($id)
  325. {
  326. $this->_init();
  327. $path = '/statuses/show/' . $this->_validInteger($id) . '.xml';
  328. $response = $this->restGet($path);
  329. return new Zend_Rest_Client_Result($response->getBody());
  330. }
  331. /**
  332. * Update user's current status
  333. *
  334. * @param string $status
  335. * @param int $in_reply_to_status_id
  336. * @return Zend_Rest_Client_Result
  337. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  338. * @throws Zend_Service_Twitter_Exception if message is too short or too long
  339. */
  340. public function statusUpdate ($status, $in_reply_to_status_id = null)
  341. {
  342. $this->_init();
  343. $path = '/statuses/update.xml';
  344. $len = iconv_strlen($status, 'UTF-8');
  345. if ($len > 140) {
  346. include_once 'Zend/Service/Twitter/Exception.php';
  347. throw new Zend_Service_Twitter_Exception('Status must be no more than 140 characters in length');
  348. } elseif (0 == $len) {
  349. include_once 'Zend/Service/Twitter/Exception.php';
  350. throw new Zend_Service_Twitter_Exception('Status must contain at least one character');
  351. }
  352. $data = array('status' => $status);
  353. if (is_numeric($in_reply_to_status_id) && ! empty($in_reply_to_status_id)) {
  354. $data['in_reply_to_status_id'] = $in_reply_to_status_id;
  355. }
  356. //$this->status = $status;
  357. $response = $this->restPost($path, $data);
  358. return new Zend_Rest_Client_Result($response->getBody());
  359. }
  360. /**
  361. * Get status replies
  362. *
  363. * $params may include one or more of the following keys
  364. * - since: return results only after the date specified
  365. * - since_id: return results only after the specified tweet id
  366. * - page: return page X of results
  367. *
  368. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  369. * @return Zend_Rest_Client_Result
  370. */
  371. public function statusReplies (array $params = array())
  372. {
  373. $this->_init();
  374. $path = '/statuses/replies.xml';
  375. $_params = array();
  376. foreach ($params as $key => $value) {
  377. switch (strtolower($key)) {
  378. case 'since':
  379. $this->_setDate($value);
  380. break;
  381. case 'since_id':
  382. $_params['since_id'] = $this->_validInteger($value);
  383. break;
  384. case 'page':
  385. $_params['page'] = (int) $value;
  386. break;
  387. default:
  388. break;
  389. }
  390. }
  391. $response = $this->restGet($path, $_params);
  392. return new Zend_Rest_Client_Result($response->getBody());
  393. }
  394. /**
  395. * Destroy a status message
  396. *
  397. * @param int $id ID of status to destroy
  398. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  399. * @return Zend_Rest_Client_Result
  400. */
  401. public function statusDestroy ($id)
  402. {
  403. $this->_init();
  404. $path = '/statuses/destroy/' . $this->_validInteger($id) . '.xml';
  405. $response = $this->restPost($path);
  406. return new Zend_Rest_Client_Result($response->getBody());
  407. }
  408. /**
  409. * User friends
  410. *
  411. * @param int|string $id Id or username of user for whom to fetch friends
  412. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  413. * @return Zend_Rest_Client_Result
  414. */
  415. public function userFriends (array $params = array())
  416. {
  417. $this->_init();
  418. $path = '/statuses/friends';
  419. $_params = array();
  420. foreach ($params as $key => $value) {
  421. switch (strtolower($key)) {
  422. case 'id':
  423. $path .= '/' . $this->_validInteger($value);
  424. break;
  425. case 'since':
  426. $this->_setDate($value);
  427. break;
  428. case 'page':
  429. $_params['page'] = (int) $value;
  430. break;
  431. default:
  432. break;
  433. }
  434. }
  435. $path .= '.xml';
  436. $response = $this->restGet($path, $_params);
  437. return new Zend_Rest_Client_Result($response->getBody());
  438. }
  439. /**
  440. * User Followers
  441. *
  442. * @param bool $lite If true, prevents inline inclusion of current status for followers; defaults to false
  443. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  444. * @return Zend_Rest_Client_Result
  445. */
  446. public function userFollowers ($lite = false)
  447. {
  448. $this->_init();
  449. $path = '/statuses/followers.xml';
  450. if ($lite) {
  451. $this->lite = 'true';
  452. }
  453. $response = $this->restGet($path);
  454. return new Zend_Rest_Client_Result($response->getBody());
  455. }
  456. /**
  457. * Get featured users
  458. *
  459. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  460. * @return Zend_Rest_Client_Result
  461. */
  462. public function userFeatured ()
  463. {
  464. $this->_init();
  465. $path = '/statuses/featured.xml';
  466. $response = $this->restGet($path);
  467. return new Zend_Rest_Client_Result($response->getBody());
  468. }
  469. /**
  470. * Show extended information on a user
  471. *
  472. * @param int|string $id User ID or name
  473. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  474. * @return Zend_Rest_Client_Result
  475. */
  476. public function userShow ($id)
  477. {
  478. $this->_init();
  479. $path = '/users/show/' . $this->_validInteger($id) . '.xml';
  480. $response = $this->restGet($path);
  481. return new Zend_Rest_Client_Result($response->getBody());
  482. }
  483. /**
  484. * Retrieve direct messages for the current user
  485. *
  486. * $params may include one or more of the following keys
  487. * - since: return results only after the date specified
  488. * - since_id: return statuses only greater than the one specified
  489. * - page: return page X of results
  490. *
  491. * @param array $params
  492. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  493. * @return Zend_Rest_Client_Result
  494. */
  495. public function directMessageMessages (array $params = array())
  496. {
  497. $this->_init();
  498. $path = '/direct_messages.xml';
  499. $_params = array();
  500. foreach ($params as $key => $value) {
  501. switch (strtolower($key)) {
  502. case 'since':
  503. $this->_setDate($value);
  504. break;
  505. case 'since_id':
  506. $_params['since_id'] = $this->_validInteger($value);
  507. break;
  508. case 'page':
  509. $_params['page'] = (int) $value;
  510. break;
  511. default:
  512. break;
  513. }
  514. }
  515. $response = $this->restGet($path, $_params);
  516. return new Zend_Rest_Client_Result($response->getBody());
  517. }
  518. /**
  519. * Retrieve list of direct messages sent by current user
  520. *
  521. * $params may include one or more of the following keys
  522. * - since: return results only after the date specified
  523. * - since_id: return statuses only greater than the one specified
  524. * - page: return page X of results
  525. *
  526. * @param array $params
  527. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  528. * @return Zend_Rest_Client_Result
  529. */
  530. public function directMessageSent (array $params = array())
  531. {
  532. $this->_init();
  533. $path = '/direct_messages/sent.xml';
  534. $_params = array();
  535. foreach ($params as $key => $value) {
  536. switch (strtolower($key)) {
  537. case 'since':
  538. $this->_setDate($value);
  539. break;
  540. case 'since_id':
  541. $_params['since_id'] = $this->_validInteger($value);
  542. break;
  543. case 'page':
  544. $_params['page'] = (int) $value;
  545. break;
  546. default:
  547. break;
  548. }
  549. }
  550. $response = $this->restGet($path, $_params);
  551. return new Zend_Rest_Client_Result($response->getBody());
  552. }
  553. /**
  554. * Send a direct message to a user
  555. *
  556. * @param int|string $user User to whom to send message
  557. * @param string $text Message to send to user
  558. * @return Zend_Rest_Client_Result
  559. * @throws Zend_Service_Twitter_Exception if message is too short or too long
  560. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  561. */
  562. public function directMessageNew ($user, $text)
  563. {
  564. $this->_init();
  565. $path = '/direct_messages/new.xml';
  566. $len = iconv_strlen($text, 'UTF-8');
  567. if (0 == $len) {
  568. throw new Zend_Service_Twitter_Exception('Direct message must contain at least one character');
  569. } elseif (140 < $len) {
  570. throw new Zend_Service_Twitter_Exception('Direct message must contain no more than 140 characters');
  571. }
  572. $data = array('user' => $user , 'text' => $text);
  573. $response = $this->restPost($path, $data);
  574. return new Zend_Rest_Client_Result($response->getBody());
  575. }
  576. /**
  577. * Destroy a direct message
  578. *
  579. * @param int $id ID of message to destroy
  580. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  581. * @return Zend_Rest_Client_Result
  582. */
  583. public function directMessageDestroy ($id)
  584. {
  585. $this->_init();
  586. $path = '/direct_messages/destroy/' . $this->_validInteger($id) . '.xml';
  587. $response = $this->restPost($path);
  588. return new Zend_Rest_Client_Result($response->getBody());
  589. }
  590. /**
  591. * Create friendship
  592. *
  593. * @param int|string $id User ID or name of new friend
  594. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  595. * @return Zend_Rest_Client_Result
  596. */
  597. public function friendshipCreate ($id)
  598. {
  599. $this->_init();
  600. $path = '/friendships/create/' . $this->_validInteger($id) . '.xml';
  601. $response = $this->restPost($path);
  602. return new Zend_Rest_Client_Result($response->getBody());
  603. }
  604. /**
  605. * Destroy friendship
  606. *
  607. * @param int|string $id User ID or name of friend to remove
  608. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  609. * @return Zend_Rest_Client_Result
  610. */
  611. public function friendshipDestroy ($id)
  612. {
  613. $this->_init();
  614. $path = '/friendships/destroy/' . $this->_validInteger($id) . '.xml';
  615. $response = $this->restPost($path);
  616. return new Zend_Rest_Client_Result($response->getBody());
  617. }
  618. /**
  619. * Friendship exists
  620. *
  621. * @param int|string $id User ID or name of friend to see if they are your friend
  622. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  623. * @return Zend_Rest_Client_result
  624. */
  625. public function friendshipExists ($id)
  626. {
  627. $this->_init();
  628. $path = '/friendships/exists.xml';
  629. $data = array('user_a' => $this->getUsername() , 'user_b' => $this->_validInteger($id));
  630. $response = $this->restGet($path, $data);
  631. return new Zend_Rest_Client_Result($response->getBody());
  632. }
  633. /**
  634. * Verify Account Credentials
  635. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  636. *
  637. * @return Zend_Rest_Client_Result
  638. */
  639. public function accountVerifyCredentials ()
  640. {
  641. $this->_init();
  642. $response = $this->restGet('/account/verify_credentials.xml');
  643. return new Zend_Rest_Client_Result($response->getBody());
  644. }
  645. /**
  646. * End current session
  647. *
  648. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  649. * @return true
  650. */
  651. public function accountEndSession ()
  652. {
  653. $this->_init();
  654. $this->restGet('/account/end_session');
  655. return true;
  656. }
  657. /**
  658. * Returns the number of api requests you have left per hour.
  659. *
  660. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  661. * @return Zend_Rest_Client_Result
  662. */
  663. public function accountRateLimitStatus ()
  664. {
  665. $this->_init();
  666. $response = $this->restGet('/account/rate_limit_status.xml');
  667. return new Zend_Rest_Client_Result($response->getBody());
  668. }
  669. /**
  670. * Fetch favorites
  671. *
  672. * $params may contain one or more of the following:
  673. * - 'id': Id of a user for whom to fetch favorites
  674. * - 'page': Retrieve a different page of resuls
  675. *
  676. * @param array $params
  677. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  678. * @return Zend_Rest_Client_Result
  679. */
  680. public function favoriteFavorites (array $params = array())
  681. {
  682. $this->_init();
  683. $path = '/favorites';
  684. $_params = array();
  685. foreach ($params as $key => $value) {
  686. switch (strtolower($key)) {
  687. case 'id':
  688. $path .= '/' . $this->_validInteger($value);
  689. break;
  690. case 'page':
  691. $_params['page'] = (int) $value;
  692. break;
  693. default:
  694. break;
  695. }
  696. }
  697. $path .= '.xml';
  698. $response = $this->restGet($path, $_params);
  699. return new Zend_Rest_Client_Result($response->getBody());
  700. }
  701. /**
  702. * Mark a status as a favorite
  703. *
  704. * @param int $id Status ID you want to mark as a favorite
  705. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  706. * @return Zend_Rest_Client_Result
  707. */
  708. public function favoriteCreate ($id)
  709. {
  710. $this->_init();
  711. $path = '/favorites/create/' . $this->_validInteger($id) . '.xml';
  712. $response = $this->restPost($path);
  713. return new Zend_Rest_Client_Result($response->getBody());
  714. }
  715. /**
  716. * Remove a favorite
  717. *
  718. * @param int $id Status ID you want to de-list as a favorite
  719. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  720. * @return Zend_Rest_Client_Result
  721. */
  722. public function favoriteDestroy ($id)
  723. {
  724. $this->_init();
  725. $path = '/favorites/destroy/' . $this->_validInteger($id) . '.xml';
  726. $response = $this->restPost($path);
  727. return new Zend_Rest_Client_Result($response->getBody());
  728. }
  729. /**
  730. * Protected function to validate that the integer is valid or return a 0
  731. * @param $int
  732. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  733. * @return integer
  734. */
  735. protected function _validInteger ($int)
  736. {
  737. if (preg_match("/(\d+)/", $int)) {
  738. return $int;
  739. }
  740. return 0;
  741. }
  742. }