Twitter.php 30 KB

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