Twitter.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  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. * @deprecated Not supported by Twitter since April 08, 2009
  202. * @return void
  203. */
  204. protected function _setDate ($value)
  205. {
  206. if (is_int($value)) {
  207. $date = date($this->_dateFormat, $value);
  208. } else {
  209. $date = date($this->_dateFormat, strtotime($value));
  210. }
  211. self::getHttpClient()->setHeaders('If-Modified-Since', $date);
  212. }
  213. /**
  214. * Public Timeline status
  215. *
  216. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  217. * @return Zend_Rest_Client_Result
  218. */
  219. public function statusPublicTimeline ()
  220. {
  221. $this->_init();
  222. $path = '/statuses/public_timeline.xml';
  223. $response = $this->restGet($path);
  224. return new Zend_Rest_Client_Result($response->getBody());
  225. }
  226. /**
  227. * Friend Timeline Status
  228. *
  229. * $params may include one or more of the following keys
  230. * - id: ID of a friend whose timeline you wish to receive
  231. * - count: how many statuses to return
  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 'page':
  259. $_params['page'] = (int) $value;
  260. break;
  261. default:
  262. break;
  263. }
  264. }
  265. $path .= '.xml';
  266. $response = $this->restGet($path, $_params);
  267. return new Zend_Rest_Client_Result($response->getBody());
  268. }
  269. /**
  270. * User Timeline status
  271. *
  272. * $params may include one or more of the following keys
  273. * - id: ID of a friend whose timeline you wish to receive
  274. * - since_id: return results only after the tweet id specified
  275. * - page: return page X of results
  276. * - count: how many statuses to return
  277. * - max_id: returns only statuses with an ID less than or equal to the specified ID
  278. * - user_id: specfies the ID of the user for whom to return the user_timeline
  279. * - screen_name: specfies the screen name of the user for whom to return the user_timeline
  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. $_params['user_id'] = $this->_validInteger($value);
  308. break;
  309. case 'screen_name':
  310. $_params['screen_name'] = $this->_validateScreenName($value);
  311. break;
  312. case 'since_id':
  313. $_params['since_id'] = $this->_validInteger($value);
  314. break;
  315. case 'max_id':
  316. $_params['max_id'] = $this->_validInteger($value);
  317. break;
  318. default:
  319. break;
  320. }
  321. }
  322. $path .= '.xml';
  323. $response = $this->restGet($path, $_params);
  324. return new Zend_Rest_Client_Result($response->getBody());
  325. }
  326. /**
  327. * Show a single status
  328. *
  329. * @param int $id Id of status to show
  330. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  331. * @return Zend_Rest_Client_Result
  332. */
  333. public function statusShow ($id)
  334. {
  335. $this->_init();
  336. $path = '/statuses/show/' . $this->_validInteger($id) . '.xml';
  337. $response = $this->restGet($path);
  338. return new Zend_Rest_Client_Result($response->getBody());
  339. }
  340. /**
  341. * Update user's current status
  342. *
  343. * @param string $status
  344. * @param int $in_reply_to_status_id
  345. * @return Zend_Rest_Client_Result
  346. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  347. * @throws Zend_Service_Twitter_Exception if message is too short or too long
  348. */
  349. public function statusUpdate ($status, $in_reply_to_status_id = null)
  350. {
  351. $this->_init();
  352. $path = '/statuses/update.xml';
  353. $len = iconv_strlen($status, 'UTF-8');
  354. if ($len > 140) {
  355. include_once 'Zend/Service/Twitter/Exception.php';
  356. throw new Zend_Service_Twitter_Exception('Status must be no more than 140 characters in length');
  357. } elseif (0 == $len) {
  358. include_once 'Zend/Service/Twitter/Exception.php';
  359. throw new Zend_Service_Twitter_Exception('Status must contain at least one character');
  360. }
  361. $data = array('status' => $status);
  362. if (is_numeric($in_reply_to_status_id) && ! empty($in_reply_to_status_id)) {
  363. $data['in_reply_to_status_id'] = $in_reply_to_status_id;
  364. }
  365. //$this->status = $status;
  366. $response = $this->restPost($path, $data);
  367. return new Zend_Rest_Client_Result($response->getBody());
  368. }
  369. /**
  370. * Get status replies
  371. *
  372. * $params may include one or more of the following keys
  373. * - since_id: return results only after the specified tweet id
  374. * - page: return page X of results
  375. *
  376. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  377. * @return Zend_Rest_Client_Result
  378. */
  379. public function statusReplies (array $params = array())
  380. {
  381. $this->_init();
  382. $path = '/statuses/replies.xml';
  383. $_params = array();
  384. foreach ($params as $key => $value) {
  385. switch (strtolower($key)) {
  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 'page':
  431. $_params['page'] = (int) $value;
  432. break;
  433. default:
  434. break;
  435. }
  436. }
  437. $path .= '.xml';
  438. $response = $this->restGet($path, $_params);
  439. return new Zend_Rest_Client_Result($response->getBody());
  440. }
  441. /**
  442. * User Followers
  443. *
  444. * @param bool $lite If true, prevents inline inclusion of current status for followers; defaults to false
  445. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  446. * @return Zend_Rest_Client_Result
  447. */
  448. public function userFollowers ($lite = false)
  449. {
  450. $this->_init();
  451. $path = '/statuses/followers.xml';
  452. if ($lite) {
  453. $this->lite = 'true';
  454. }
  455. $response = $this->restGet($path);
  456. return new Zend_Rest_Client_Result($response->getBody());
  457. }
  458. /**
  459. * Get featured users
  460. *
  461. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  462. * @return Zend_Rest_Client_Result
  463. */
  464. public function userFeatured ()
  465. {
  466. $this->_init();
  467. $path = '/statuses/featured.xml';
  468. $response = $this->restGet($path);
  469. return new Zend_Rest_Client_Result($response->getBody());
  470. }
  471. /**
  472. * Show extended information on a user
  473. *
  474. * @param int|string $id User ID or name
  475. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  476. * @return Zend_Rest_Client_Result
  477. */
  478. public function userShow ($id)
  479. {
  480. $this->_init();
  481. $path = '/users/show/' . $this->_validInteger($id) . '.xml';
  482. $response = $this->restGet($path);
  483. return new Zend_Rest_Client_Result($response->getBody());
  484. }
  485. /**
  486. * Retrieve direct messages for the current user
  487. *
  488. * $params may include one or more of the following keys
  489. * - since_id: return statuses only greater than the one specified
  490. * - page: return page X of results
  491. *
  492. * @param array $params
  493. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  494. * @return Zend_Rest_Client_Result
  495. */
  496. public function directMessageMessages (array $params = array())
  497. {
  498. $this->_init();
  499. $path = '/direct_messages.xml';
  500. $_params = array();
  501. foreach ($params as $key => $value) {
  502. switch (strtolower($key)) {
  503. case 'since_id':
  504. $_params['since_id'] = $this->_validInteger($value);
  505. break;
  506. case 'page':
  507. $_params['page'] = (int) $value;
  508. break;
  509. default:
  510. break;
  511. }
  512. }
  513. $response = $this->restGet($path, $_params);
  514. return new Zend_Rest_Client_Result($response->getBody());
  515. }
  516. /**
  517. * Retrieve list of direct messages sent by current user
  518. *
  519. * $params may include one or more of the following keys
  520. * - since_id: return statuses only greater than the one specified
  521. * - page: return page X of results
  522. *
  523. * @param array $params
  524. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  525. * @return Zend_Rest_Client_Result
  526. */
  527. public function directMessageSent (array $params = array())
  528. {
  529. $this->_init();
  530. $path = '/direct_messages/sent.xml';
  531. $_params = array();
  532. foreach ($params as $key => $value) {
  533. switch (strtolower($key)) {
  534. case 'since_id':
  535. $_params['since_id'] = $this->_validInteger($value);
  536. break;
  537. case 'page':
  538. $_params['page'] = (int) $value;
  539. break;
  540. default:
  541. break;
  542. }
  543. }
  544. $response = $this->restGet($path, $_params);
  545. return new Zend_Rest_Client_Result($response->getBody());
  546. }
  547. /**
  548. * Send a direct message to a user
  549. *
  550. * @param int|string $user User to whom to send message
  551. * @param string $text Message to send to user
  552. * @return Zend_Rest_Client_Result
  553. * @throws Zend_Service_Twitter_Exception if message is too short or too long
  554. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  555. */
  556. public function directMessageNew ($user, $text)
  557. {
  558. $this->_init();
  559. $path = '/direct_messages/new.xml';
  560. $len = iconv_strlen($text, 'UTF-8');
  561. if (0 == $len) {
  562. throw new Zend_Service_Twitter_Exception('Direct message must contain at least one character');
  563. } elseif (140 < $len) {
  564. throw new Zend_Service_Twitter_Exception('Direct message must contain no more than 140 characters');
  565. }
  566. $data = array('user' => $user , 'text' => $text);
  567. $response = $this->restPost($path, $data);
  568. return new Zend_Rest_Client_Result($response->getBody());
  569. }
  570. /**
  571. * Destroy a direct message
  572. *
  573. * @param int $id ID of message to destroy
  574. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  575. * @return Zend_Rest_Client_Result
  576. */
  577. public function directMessageDestroy ($id)
  578. {
  579. $this->_init();
  580. $path = '/direct_messages/destroy/' . $this->_validInteger($id) . '.xml';
  581. $response = $this->restPost($path);
  582. return new Zend_Rest_Client_Result($response->getBody());
  583. }
  584. /**
  585. * Create friendship
  586. *
  587. * @param int|string $id User ID or name of new friend
  588. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  589. * @return Zend_Rest_Client_Result
  590. */
  591. public function friendshipCreate ($id)
  592. {
  593. $this->_init();
  594. $path = '/friendships/create/' . $this->_validInteger($id) . '.xml';
  595. $response = $this->restPost($path);
  596. return new Zend_Rest_Client_Result($response->getBody());
  597. }
  598. /**
  599. * Destroy friendship
  600. *
  601. * @param int|string $id User ID or name of friend to remove
  602. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  603. * @return Zend_Rest_Client_Result
  604. */
  605. public function friendshipDestroy ($id)
  606. {
  607. $this->_init();
  608. $path = '/friendships/destroy/' . $this->_validInteger($id) . '.xml';
  609. $response = $this->restPost($path);
  610. return new Zend_Rest_Client_Result($response->getBody());
  611. }
  612. /**
  613. * Friendship exists
  614. *
  615. * @param int|string $id User ID or name of friend to see if they are your friend
  616. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  617. * @return Zend_Rest_Client_result
  618. */
  619. public function friendshipExists ($id)
  620. {
  621. $this->_init();
  622. $path = '/friendships/exists.xml';
  623. $data = array('user_a' => $this->getUsername() , 'user_b' => $this->_validInteger($id));
  624. $response = $this->restGet($path, $data);
  625. return new Zend_Rest_Client_Result($response->getBody());
  626. }
  627. /**
  628. * Verify Account Credentials
  629. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  630. *
  631. * @return Zend_Rest_Client_Result
  632. */
  633. public function accountVerifyCredentials ()
  634. {
  635. $this->_init();
  636. $response = $this->restGet('/account/verify_credentials.xml');
  637. return new Zend_Rest_Client_Result($response->getBody());
  638. }
  639. /**
  640. * End current session
  641. *
  642. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  643. * @return true
  644. */
  645. public function accountEndSession ()
  646. {
  647. $this->_init();
  648. $this->restGet('/account/end_session');
  649. return true;
  650. }
  651. /**
  652. * Returns the number of api requests you have left per hour.
  653. *
  654. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  655. * @return Zend_Rest_Client_Result
  656. */
  657. public function accountRateLimitStatus ()
  658. {
  659. $this->_init();
  660. $response = $this->restGet('/account/rate_limit_status.xml');
  661. return new Zend_Rest_Client_Result($response->getBody());
  662. }
  663. /**
  664. * Fetch favorites
  665. *
  666. * $params may contain one or more of the following:
  667. * - 'id': Id of a user for whom to fetch favorites
  668. * - 'page': Retrieve a different page of resuls
  669. *
  670. * @param array $params
  671. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  672. * @return Zend_Rest_Client_Result
  673. */
  674. public function favoriteFavorites (array $params = array())
  675. {
  676. $this->_init();
  677. $path = '/favorites';
  678. $_params = array();
  679. foreach ($params as $key => $value) {
  680. switch (strtolower($key)) {
  681. case 'id':
  682. $path .= '/' . $this->_validInteger($value);
  683. break;
  684. case 'page':
  685. $_params['page'] = (int) $value;
  686. break;
  687. default:
  688. break;
  689. }
  690. }
  691. $path .= '.xml';
  692. $response = $this->restGet($path, $_params);
  693. return new Zend_Rest_Client_Result($response->getBody());
  694. }
  695. /**
  696. * Mark a status as a favorite
  697. *
  698. * @param int $id Status ID you want to mark as a favorite
  699. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  700. * @return Zend_Rest_Client_Result
  701. */
  702. public function favoriteCreate ($id)
  703. {
  704. $this->_init();
  705. $path = '/favorites/create/' . $this->_validInteger($id) . '.xml';
  706. $response = $this->restPost($path);
  707. return new Zend_Rest_Client_Result($response->getBody());
  708. }
  709. /**
  710. * Remove a favorite
  711. *
  712. * @param int $id Status ID you want to de-list as a favorite
  713. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  714. * @return Zend_Rest_Client_Result
  715. */
  716. public function favoriteDestroy ($id)
  717. {
  718. $this->_init();
  719. $path = '/favorites/destroy/' . $this->_validInteger($id) . '.xml';
  720. $response = $this->restPost($path);
  721. return new Zend_Rest_Client_Result($response->getBody());
  722. }
  723. /**
  724. * Protected function to validate that the integer is valid or return a 0
  725. * @param $int
  726. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  727. * @return integer
  728. */
  729. protected function _validInteger ($int)
  730. {
  731. if (preg_match("/(\d+)/", $int)) {
  732. return $int;
  733. }
  734. return 0;
  735. }
  736. /**
  737. * Validate a screen name using Twitter rules
  738. *
  739. * @param string $name
  740. * @throws Zend_Service_Twitter_Exception
  741. * @return string
  742. */
  743. protected function _validateScreenName($name)
  744. {
  745. if (!preg_match('/^[a-z0-9_]{0,20}$/', $name)) {
  746. require_once 'Zend/Service/Twitter/Exception.php';
  747. throw new Zend_Service_Twitter_Exception('Screen name, "'
  748. . $name . '" should only contain alphanumeric characters and'
  749. . ' underscores, and not exceed 15 characters.');
  750. }
  751. return $name;
  752. }
  753. }