Twitter.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  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. * Local HTTP Client cloned from statically set client
  75. * @var Zend_Http_Client
  76. */
  77. protected $_localHttpClient = null;
  78. /**
  79. * Constructor
  80. *
  81. * @param string $username
  82. * @param string $password
  83. * @return void
  84. */
  85. public function __construct ($username, $password = null)
  86. {
  87. $this->setLocalHttpClient(clone self::getHttpClient());
  88. if (is_array($username) && is_null($password)) {
  89. if (isset($username['username']) && isset($username['password'])) {
  90. $this->setUsername($username['username']);
  91. $this->setPassword($username['password']);
  92. } elseif (isset($username[0]) && isset($username[1])) {
  93. $this->setUsername($username[0]);
  94. $this->setPassword($username[1]);
  95. }
  96. } else {
  97. $this->setUsername($username);
  98. $this->setPassword($password);
  99. }
  100. $this->setUri('http://twitter.com');
  101. $this->_localHttpClient->setHeaders('Accept-Charset', 'ISO-8859-1,utf-8');
  102. }
  103. /**
  104. * Set local HTTP client as distinct from the static HTTP client
  105. * as inherited from Zend_Rest_Client.
  106. *
  107. * @param Zend_Http_Client $client
  108. * @return self
  109. */
  110. public function setLocalHttpClient(Zend_Http_Client $client)
  111. {
  112. $this->_localHttpClient = $client;
  113. return $this;
  114. }
  115. public function getLocalHttpClient()
  116. {
  117. return $this->_localHttpClient;
  118. }
  119. /**
  120. * Retrieve username
  121. *
  122. * @return string
  123. */
  124. public function getUsername ()
  125. {
  126. return $this->_username;
  127. }
  128. /**
  129. * Set username
  130. *
  131. * @param string $value
  132. * @return Zend_Service_Twitter
  133. */
  134. public function setUsername ($value)
  135. {
  136. $this->_username = $value;
  137. $this->_authInitialized = false;
  138. return $this;
  139. }
  140. /**
  141. * Retrieve password
  142. *
  143. * @return string
  144. */
  145. public function getPassword ()
  146. {
  147. return $this->_password;
  148. }
  149. /**
  150. * Set password
  151. *
  152. * @param string $value
  153. * @return Zend_Service_Twitter
  154. */
  155. public function setPassword ($value)
  156. {
  157. $this->_password = $value;
  158. $this->_authInitialized = false;
  159. return $this;
  160. }
  161. /**
  162. * Proxy service methods
  163. *
  164. * @param string $type
  165. * @return Zend_Service_Twitter
  166. * @throws Zend_Service_Twitter_Exception if method is not in method types list
  167. */
  168. public function __get ($type)
  169. {
  170. if (! in_array($type, $this->_methodTypes)) {
  171. include_once 'Zend/Service/Twitter/Exception.php';
  172. throw new Zend_Service_Twitter_Exception('Invalid method type "' . $type . '"');
  173. }
  174. $this->_methodType = $type;
  175. return $this;
  176. }
  177. /**
  178. * Method overloading
  179. *
  180. * @param string $method
  181. * @param array $params
  182. * @return mixed
  183. * @throws Zend_Service_Twitter_Exception if unable to find method
  184. */
  185. public function __call ($method, $params)
  186. {
  187. if (empty($this->_methodType)) {
  188. include_once 'Zend/Service/Twitter/Exception.php';
  189. throw new Zend_Service_Twitter_Exception('Invalid method "' . $method . '"');
  190. }
  191. $test = $this->_methodType . ucfirst($method);
  192. if (! method_exists($this, $test)) {
  193. include_once 'Zend/Service/Twitter/Exception.php';
  194. throw new Zend_Service_Twitter_Exception('Invalid method "' . $test . '"');
  195. }
  196. return call_user_func_array(array($this , $test), $params);
  197. }
  198. /**
  199. * Initialize HTTP authentication
  200. *
  201. * @return void
  202. */
  203. protected function _init ()
  204. {
  205. $client = $this->_localHttpClient;
  206. $client->resetParameters();
  207. if (null == $this->_cookieJar) {
  208. $client->setCookieJar();
  209. $this->_cookieJar = $client->getCookieJar();
  210. } else {
  211. $client->setCookieJar($this->_cookieJar);
  212. }
  213. if (! $this->_authInitialized) {
  214. $client->setAuth($this->getUsername(), $this->getPassword());
  215. $this->_authInitialized = true;
  216. }
  217. }
  218. /**
  219. * Set date header
  220. *
  221. * @param int|string $value
  222. * @deprecated Not supported by Twitter since April 08, 2009
  223. * @return void
  224. */
  225. protected function _setDate ($value)
  226. {
  227. if (is_int($value)) {
  228. $date = date($this->_dateFormat, $value);
  229. } else {
  230. $date = date($this->_dateFormat, strtotime($value));
  231. }
  232. $this->_localHttpClient->setHeaders('If-Modified-Since', $date);
  233. }
  234. /**
  235. * Public Timeline status
  236. *
  237. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  238. * @return Zend_Rest_Client_Result
  239. */
  240. public function statusPublicTimeline ()
  241. {
  242. $this->_init();
  243. $path = '/statuses/public_timeline.xml';
  244. $response = $this->_get($path);
  245. return new Zend_Rest_Client_Result($response->getBody());
  246. }
  247. /**
  248. * Friend Timeline Status
  249. *
  250. * $params may include one or more of the following keys
  251. * - id: ID of a friend whose timeline you wish to receive
  252. * - count: how many statuses to return
  253. * - since_id: return results only after the specific tweet
  254. * - page: return page X of results
  255. *
  256. * @param array $params
  257. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  258. * @return void
  259. */
  260. public function statusFriendsTimeline (array $params = array())
  261. {
  262. $this->_init();
  263. $path = '/statuses/friends_timeline';
  264. $_params = array();
  265. foreach ($params as $key => $value) {
  266. switch (strtolower($key)) {
  267. case 'count':
  268. $count = (int) $value;
  269. if (0 >= $count) {
  270. $count = 1;
  271. } elseif (200 < $count) {
  272. $count = 200;
  273. }
  274. $_params['count'] = (int) $count;
  275. break;
  276. case 'since_id':
  277. $_params['since_id'] = $this->_validInteger($value);
  278. break;
  279. case 'page':
  280. $_params['page'] = (int) $value;
  281. break;
  282. default:
  283. break;
  284. }
  285. }
  286. $path .= '.xml';
  287. $response = $this->_get($path, $_params);
  288. return new Zend_Rest_Client_Result($response->getBody());
  289. }
  290. /**
  291. * User Timeline status
  292. *
  293. * $params may include one or more of the following keys
  294. * - id: ID of a friend whose timeline you wish to receive
  295. * - since_id: return results only after the tweet id specified
  296. * - page: return page X of results
  297. * - count: how many statuses to return
  298. * - max_id: returns only statuses with an ID less than or equal to the specified ID
  299. * - user_id: specfies the ID of the user for whom to return the user_timeline
  300. * - screen_name: specfies the screen name of the user for whom to return the user_timeline
  301. *
  302. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  303. * @return Zend_Rest_Client_Result
  304. */
  305. public function statusUserTimeline (array $params = array())
  306. {
  307. $this->_init();
  308. $path = '/statuses/user_timeline';
  309. $_params = array();
  310. foreach ($params as $key => $value) {
  311. switch (strtolower($key)) {
  312. case 'id':
  313. $path .= '/' . $this->_validInteger($value);
  314. break;
  315. case 'page':
  316. $_params['page'] = (int) $value;
  317. break;
  318. case 'count':
  319. $count = (int) $value;
  320. if (0 >= $count) {
  321. $count = 1;
  322. } elseif (200 < $count) {
  323. $count = 200;
  324. }
  325. $_params['count'] = $count;
  326. break;
  327. case 'user_id':
  328. $_params['user_id'] = $this->_validInteger($value);
  329. break;
  330. case 'screen_name':
  331. $_params['screen_name'] = $this->_validateScreenName($value);
  332. break;
  333. case 'since_id':
  334. $_params['since_id'] = $this->_validInteger($value);
  335. break;
  336. case 'max_id':
  337. $_params['max_id'] = $this->_validInteger($value);
  338. break;
  339. default:
  340. break;
  341. }
  342. }
  343. $path .= '.xml';
  344. $response = $this->_get($path, $_params);
  345. return new Zend_Rest_Client_Result($response->getBody());
  346. }
  347. /**
  348. * Show a single status
  349. *
  350. * @param int $id Id of status to show
  351. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  352. * @return Zend_Rest_Client_Result
  353. */
  354. public function statusShow ($id)
  355. {
  356. $this->_init();
  357. $path = '/statuses/show/' . $this->_validInteger($id) . '.xml';
  358. $response = $this->_get($path);
  359. return new Zend_Rest_Client_Result($response->getBody());
  360. }
  361. /**
  362. * Update user's current status
  363. *
  364. * @param string $status
  365. * @param int $in_reply_to_status_id
  366. * @return Zend_Rest_Client_Result
  367. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  368. * @throws Zend_Service_Twitter_Exception if message is too short or too long
  369. */
  370. public function statusUpdate ($status, $in_reply_to_status_id = null)
  371. {
  372. $this->_init();
  373. $path = '/statuses/update.xml';
  374. $len = iconv_strlen($status, 'UTF-8');
  375. if ($len > 140) {
  376. include_once 'Zend/Service/Twitter/Exception.php';
  377. throw new Zend_Service_Twitter_Exception('Status must be no more than 140 characters in length');
  378. } elseif (0 == $len) {
  379. include_once 'Zend/Service/Twitter/Exception.php';
  380. throw new Zend_Service_Twitter_Exception('Status must contain at least one character');
  381. }
  382. $data = array('status' => $status);
  383. if (is_numeric($in_reply_to_status_id) && ! empty($in_reply_to_status_id)) {
  384. $data['in_reply_to_status_id'] = $in_reply_to_status_id;
  385. }
  386. //$this->status = $status;
  387. $response = $this->_post($path, $data);
  388. return new Zend_Rest_Client_Result($response->getBody());
  389. }
  390. /**
  391. * Get status replies
  392. *
  393. * $params may include one or more of the following keys
  394. * - since_id: return results only after the specified tweet id
  395. * - page: return page X of results
  396. *
  397. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  398. * @return Zend_Rest_Client_Result
  399. */
  400. public function statusReplies (array $params = array())
  401. {
  402. $this->_init();
  403. $path = '/statuses/replies.xml';
  404. $_params = array();
  405. foreach ($params as $key => $value) {
  406. switch (strtolower($key)) {
  407. case 'since_id':
  408. $_params['since_id'] = $this->_validInteger($value);
  409. break;
  410. case 'page':
  411. $_params['page'] = (int) $value;
  412. break;
  413. default:
  414. break;
  415. }
  416. }
  417. $response = $this->_get($path, $_params);
  418. return new Zend_Rest_Client_Result($response->getBody());
  419. }
  420. /**
  421. * Destroy a status message
  422. *
  423. * @param int $id ID of status to destroy
  424. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  425. * @return Zend_Rest_Client_Result
  426. */
  427. public function statusDestroy ($id)
  428. {
  429. $this->_init();
  430. $path = '/statuses/destroy/' . $this->_validInteger($id) . '.xml';
  431. $response = $this->_post($path);
  432. return new Zend_Rest_Client_Result($response->getBody());
  433. }
  434. /**
  435. * User friends
  436. *
  437. * @param int|string $id Id or username of user for whom to fetch friends
  438. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  439. * @return Zend_Rest_Client_Result
  440. */
  441. public function userFriends (array $params = array())
  442. {
  443. $this->_init();
  444. $path = '/statuses/friends';
  445. $_params = array();
  446. foreach ($params as $key => $value) {
  447. switch (strtolower($key)) {
  448. case 'id':
  449. $path .= '/' . $this->_validInteger($value);
  450. break;
  451. case 'page':
  452. $_params['page'] = (int) $value;
  453. break;
  454. default:
  455. break;
  456. }
  457. }
  458. $path .= '.xml';
  459. $response = $this->_get($path, $_params);
  460. return new Zend_Rest_Client_Result($response->getBody());
  461. }
  462. /**
  463. * User Followers
  464. *
  465. * @param bool $lite If true, prevents inline inclusion of current status for followers; defaults to false
  466. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  467. * @return Zend_Rest_Client_Result
  468. */
  469. public function userFollowers ($lite = false)
  470. {
  471. $this->_init();
  472. $path = '/statuses/followers.xml';
  473. if ($lite) {
  474. $this->lite = 'true';
  475. }
  476. $response = $this->_get($path);
  477. return new Zend_Rest_Client_Result($response->getBody());
  478. }
  479. /**
  480. * Get featured users
  481. *
  482. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  483. * @return Zend_Rest_Client_Result
  484. */
  485. public function userFeatured ()
  486. {
  487. $this->_init();
  488. $path = '/statuses/featured.xml';
  489. $response = $this->_get($path);
  490. return new Zend_Rest_Client_Result($response->getBody());
  491. }
  492. /**
  493. * Show extended information on a user
  494. *
  495. * @param int|string $id User ID or name
  496. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  497. * @return Zend_Rest_Client_Result
  498. */
  499. public function userShow ($id)
  500. {
  501. $this->_init();
  502. $path = '/users/show/' . $this->_validInteger($id) . '.xml';
  503. $response = $this->_get($path);
  504. return new Zend_Rest_Client_Result($response->getBody());
  505. }
  506. /**
  507. * Retrieve direct messages for the current user
  508. *
  509. * $params may include one or more of the following keys
  510. * - since_id: return statuses only greater than the one specified
  511. * - page: return page X of results
  512. *
  513. * @param array $params
  514. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  515. * @return Zend_Rest_Client_Result
  516. */
  517. public function directMessageMessages (array $params = array())
  518. {
  519. $this->_init();
  520. $path = '/direct_messages.xml';
  521. $_params = array();
  522. foreach ($params as $key => $value) {
  523. switch (strtolower($key)) {
  524. case 'since_id':
  525. $_params['since_id'] = $this->_validInteger($value);
  526. break;
  527. case 'page':
  528. $_params['page'] = (int) $value;
  529. break;
  530. default:
  531. break;
  532. }
  533. }
  534. $response = $this->_get($path, $_params);
  535. return new Zend_Rest_Client_Result($response->getBody());
  536. }
  537. /**
  538. * Retrieve list of direct messages sent by current user
  539. *
  540. * $params may include one or more of the following keys
  541. * - since_id: return statuses only greater than the one specified
  542. * - page: return page X of results
  543. *
  544. * @param array $params
  545. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  546. * @return Zend_Rest_Client_Result
  547. */
  548. public function directMessageSent (array $params = array())
  549. {
  550. $this->_init();
  551. $path = '/direct_messages/sent.xml';
  552. $_params = array();
  553. foreach ($params as $key => $value) {
  554. switch (strtolower($key)) {
  555. case 'since_id':
  556. $_params['since_id'] = $this->_validInteger($value);
  557. break;
  558. case 'page':
  559. $_params['page'] = (int) $value;
  560. break;
  561. default:
  562. break;
  563. }
  564. }
  565. $response = $this->_get($path, $_params);
  566. return new Zend_Rest_Client_Result($response->getBody());
  567. }
  568. /**
  569. * Send a direct message to a user
  570. *
  571. * @param int|string $user User to whom to send message
  572. * @param string $text Message to send to user
  573. * @return Zend_Rest_Client_Result
  574. * @throws Zend_Service_Twitter_Exception if message is too short or too long
  575. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  576. */
  577. public function directMessageNew ($user, $text)
  578. {
  579. $this->_init();
  580. $path = '/direct_messages/new.xml';
  581. $len = iconv_strlen($text, 'UTF-8');
  582. if (0 == $len) {
  583. throw new Zend_Service_Twitter_Exception('Direct message must contain at least one character');
  584. } elseif (140 < $len) {
  585. throw new Zend_Service_Twitter_Exception('Direct message must contain no more than 140 characters');
  586. }
  587. $data = array('user' => $user , 'text' => $text);
  588. $response = $this->_post($path, $data);
  589. return new Zend_Rest_Client_Result($response->getBody());
  590. }
  591. /**
  592. * Destroy a direct message
  593. *
  594. * @param int $id ID of message to destroy
  595. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  596. * @return Zend_Rest_Client_Result
  597. */
  598. public function directMessageDestroy ($id)
  599. {
  600. $this->_init();
  601. $path = '/direct_messages/destroy/' . $this->_validInteger($id) . '.xml';
  602. $response = $this->_post($path);
  603. return new Zend_Rest_Client_Result($response->getBody());
  604. }
  605. /**
  606. * Create friendship
  607. *
  608. * @param int|string $id User ID or name of new friend
  609. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  610. * @return Zend_Rest_Client_Result
  611. */
  612. public function friendshipCreate ($id)
  613. {
  614. $this->_init();
  615. $path = '/friendships/create/' . $this->_validInteger($id) . '.xml';
  616. $response = $this->_post($path);
  617. return new Zend_Rest_Client_Result($response->getBody());
  618. }
  619. /**
  620. * Destroy friendship
  621. *
  622. * @param int|string $id User ID or name of friend to remove
  623. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  624. * @return Zend_Rest_Client_Result
  625. */
  626. public function friendshipDestroy ($id)
  627. {
  628. $this->_init();
  629. $path = '/friendships/destroy/' . $this->_validInteger($id) . '.xml';
  630. $response = $this->_post($path);
  631. return new Zend_Rest_Client_Result($response->getBody());
  632. }
  633. /**
  634. * Friendship exists
  635. *
  636. * @param int|string $id User ID or name of friend to see if they are your friend
  637. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  638. * @return Zend_Rest_Client_result
  639. */
  640. public function friendshipExists ($id)
  641. {
  642. $this->_init();
  643. $path = '/friendships/exists.xml';
  644. $data = array('user_a' => $this->getUsername() , 'user_b' => $this->_validInteger($id));
  645. $response = $this->_get($path, $data);
  646. return new Zend_Rest_Client_Result($response->getBody());
  647. }
  648. /**
  649. * Verify Account Credentials
  650. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  651. *
  652. * @return Zend_Rest_Client_Result
  653. */
  654. public function accountVerifyCredentials ()
  655. {
  656. $this->_init();
  657. $response = $this->_get('/account/verify_credentials.xml');
  658. return new Zend_Rest_Client_Result($response->getBody());
  659. }
  660. /**
  661. * End current session
  662. *
  663. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  664. * @return true
  665. */
  666. public function accountEndSession ()
  667. {
  668. $this->_init();
  669. $this->_get('/account/end_session');
  670. return true;
  671. }
  672. /**
  673. * Returns the number of api requests you have left per hour.
  674. *
  675. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  676. * @return Zend_Rest_Client_Result
  677. */
  678. public function accountRateLimitStatus ()
  679. {
  680. $this->_init();
  681. $response = $this->_get('/account/rate_limit_status.xml');
  682. return new Zend_Rest_Client_Result($response->getBody());
  683. }
  684. /**
  685. * Fetch favorites
  686. *
  687. * $params may contain one or more of the following:
  688. * - 'id': Id of a user for whom to fetch favorites
  689. * - 'page': Retrieve a different page of resuls
  690. *
  691. * @param array $params
  692. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  693. * @return Zend_Rest_Client_Result
  694. */
  695. public function favoriteFavorites (array $params = array())
  696. {
  697. $this->_init();
  698. $path = '/favorites';
  699. $_params = array();
  700. foreach ($params as $key => $value) {
  701. switch (strtolower($key)) {
  702. case 'id':
  703. $path .= '/' . $this->_validInteger($value);
  704. break;
  705. case 'page':
  706. $_params['page'] = (int) $value;
  707. break;
  708. default:
  709. break;
  710. }
  711. }
  712. $path .= '.xml';
  713. $response = $this->_get($path, $_params);
  714. return new Zend_Rest_Client_Result($response->getBody());
  715. }
  716. /**
  717. * Mark a status as a favorite
  718. *
  719. * @param int $id Status ID you want to mark as a favorite
  720. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  721. * @return Zend_Rest_Client_Result
  722. */
  723. public function favoriteCreate ($id)
  724. {
  725. $this->_init();
  726. $path = '/favorites/create/' . $this->_validInteger($id) . '.xml';
  727. $response = $this->_post($path);
  728. return new Zend_Rest_Client_Result($response->getBody());
  729. }
  730. /**
  731. * Remove a favorite
  732. *
  733. * @param int $id Status ID you want to de-list as a favorite
  734. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  735. * @return Zend_Rest_Client_Result
  736. */
  737. public function favoriteDestroy ($id)
  738. {
  739. $this->_init();
  740. $path = '/favorites/destroy/' . $this->_validInteger($id) . '.xml';
  741. $response = $this->_post($path);
  742. return new Zend_Rest_Client_Result($response->getBody());
  743. }
  744. /**
  745. * Protected function to validate that the integer is valid or return a 0
  746. * @param $int
  747. * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  748. * @return integer
  749. */
  750. protected function _validInteger ($int)
  751. {
  752. if (preg_match("/(\d+)/", $int)) {
  753. return $int;
  754. }
  755. return 0;
  756. }
  757. /**
  758. * Validate a screen name using Twitter rules
  759. *
  760. * @param string $name
  761. * @throws Zend_Service_Twitter_Exception
  762. * @return string
  763. */
  764. protected function _validateScreenName($name)
  765. {
  766. if (!preg_match('/^[a-z0-9_]{0,20}$/', $name)) {
  767. require_once 'Zend/Service/Twitter/Exception.php';
  768. throw new Zend_Service_Twitter_Exception('Screen name, "'
  769. . $name . '" should only contain alphanumeric characters and'
  770. . ' underscores, and not exceed 15 characters.');
  771. }
  772. return $name;
  773. }
  774. /**
  775. * Call a remote REST web service URI and return the Zend_Http_Response object
  776. *
  777. * @param string $path The path to append to the URI
  778. * @throws Zend_Rest_Client_Exception
  779. * @return void
  780. */
  781. protected function _prepare($path)
  782. {
  783. // Get the URI object and configure it
  784. if (!$this->_uri instanceof Zend_Uri_Http) {
  785. require_once 'Zend/Rest/Client/Exception.php';
  786. throw new Zend_Rest_Client_Exception('URI object must be set before performing call');
  787. }
  788. $uri = $this->_uri->getUri();
  789. if ($path[0] != '/' && $uri[strlen($uri)-1] != '/') {
  790. $path = '/' . $path;
  791. }
  792. $this->_uri->setPath($path);
  793. /**
  794. * Get the HTTP client and configure it for the endpoint URI. Do this each time
  795. * because the Zend_Http_Client instance is shared among all Zend_Service_Abstract subclasses.
  796. */
  797. $this->_localHttpClient->resetParameters()->setUri($this->_uri);
  798. }
  799. /**
  800. * Performs an HTTP GET request to the $path.
  801. *
  802. * @param string $path
  803. * @param array $query Array of GET parameters
  804. * @throws Zend_Http_Client_Exception
  805. * @return Zend_Http_Response
  806. */
  807. protected function _get($path, array $query = null)
  808. {
  809. $this->_prepare($path);
  810. $this->_localHttpClient->setParameterGet($query);
  811. return $this->_localHttpClient->request('GET');
  812. }
  813. /**
  814. * Performs an HTTP POST request to $path.
  815. *
  816. * @param string $path
  817. * @param mixed $data Raw data to send
  818. * @throws Zend_Http_Client_Exception
  819. * @return Zend_Http_Response
  820. */
  821. protected function _post($path, $data = null)
  822. {
  823. $this->_prepare($path);
  824. return $this->_performPost('POST', $data);
  825. }
  826. /**
  827. * Perform a POST or PUT
  828. *
  829. * Performs a POST or PUT request. Any data provided is set in the HTTP
  830. * client. String data is pushed in as raw POST data; array or object data
  831. * is pushed in as POST parameters.
  832. *
  833. * @param mixed $method
  834. * @param mixed $data
  835. * @return Zend_Http_Response
  836. */
  837. protected function _performPost($method, $data = null)
  838. {
  839. $client = $this->_localHttpClient;
  840. if (is_string($data)) {
  841. $client->setRawData($data);
  842. } elseif (is_array($data) || is_object($data)) {
  843. $client->setParameterPost((array) $data);
  844. }
  845. return $client->request($method);
  846. }
  847. }