Twitter.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Service
  17. * @subpackage Twitter
  18. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @see Zend_Rest_Client
  24. */
  25. require_once 'Zend/Rest/Client.php';
  26. /**
  27. * @see Zend_Rest_Client_Result
  28. */
  29. require_once 'Zend/Rest/Client/Result.php';
  30. /**
  31. * @category Zend
  32. * @package Zend_Service
  33. * @subpackage Twitter
  34. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. */
  37. class Zend_Service_Twitter extends Zend_Rest_Client
  38. {
  39. /**
  40. * Whether or not authorization has been initialized for the current user.
  41. * @var bool
  42. */
  43. protected $_authInitialized = false;
  44. /**
  45. * @var Zend_Http_CookieJar
  46. */
  47. protected $_cookieJar;
  48. /**
  49. * Date format for 'since' strings
  50. * @var string
  51. */
  52. protected $_dateFormat = 'D, d M Y H:i:s T';
  53. /**
  54. * Username
  55. * @var string
  56. */
  57. protected $_username;
  58. /**
  59. * Password
  60. * @var string
  61. */
  62. protected $_password;
  63. /**
  64. * Current method type (for method proxying)
  65. * @var string
  66. */
  67. protected $_methodType;
  68. /**
  69. * Types of API methods
  70. * @var array
  71. */
  72. protected $_methodTypes = array('status' , 'user' , 'directMessage' , 'friendship' , 'account' , 'favorite');
  73. /**
  74. * Constructor
  75. *
  76. * @param string $username
  77. * @param string $password
  78. * @return void
  79. */
  80. public function __construct ($username, $password = null)
  81. {
  82. if (is_array($username) && is_null($password)) {
  83. if (isset($username['username']) && isset($username['password'])) {
  84. $this->setUsername($username['username']);
  85. $this->setPassword($username['password']);
  86. } elseif (isset($username[0]) && isset($username[1])) {
  87. $this->setUsername($username[0]);
  88. $this->setPassword($username[1]);
  89. }
  90. } else {
  91. $this->setUsername($username);
  92. $this->setPassword($password);
  93. }
  94. $this->setUri('http://twitter.com');
  95. $client = self::getHttpClient();
  96. $client->setHeaders('Accept-Charset', 'ISO-8859-1,utf-8');
  97. }
  98. /**
  99. * Retrieve username
  100. *
  101. * @return string
  102. */
  103. public function getUsername ()
  104. {
  105. return $this->_username;
  106. }
  107. /**
  108. * Set username
  109. *
  110. * @param string $value
  111. * @return Zend_Service_Twitter
  112. */
  113. public function setUsername ($value)
  114. {
  115. $this->_username = $value;
  116. $this->_authInitialized = false;
  117. return $this;
  118. }
  119. /**
  120. * Retrieve password
  121. *
  122. * @return string
  123. */
  124. public function getPassword ()
  125. {
  126. return $this->_password;
  127. }
  128. /**
  129. * Set password
  130. *
  131. * @param string $value
  132. * @return Zend_Service_Twitter
  133. */
  134. public function setPassword ($value)
  135. {
  136. $this->_password = $value;
  137. $this->_authInitialized = false;
  138. return $this;
  139. }
  140. /**
  141. * Proxy service methods
  142. *
  143. * @param string $type
  144. * @return Zend_Service_Twitter
  145. * @throws Zend_Service_Twitter_Exception if method is not in method types list
  146. */
  147. public function __get ($type)
  148. {
  149. if (! in_array($type, $this->_methodTypes)) {
  150. include_once 'Zend/Service/Twitter/Exception.php';
  151. throw new Zend_Service_Twitter_Exception('Invalid method type "' . $type . '"');
  152. }
  153. $this->_methodType = $type;
  154. return $this;
  155. }
  156. /**
  157. * Method overloading
  158. *
  159. * @param string $method
  160. * @param array $params
  161. * @return mixed
  162. * @throws Zend_Service_Twitter_Exception if unable to find method
  163. */
  164. public function __call ($method, $params)
  165. {
  166. if (empty($this->_methodType)) {
  167. include_once 'Zend/Service/Twitter/Exception.php';
  168. throw new Zend_Service_Twitter_Exception('Invalid method "' . $method . '"');
  169. }
  170. $test = $this->_methodType . ucfirst($method);
  171. if (! method_exists($this, $test)) {
  172. include_once 'Zend/Service/Twitter/Exception.php';
  173. throw new Zend_Service_Twitter_Exception('Invalid method "' . $test . '"');
  174. }
  175. return call_user_func_array(array($this , $test), $params);
  176. }
  177. /**
  178. * Initialize HTTP authentication
  179. *
  180. * @return void
  181. */
  182. protected function _init ()
  183. {
  184. $client = self::getHttpClient();
  185. $client->resetParameters();
  186. if (null == $this->_cookieJar) {
  187. $client->setCookieJar();
  188. $this->_cookieJar = $client->getCookieJar();
  189. } else {
  190. $client->setCookieJar($this->_cookieJar);
  191. }
  192. if (! $this->_authInitialized) {
  193. $client->setAuth($this->getUsername(), $this->getPassword());
  194. $this->_authInitialized = true;
  195. }
  196. }
  197. /**
  198. * Set date header
  199. *
  200. * @param int|string $value
  201. * @return void
  202. */
  203. protected function _setDate ($value)
  204. {
  205. if (is_int($value)) {
  206. $date = date($this->_dateFormat, $value);
  207. } else {
  208. $date = date($this->_dateFormat, strtotime($value));
  209. }
  210. self::getHttpClient()->setHeaders('If-Modified-Since', $date);
  211. }
  212. /**
  213. * Public Timeline status
  214. *
  215. * @return Zend_Rest_Client_Result
  216. */
  217. public function statusPublicTimeline ()
  218. {
  219. $this->_init();
  220. $path = '/statuses/public_timeline.xml';
  221. $response = $this->restGet($path);
  222. return new Zend_Rest_Client_Result($response->getBody());
  223. }
  224. /**
  225. * Friend Timeline Status
  226. *
  227. * $params may include one or more of the following keys
  228. * - id: ID of a friend whose timeline you wish to receive
  229. * - count: how many statuses to return
  230. * - since: return results only after the date specified
  231. * - since_id: return results only after the specific tweet
  232. * - page: return page X of results
  233. *
  234. * @param array $params
  235. * @return void
  236. */
  237. public function statusFriendsTimeline (array $params = array())
  238. {
  239. $this->_init();
  240. $path = '/statuses/friends_timeline';
  241. $_params = array();
  242. foreach ($params as $key => $value) {
  243. switch (strtolower($key)) {
  244. case 'count':
  245. $count = (int) $value;
  246. if (0 >= $count) {
  247. $count = 1;
  248. } elseif (200 < $count) {
  249. $count = 200;
  250. }
  251. $_params['count'] = (int) $count;
  252. break;
  253. case 'since_id':
  254. $_params['since_id'] = $this->_validInteger($value);
  255. break;
  256. case 'since':
  257. $this->_setDate($value);
  258. break;
  259. case 'page':
  260. $_params['page'] = (int) $value;
  261. break;
  262. default:
  263. break;
  264. }
  265. }
  266. $path .= '.xml';
  267. $response = $this->restGet($path, $_params);
  268. return new Zend_Rest_Client_Result($response->getBody());
  269. }
  270. /**
  271. * User Timeline status
  272. *
  273. * $params may include one or more of the following keys
  274. * - id: ID of a friend whose timeline you wish to receive
  275. * - since: return results only after the date specified
  276. * - page: return page X of results
  277. * - count: how many statuses to return
  278. *
  279. * @return Zend_Rest_Client_Result
  280. */
  281. public function statusUserTimeline (array $params = array())
  282. {
  283. $this->_init();
  284. $path = '/statuses/user_timeline';
  285. $_params = array();
  286. foreach ($params as $key => $value) {
  287. switch (strtolower($key)) {
  288. case 'id':
  289. $path .= '/' . $this->_validInteger($value);
  290. break;
  291. case 'since':
  292. $this->_setDate($value);
  293. break;
  294. case 'page':
  295. $_params['page'] = (int) $value;
  296. break;
  297. case 'count':
  298. $count = (int) $value;
  299. if (0 >= $count) {
  300. $count = 1;
  301. } elseif (200 < $count) {
  302. $count = 200;
  303. }
  304. $_params['count'] = $count;
  305. break;
  306. default:
  307. break;
  308. }
  309. }
  310. $path .= '.xml';
  311. $response = $this->restGet($path, $_params);
  312. return new Zend_Rest_Client_Result($response->getBody());
  313. }
  314. /**
  315. * Show a single status
  316. *
  317. * @param int $id Id of status to show
  318. * @return Zend_Rest_Client_Result
  319. */
  320. public function statusShow ($id)
  321. {
  322. $this->_init();
  323. $path = '/statuses/show/' . $this->_validInteger($id) . '.xml';
  324. $response = $this->restGet($path);
  325. return new Zend_Rest_Client_Result($response->getBody());
  326. }
  327. /**
  328. * Update user's current status
  329. *
  330. * @param string $status
  331. * @param int $in_reply_to_status_id
  332. * @return Zend_Rest_Client_Result
  333. * @throws Zend_Service_Twitter_Exception if message is too short or too long
  334. */
  335. public function statusUpdate ($status, $in_reply_to_status_id = null)
  336. {
  337. $this->_init();
  338. $path = '/statuses/update.xml';
  339. $len = iconv_strlen($status, 'UTF-8');
  340. if ($len > 140) {
  341. include_once 'Zend/Service/Twitter/Exception.php';
  342. throw new Zend_Service_Twitter_Exception('Status must be no more than 140 characters in length');
  343. } elseif (0 == $len) {
  344. include_once 'Zend/Service/Twitter/Exception.php';
  345. throw new Zend_Service_Twitter_Exception('Status must contain at least one character');
  346. }
  347. $data = array('status' => $status);
  348. if (is_numeric($in_reply_to_status_id) && ! empty($in_reply_to_status_id)) {
  349. $data['in_reply_to_status_id'] = $in_reply_to_status_id;
  350. }
  351. //$this->status = $status;
  352. $response = $this->restPost($path, $data);
  353. return new Zend_Rest_Client_Result($response->getBody());
  354. }
  355. /**
  356. * Get status replies
  357. *
  358. * $params may include one or more of the following keys
  359. * - since: return results only after the date specified
  360. * - since_id: return results only after the specified tweet id
  361. * - page: return page X of results
  362. *
  363. * @return Zend_Rest_Client_Result
  364. */
  365. public function statusReplies (array $params = array())
  366. {
  367. $this->_init();
  368. $path = '/statuses/replies.xml';
  369. $_params = array();
  370. foreach ($params as $key => $value) {
  371. switch (strtolower($key)) {
  372. case 'since':
  373. $this->_setDate($value);
  374. break;
  375. case 'since_id':
  376. $_params['since_id'] = $this->_validInteger($value);
  377. break;
  378. case 'page':
  379. $_params['page'] = (int) $value;
  380. break;
  381. default:
  382. break;
  383. }
  384. }
  385. $response = $this->restGet($path, $_params);
  386. return new Zend_Rest_Client_Result($response->getBody());
  387. }
  388. /**
  389. * Destroy a status message
  390. *
  391. * @param int $id ID of status to destroy
  392. * @return Zend_Rest_Client_Result
  393. */
  394. public function statusDestroy ($id)
  395. {
  396. $this->_init();
  397. $path = '/statuses/destroy/' . $this->_validInteger($id) . '.xml';
  398. $response = $this->restPost($path);
  399. return new Zend_Rest_Client_Result($response->getBody());
  400. }
  401. /**
  402. * User friends
  403. *
  404. * @param int|string $id Id or username of user for whom to fetch friends
  405. * @return Zend_Rest_Client_Result
  406. */
  407. public function userFriends (array $params = array())
  408. {
  409. $this->_init();
  410. $path = '/statuses/friends';
  411. $_params = array();
  412. foreach ($params as $key => $value) {
  413. switch (strtolower($key)) {
  414. case 'id':
  415. $path .= '/' . $this->_validInteger($value);
  416. break;
  417. case 'since':
  418. $this->_setDate($value);
  419. break;
  420. case 'page':
  421. $_params['page'] = (int) $value;
  422. break;
  423. default:
  424. break;
  425. }
  426. }
  427. $path .= '.xml';
  428. $response = $this->restGet($path, $_params);
  429. return new Zend_Rest_Client_Result($response->getBody());
  430. }
  431. /**
  432. * User Followers
  433. *
  434. * @param bool $lite If true, prevents inline inclusion of current status for followers; defaults to false
  435. * @return Zend_Rest_Client_Result
  436. */
  437. public function userFollowers ($lite = false)
  438. {
  439. $this->_init();
  440. $path = '/statuses/followers.xml';
  441. if ($lite) {
  442. $this->lite = 'true';
  443. }
  444. $response = $this->restGet($path);
  445. return new Zend_Rest_Client_Result($response->getBody());
  446. }
  447. /**
  448. * Get featured users
  449. *
  450. * @return Zend_Rest_Client_Result
  451. */
  452. public function userFeatured ()
  453. {
  454. $this->_init();
  455. $path = '/statuses/featured.xml';
  456. $response = $this->restGet($path);
  457. return new Zend_Rest_Client_Result($response->getBody());
  458. }
  459. /**
  460. * Show extended information on a user
  461. *
  462. * @param int|string $id User ID or name
  463. * @return Zend_Rest_Client_Result
  464. */
  465. public function userShow ($id)
  466. {
  467. $this->_init();
  468. $path = '/users/show/' . $this->_validInteger($id) . '.xml';
  469. $response = $this->restGet($path);
  470. return new Zend_Rest_Client_Result($response->getBody());
  471. }
  472. /**
  473. * Retrieve direct messages for the current user
  474. *
  475. * $params may include one or more of the following keys
  476. * - since: return results only after the date specified
  477. * - since_id: return statuses only greater than the one specified
  478. * - page: return page X of results
  479. *
  480. * @param array $params
  481. * @return Zend_Rest_Client_Result
  482. */
  483. public function directMessageMessages (array $params = array())
  484. {
  485. $this->_init();
  486. $path = '/direct_messages.xml';
  487. $_params = array();
  488. foreach ($params as $key => $value) {
  489. switch (strtolower($key)) {
  490. case 'since':
  491. $this->_setDate($value);
  492. break;
  493. case 'since_id':
  494. $_params['since_id'] = $this->_validInteger($value);
  495. break;
  496. case 'page':
  497. $_params['page'] = (int) $value;
  498. break;
  499. default:
  500. break;
  501. }
  502. }
  503. $response = $this->restGet($path, $_params);
  504. return new Zend_Rest_Client_Result($response->getBody());
  505. }
  506. /**
  507. * Retrieve list of direct messages sent by current user
  508. *
  509. * $params may include one or more of the following keys
  510. * - since: return results only after the date specified
  511. * - since_id: return statuses only greater than the one specified
  512. * - page: return page X of results
  513. *
  514. * @param array $params
  515. * @return Zend_Rest_Client_Result
  516. */
  517. public function directMessageSent (array $params = array())
  518. {
  519. $this->_init();
  520. $path = '/direct_messages/sent.xml';
  521. $_params = array();
  522. foreach ($params as $key => $value) {
  523. switch (strtolower($key)) {
  524. case 'since':
  525. $this->_setDate($value);
  526. break;
  527. case 'since_id':
  528. $_params['since_id'] = $this->_validInteger($value);
  529. break;
  530. case 'page':
  531. $_params['page'] = (int) $value;
  532. break;
  533. default:
  534. break;
  535. }
  536. }
  537. $response = $this->restGet($path, $_params);
  538. return new Zend_Rest_Client_Result($response->getBody());
  539. }
  540. /**
  541. * Send a direct message to a user
  542. *
  543. * @param int|string $user User to whom to send message
  544. * @param string $text Message to send to user
  545. * @return Zend_Rest_Client_Result
  546. * @throws Zend_Service_Twitter_Exception if message is too short or too long
  547. */
  548. public function directMessageNew ($user, $text)
  549. {
  550. $this->_init();
  551. $path = '/direct_messages/new.xml';
  552. $len = iconv_strlen($text, 'UTF-8');
  553. if (0 == $len) {
  554. throw new Zend_Service_Twitter_Exception('Direct message must contain at least one character');
  555. } elseif (140 < $len) {
  556. throw new Zend_Service_Twitter_Exception('Direct message must contain no more than 140 characters');
  557. }
  558. $data = array('user' => $user , 'text' => $text);
  559. $response = $this->restPost($path, $data);
  560. return new Zend_Rest_Client_Result($response->getBody());
  561. }
  562. /**
  563. * Destroy a direct message
  564. *
  565. * @param int $id ID of message to destroy
  566. * @return Zend_Rest_Client_Result
  567. */
  568. public function directMessageDestroy ($id)
  569. {
  570. $this->_init();
  571. $path = '/direct_messages/destroy/' . $this->_validInteger($id) . '.xml';
  572. $response = $this->restPost($path);
  573. return new Zend_Rest_Client_Result($response->getBody());
  574. }
  575. /**
  576. * Create friendship
  577. *
  578. * @param int|string $id User ID or name of new friend
  579. * @return Zend_Rest_Client_Result
  580. */
  581. public function friendshipCreate ($id)
  582. {
  583. $this->_init();
  584. $path = '/friendships/create/' . $this->_validInteger($id) . '.xml';
  585. $response = $this->restPost($path);
  586. return new Zend_Rest_Client_Result($response->getBody());
  587. }
  588. /**
  589. * Destroy friendship
  590. *
  591. * @param int|string $id User ID or name of friend to remove
  592. * @return Zend_Rest_Client_Result
  593. */
  594. public function friendshipDestroy ($id)
  595. {
  596. $this->_init();
  597. $path = '/friendships/destroy/' . $this->_validInteger($id) . '.xml';
  598. $response = $this->restPost($path);
  599. return new Zend_Rest_Client_Result($response->getBody());
  600. }
  601. /**
  602. * Friendship exists
  603. *
  604. * @param int|string $id User ID or name of friend to see if they are your friend
  605. * @return Zend_Rest_Client_result
  606. */
  607. public function friendshipExists ($id)
  608. {
  609. $this->_init();
  610. $path = '/friendships/exists.xml';
  611. $data = array('user_a' => $this->getUsername() , 'user_b' => $this->_validInteger($id));
  612. $response = $this->restGet($path, $data);
  613. return new Zend_Rest_Client_Result($response->getBody());
  614. }
  615. /**
  616. * Verify Account Credentials
  617. *
  618. * @return Zend_Rest_Client_Result
  619. */
  620. public function accountVerifyCredentials ()
  621. {
  622. $this->_init();
  623. $response = $this->restGet('/account/verify_credentials.xml');
  624. return new Zend_Rest_Client_Result($response->getBody());
  625. }
  626. /**
  627. * End current session
  628. *
  629. * @return true
  630. */
  631. public function accountEndSession ()
  632. {
  633. $this->_init();
  634. $this->restGet('/account/end_session');
  635. return true;
  636. }
  637. /**
  638. * Returns the number of api requests you have left per hour.
  639. *
  640. * @return Zend_Rest_Client_Result
  641. */
  642. public function accountRateLimitStatus ()
  643. {
  644. $this->_init();
  645. $response = $this->restGet('/account/rate_limit_status.xml');
  646. return new Zend_Rest_Client_Result($response->getBody());
  647. }
  648. /**
  649. * Fetch favorites
  650. *
  651. * $params may contain one or more of the following:
  652. * - 'id': Id of a user for whom to fetch favorites
  653. * - 'page': Retrieve a different page of resuls
  654. *
  655. * @param array $params
  656. * @return Zend_Rest_Client_Result
  657. */
  658. public function favoriteFavorites (array $params = array())
  659. {
  660. $this->_init();
  661. $path = '/favorites';
  662. $_params = array();
  663. foreach ($params as $key => $value) {
  664. switch (strtolower($key)) {
  665. case 'id':
  666. $path .= '/' . $this->_validInteger($value);
  667. break;
  668. case 'page':
  669. $_params['page'] = (int) $value;
  670. break;
  671. default:
  672. break;
  673. }
  674. }
  675. $path .= '.xml';
  676. $response = $this->restGet($path, $_params);
  677. return new Zend_Rest_Client_Result($response->getBody());
  678. }
  679. /**
  680. * Mark a status as a favorite
  681. *
  682. * @param int $id Status ID you want to mark as a favorite
  683. * @return Zend_Rest_Client_Result
  684. */
  685. public function favoriteCreate ($id)
  686. {
  687. $this->_init();
  688. $path = '/favorites/create/' . $this->_validInteger($id) . '.xml';
  689. $response = $this->restPost($path);
  690. return new Zend_Rest_Client_Result($response->getBody());
  691. }
  692. /**
  693. * Remove a favorite
  694. *
  695. * @param int $id Status ID you want to de-list as a favorite
  696. * @return Zend_Rest_Client_Result
  697. */
  698. public function favoriteDestroy ($id)
  699. {
  700. $this->_init();
  701. $path = '/favorites/destroy/' . $this->_validInteger($id) . '.xml';
  702. $response = $this->restPost($path);
  703. return new Zend_Rest_Client_Result($response->getBody());
  704. }
  705. /**
  706. * Protected function to validate that the integer is valid or return a 0
  707. * @param $int
  708. * @return integer
  709. */
  710. protected function _validInteger ($int)
  711. {
  712. if (preg_match("/(\d+)/", $int)) {
  713. return $int;
  714. }
  715. return 0;
  716. }
  717. }