Twitter.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  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 'page':
  295. $_params['page'] = (int) $value;
  296. break;
  297. case 'count':
  298. $count = (int) $value;
  299. if (0 >= $count) {
  300. $count = 1;
  301. } elseif (200 < $count) {
  302. $count = 200;
  303. }
  304. $_params['count'] = $count;
  305. break;
  306. case 'user_id':
  307. break;
  308. case 'screen_name':
  309. break;
  310. case 'since_id':
  311. break;
  312. case 'max_id':
  313. break;
  314. default:
  315. break;
  316. }
  317. }
  318. $path .= '.xml';
  319. $response = $this->restGet($path, $_params);
  320. return new Zend_Rest_Client_Result($response->getBody());
  321. }
  322. /**
  323. * Show a single status
  324. *
  325. * @param int $id Id of status to show
  326. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  327. * @return Zend_Rest_Client_Result
  328. */
  329. public function statusShow ($id)
  330. {
  331. $this->_init();
  332. $path = '/statuses/show/' . $this->_validInteger($id) . '.xml';
  333. $response = $this->restGet($path);
  334. return new Zend_Rest_Client_Result($response->getBody());
  335. }
  336. /**
  337. * Update user's current status
  338. *
  339. * @param string $status
  340. * @param int $in_reply_to_status_id
  341. * @return Zend_Rest_Client_Result
  342. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  343. * @throws Zend_Service_Twitter_Exception if message is too short or too long
  344. */
  345. public function statusUpdate ($status, $in_reply_to_status_id = null)
  346. {
  347. $this->_init();
  348. $path = '/statuses/update.xml';
  349. $len = iconv_strlen($status, 'UTF-8');
  350. if ($len > 140) {
  351. include_once 'Zend/Service/Twitter/Exception.php';
  352. throw new Zend_Service_Twitter_Exception('Status must be no more than 140 characters in length');
  353. } elseif (0 == $len) {
  354. include_once 'Zend/Service/Twitter/Exception.php';
  355. throw new Zend_Service_Twitter_Exception('Status must contain at least one character');
  356. }
  357. $data = array('status' => $status);
  358. if (is_numeric($in_reply_to_status_id) && ! empty($in_reply_to_status_id)) {
  359. $data['in_reply_to_status_id'] = $in_reply_to_status_id;
  360. }
  361. //$this->status = $status;
  362. $response = $this->restPost($path, $data);
  363. return new Zend_Rest_Client_Result($response->getBody());
  364. }
  365. /**
  366. * Get status replies
  367. *
  368. * $params may include one or more of the following keys
  369. * - since: return results only after the date specified
  370. * - since_id: return results only after the specified tweet id
  371. * - page: return page X of results
  372. *
  373. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  374. * @return Zend_Rest_Client_Result
  375. */
  376. public function statusReplies (array $params = array())
  377. {
  378. $this->_init();
  379. $path = '/statuses/replies.xml';
  380. $_params = array();
  381. foreach ($params as $key => $value) {
  382. switch (strtolower($key)) {
  383. case 'since':
  384. $this->_setDate($value);
  385. break;
  386. case 'since_id':
  387. $_params['since_id'] = $this->_validInteger($value);
  388. break;
  389. case 'page':
  390. $_params['page'] = (int) $value;
  391. break;
  392. default:
  393. break;
  394. }
  395. }
  396. $response = $this->restGet($path, $_params);
  397. return new Zend_Rest_Client_Result($response->getBody());
  398. }
  399. /**
  400. * Destroy a status message
  401. *
  402. * @param int $id ID of status to destroy
  403. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  404. * @return Zend_Rest_Client_Result
  405. */
  406. public function statusDestroy ($id)
  407. {
  408. $this->_init();
  409. $path = '/statuses/destroy/' . $this->_validInteger($id) . '.xml';
  410. $response = $this->restPost($path);
  411. return new Zend_Rest_Client_Result($response->getBody());
  412. }
  413. /**
  414. * User friends
  415. *
  416. * @param int|string $id Id or username of user for whom to fetch friends
  417. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  418. * @return Zend_Rest_Client_Result
  419. */
  420. public function userFriends (array $params = array())
  421. {
  422. $this->_init();
  423. $path = '/statuses/friends';
  424. $_params = array();
  425. foreach ($params as $key => $value) {
  426. switch (strtolower($key)) {
  427. case 'id':
  428. $path .= '/' . $this->_validInteger($value);
  429. break;
  430. case 'since':
  431. $this->_setDate($value);
  432. break;
  433. case 'page':
  434. $_params['page'] = (int) $value;
  435. break;
  436. default:
  437. break;
  438. }
  439. }
  440. $path .= '.xml';
  441. $response = $this->restGet($path, $_params);
  442. return new Zend_Rest_Client_Result($response->getBody());
  443. }
  444. /**
  445. * User Followers
  446. *
  447. * @param bool $lite If true, prevents inline inclusion of current status for followers; defaults to false
  448. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  449. * @return Zend_Rest_Client_Result
  450. */
  451. public function userFollowers ($lite = false)
  452. {
  453. $this->_init();
  454. $path = '/statuses/followers.xml';
  455. if ($lite) {
  456. $this->lite = 'true';
  457. }
  458. $response = $this->restGet($path);
  459. return new Zend_Rest_Client_Result($response->getBody());
  460. }
  461. /**
  462. * Get featured users
  463. *
  464. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  465. * @return Zend_Rest_Client_Result
  466. */
  467. public function userFeatured ()
  468. {
  469. $this->_init();
  470. $path = '/statuses/featured.xml';
  471. $response = $this->restGet($path);
  472. return new Zend_Rest_Client_Result($response->getBody());
  473. }
  474. /**
  475. * Show extended information on a user
  476. *
  477. * @param int|string $id User ID or name
  478. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  479. * @return Zend_Rest_Client_Result
  480. */
  481. public function userShow ($id)
  482. {
  483. $this->_init();
  484. $path = '/users/show/' . $this->_validInteger($id) . '.xml';
  485. $response = $this->restGet($path);
  486. return new Zend_Rest_Client_Result($response->getBody());
  487. }
  488. /**
  489. * Retrieve direct messages for the current user
  490. *
  491. * $params may include one or more of the following keys
  492. * - since: return results only after the date specified
  493. * - since_id: return statuses only greater than the one specified
  494. * - page: return page X of results
  495. *
  496. * @param array $params
  497. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  498. * @return Zend_Rest_Client_Result
  499. */
  500. public function directMessageMessages (array $params = array())
  501. {
  502. $this->_init();
  503. $path = '/direct_messages.xml';
  504. $_params = array();
  505. foreach ($params as $key => $value) {
  506. switch (strtolower($key)) {
  507. case 'since':
  508. $this->_setDate($value);
  509. break;
  510. case 'since_id':
  511. $_params['since_id'] = $this->_validInteger($value);
  512. break;
  513. case 'page':
  514. $_params['page'] = (int) $value;
  515. break;
  516. default:
  517. break;
  518. }
  519. }
  520. $response = $this->restGet($path, $_params);
  521. return new Zend_Rest_Client_Result($response->getBody());
  522. }
  523. /**
  524. * Retrieve list of direct messages sent by current user
  525. *
  526. * $params may include one or more of the following keys
  527. * - since: return results only after the date specified
  528. * - since_id: return statuses only greater than the one specified
  529. * - page: return page X of results
  530. *
  531. * @param array $params
  532. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  533. * @return Zend_Rest_Client_Result
  534. */
  535. public function directMessageSent (array $params = array())
  536. {
  537. $this->_init();
  538. $path = '/direct_messages/sent.xml';
  539. $_params = array();
  540. foreach ($params as $key => $value) {
  541. switch (strtolower($key)) {
  542. case 'since':
  543. $this->_setDate($value);
  544. break;
  545. case 'since_id':
  546. $_params['since_id'] = $this->_validInteger($value);
  547. break;
  548. case 'page':
  549. $_params['page'] = (int) $value;
  550. break;
  551. default:
  552. break;
  553. }
  554. }
  555. $response = $this->restGet($path, $_params);
  556. return new Zend_Rest_Client_Result($response->getBody());
  557. }
  558. /**
  559. * Send a direct message to a user
  560. *
  561. * @param int|string $user User to whom to send message
  562. * @param string $text Message to send to user
  563. * @return Zend_Rest_Client_Result
  564. * @throws Zend_Service_Twitter_Exception if message is too short or too long
  565. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  566. */
  567. public function directMessageNew ($user, $text)
  568. {
  569. $this->_init();
  570. $path = '/direct_messages/new.xml';
  571. $len = iconv_strlen($text, 'UTF-8');
  572. if (0 == $len) {
  573. throw new Zend_Service_Twitter_Exception('Direct message must contain at least one character');
  574. } elseif (140 < $len) {
  575. throw new Zend_Service_Twitter_Exception('Direct message must contain no more than 140 characters');
  576. }
  577. $data = array('user' => $user , 'text' => $text);
  578. $response = $this->restPost($path, $data);
  579. return new Zend_Rest_Client_Result($response->getBody());
  580. }
  581. /**
  582. * Destroy a direct message
  583. *
  584. * @param int $id ID of message to destroy
  585. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  586. * @return Zend_Rest_Client_Result
  587. */
  588. public function directMessageDestroy ($id)
  589. {
  590. $this->_init();
  591. $path = '/direct_messages/destroy/' . $this->_validInteger($id) . '.xml';
  592. $response = $this->restPost($path);
  593. return new Zend_Rest_Client_Result($response->getBody());
  594. }
  595. /**
  596. * Create friendship
  597. *
  598. * @param int|string $id User ID or name of new friend
  599. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  600. * @return Zend_Rest_Client_Result
  601. */
  602. public function friendshipCreate ($id)
  603. {
  604. $this->_init();
  605. $path = '/friendships/create/' . $this->_validInteger($id) . '.xml';
  606. $response = $this->restPost($path);
  607. return new Zend_Rest_Client_Result($response->getBody());
  608. }
  609. /**
  610. * Destroy friendship
  611. *
  612. * @param int|string $id User ID or name of friend to remove
  613. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  614. * @return Zend_Rest_Client_Result
  615. */
  616. public function friendshipDestroy ($id)
  617. {
  618. $this->_init();
  619. $path = '/friendships/destroy/' . $this->_validInteger($id) . '.xml';
  620. $response = $this->restPost($path);
  621. return new Zend_Rest_Client_Result($response->getBody());
  622. }
  623. /**
  624. * Friendship exists
  625. *
  626. * @param int|string $id User ID or name of friend to see if they are your friend
  627. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  628. * @return Zend_Rest_Client_result
  629. */
  630. public function friendshipExists ($id)
  631. {
  632. $this->_init();
  633. $path = '/friendships/exists.xml';
  634. $data = array('user_a' => $this->getUsername() , 'user_b' => $this->_validInteger($id));
  635. $response = $this->restGet($path, $data);
  636. return new Zend_Rest_Client_Result($response->getBody());
  637. }
  638. /**
  639. * Verify Account Credentials
  640. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  641. *
  642. * @return Zend_Rest_Client_Result
  643. */
  644. public function accountVerifyCredentials ()
  645. {
  646. $this->_init();
  647. $response = $this->restGet('/account/verify_credentials.xml');
  648. return new Zend_Rest_Client_Result($response->getBody());
  649. }
  650. /**
  651. * End current session
  652. *
  653. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  654. * @return true
  655. */
  656. public function accountEndSession ()
  657. {
  658. $this->_init();
  659. $this->restGet('/account/end_session');
  660. return true;
  661. }
  662. /**
  663. * Returns the number of api requests you have left per hour.
  664. *
  665. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  666. * @return Zend_Rest_Client_Result
  667. */
  668. public function accountRateLimitStatus ()
  669. {
  670. $this->_init();
  671. $response = $this->restGet('/account/rate_limit_status.xml');
  672. return new Zend_Rest_Client_Result($response->getBody());
  673. }
  674. /**
  675. * Fetch favorites
  676. *
  677. * $params may contain one or more of the following:
  678. * - 'id': Id of a user for whom to fetch favorites
  679. * - 'page': Retrieve a different page of resuls
  680. *
  681. * @param array $params
  682. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  683. * @return Zend_Rest_Client_Result
  684. */
  685. public function favoriteFavorites (array $params = array())
  686. {
  687. $this->_init();
  688. $path = '/favorites';
  689. $_params = array();
  690. foreach ($params as $key => $value) {
  691. switch (strtolower($key)) {
  692. case 'id':
  693. $path .= '/' . $this->_validInteger($value);
  694. break;
  695. case 'page':
  696. $_params['page'] = (int) $value;
  697. break;
  698. default:
  699. break;
  700. }
  701. }
  702. $path .= '.xml';
  703. $response = $this->restGet($path, $_params);
  704. return new Zend_Rest_Client_Result($response->getBody());
  705. }
  706. /**
  707. * Mark a status as a favorite
  708. *
  709. * @param int $id Status ID you want to mark as a favorite
  710. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  711. * @return Zend_Rest_Client_Result
  712. */
  713. public function favoriteCreate ($id)
  714. {
  715. $this->_init();
  716. $path = '/favorites/create/' . $this->_validInteger($id) . '.xml';
  717. $response = $this->restPost($path);
  718. return new Zend_Rest_Client_Result($response->getBody());
  719. }
  720. /**
  721. * Remove a favorite
  722. *
  723. * @param int $id Status ID you want to de-list as a favorite
  724. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  725. * @return Zend_Rest_Client_Result
  726. */
  727. public function favoriteDestroy ($id)
  728. {
  729. $this->_init();
  730. $path = '/favorites/destroy/' . $this->_validInteger($id) . '.xml';
  731. $response = $this->restPost($path);
  732. return new Zend_Rest_Client_Result($response->getBody());
  733. }
  734. /**
  735. * Protected function to validate that the integer is valid or return a 0
  736. * @param $int
  737. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  738. * @return integer
  739. */
  740. protected function _validInteger ($int)
  741. {
  742. if (preg_match("/(\d+)/", $int)) {
  743. return $int;
  744. }
  745. return 0;
  746. }
  747. }