Twitter.php 31 KB

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