twitteroauthclient.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Class for doing OAuth calls against Twitter
  6. *
  7. * PHP version 5
  8. *
  9. * LICENCE: This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * @category Integration
  23. * @package StatusNet
  24. * @author Zach Copley <zach@status.net>
  25. * @copyright 2009-2010 StatusNet, Inc.
  26. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  27. * @link http://status.net/
  28. */
  29. if (!defined('STATUSNET') && !defined('LACONICA')) {
  30. exit(1);
  31. }
  32. /**
  33. * Class for talking to the Twitter API with OAuth.
  34. *
  35. * @category Integration
  36. * @package StatusNet
  37. * @author Zach Copley <zach@status.net>
  38. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  39. * @link http://status.net/
  40. *
  41. */
  42. class TwitterOAuthClient extends OAuthClient
  43. {
  44. public static $requestTokenURL = 'https://api.twitter.com/oauth/request_token';
  45. public static $authorizeURL = 'https://api.twitter.com/oauth/authorize';
  46. public static $signinUrl = 'https://api.twitter.com/oauth/authenticate';
  47. public static $accessTokenURL = 'https://api.twitter.com/oauth/access_token';
  48. /**
  49. * Constructor
  50. *
  51. * @param string $oauth_token the user's token
  52. * @param string $oauth_token_secret the user's token secret
  53. *
  54. * @return nothing
  55. */
  56. function __construct($oauth_token = null, $oauth_token_secret = null)
  57. {
  58. $consumer_key = common_config('twitter', 'consumer_key');
  59. $consumer_secret = common_config('twitter', 'consumer_secret');
  60. if (empty($consumer_key) && empty($consumer_secret)) {
  61. $consumer_key = common_config(
  62. 'twitter',
  63. 'global_consumer_key'
  64. );
  65. $consumer_secret = common_config(
  66. 'twitter',
  67. 'global_consumer_secret'
  68. );
  69. }
  70. parent::__construct(
  71. $consumer_key,
  72. $consumer_secret,
  73. $oauth_token,
  74. $oauth_token_secret
  75. );
  76. }
  77. // XXX: the following two functions are to support the horrible hack
  78. // of using the credentils field in Foreign_link to store both
  79. // the access token and token secret. This hack should go away with
  80. // 0.9, in which we can make DB changes and add a new column for the
  81. // token itself.
  82. static function packToken($token)
  83. {
  84. return implode(chr(0), array($token->key, $token->secret));
  85. }
  86. static function unpackToken($str)
  87. {
  88. $vals = explode(chr(0), $str);
  89. return new OAuthToken($vals[0], $vals[1]);
  90. }
  91. static function isPackedToken($str)
  92. {
  93. if (strpos($str, chr(0)) === false) {
  94. return false;
  95. } else {
  96. return true;
  97. }
  98. }
  99. /**
  100. * Gets a request token from Twitter
  101. *
  102. * @return OAuthToken $token the request token
  103. */
  104. function getTwitterRequestToken()
  105. {
  106. return parent::getRequestToken(
  107. self::$requestTokenURL,
  108. common_local_url('twitterauthorization')
  109. );
  110. }
  111. /**
  112. * Builds a link to Twitter's endpoint for authorizing a request token
  113. *
  114. * @param OAuthToken $request_token token to authorize
  115. *
  116. * @return the link
  117. */
  118. function getTwitterAuthorizeLink($request_token, $signin = false)
  119. {
  120. $url = ($signin) ? self::$signinUrl : self::$authorizeURL;
  121. return parent::getAuthorizeLink($url,
  122. $request_token,
  123. common_local_url('twitterauthorization'));
  124. }
  125. /**
  126. * Fetches an access token from Twitter
  127. *
  128. * @param string $verifier 1.0a verifier
  129. *
  130. * @return OAuthToken $token the access token
  131. */
  132. function getTwitterAccessToken($verifier = null)
  133. {
  134. return parent::getAccessToken(
  135. self::$accessTokenURL,
  136. $verifier
  137. );
  138. }
  139. /**
  140. * Calls Twitter's /account/verify_credentials API method
  141. *
  142. * @return mixed the Twitter user
  143. */
  144. function verifyCredentials()
  145. {
  146. $url = 'https://api.twitter.com/1.1/account/verify_credentials.json';
  147. $response = $this->oAuthGet($url);
  148. $twitter_user = json_decode($response);
  149. return $twitter_user;
  150. }
  151. /**
  152. * Calls Twitter's /statuses/update API method
  153. *
  154. * @param string $status text of the status
  155. * @param mixed $params optional other parameters to pass to Twitter,
  156. * as defined. For back-compatibility, if an int
  157. * is passed we'll consider it a reply-to ID.
  158. *
  159. * @return mixed the status
  160. */
  161. function statusesUpdate($status, $params=array())
  162. {
  163. $url = 'https://api.twitter.com/1.1/statuses/update.json';
  164. if (is_numeric($params)) {
  165. $params = array('in_reply_to_status_id' => intval($params));
  166. }
  167. $params['status'] = $status;
  168. // We don't have to pass 'source' as the oauth key is tied to an app.
  169. $response = $this->oAuthPost($url, $params);
  170. $status = json_decode($response);
  171. return $status;
  172. }
  173. /**
  174. * Calls Twitter's /statuses/home_timeline API method
  175. *
  176. * @param int $since_id show statuses after this id
  177. * @param string $timelineUri timeline to poll statuses from
  178. * @param int $max_id show statuses before this id
  179. * @param int $cnt number of statuses to show
  180. * @param int $page page number
  181. *
  182. * @return mixed an array of statuses
  183. */
  184. function statusesTimeline($since_id = null, $timelineUri = 'home_timeline',
  185. $max_id = null, $cnt = 200, $page = null)
  186. {
  187. $url = 'https://api.twitter.com/1.1/statuses/'.$timelineUri.'.json';
  188. $params = array('include_entities' => 'true',
  189. 'include_rts' => 'true');
  190. if (!empty($since_id)) {
  191. $params['since_id'] = $since_id;
  192. }
  193. if (!empty($max_id)) {
  194. $params['max_id'] = $max_id;
  195. }
  196. if (!empty($cnt)) {
  197. $params['count'] = $cnt;
  198. }
  199. if (!empty($page)) {
  200. $params['page'] = $page;
  201. }
  202. $response = $this->oAuthGet($url, $params);
  203. $statuses = json_decode($response);
  204. return $statuses;
  205. }
  206. /**
  207. * Calls Twitter's /statuses/friends API method
  208. *
  209. * @param int $id id of the user whom you wish to see friends of
  210. * @param int $user_id numerical user id
  211. * @param int $screen_name screen name
  212. * @param int $page page number
  213. *
  214. * @return mixed an array of twitter users and their latest status
  215. */
  216. function statusesFriends($id = null, $user_id = null, $screen_name = null,
  217. $page = null)
  218. {
  219. $url = "https://api.twitter.com/1.1/friends/list.json";
  220. $params = array();
  221. if (!empty($id)) {
  222. $params['id'] = $id;
  223. }
  224. if (!empty($user_id)) {
  225. $params['user_id'] = $user_id;
  226. }
  227. if (!empty($screen_name)) {
  228. $params['screen_name'] = $screen_name;
  229. }
  230. if (!empty($page)) {
  231. $params['page'] = $page;
  232. }
  233. $response = $this->oAuthGet($url, $params);
  234. $friends = json_decode($response);
  235. return $friends;
  236. }
  237. /**
  238. * Calls Twitter's /statuses/friends/ids API method
  239. *
  240. * @param int $id id of the user whom you wish to see friends of
  241. * @param int $user_id numerical user id
  242. * @param int $screen_name screen name
  243. * @param int $page page number
  244. *
  245. * @return mixed a list of ids, 100 per page
  246. */
  247. function friendsIds($id = null, $user_id = null, $screen_name = null,
  248. $page = null)
  249. {
  250. $url = "https://api.twitter.com/1.1/friends/ids.json";
  251. $params = array();
  252. if (!empty($id)) {
  253. $params['id'] = $id;
  254. }
  255. if (!empty($user_id)) {
  256. $params['user_id'] = $user_id;
  257. }
  258. if (!empty($screen_name)) {
  259. $params['screen_name'] = $screen_name;
  260. }
  261. if (!empty($page)) {
  262. $params['page'] = $page;
  263. }
  264. $response = $this->oAuthGet($url, $params);
  265. $ids = json_decode($response);
  266. return $ids;
  267. }
  268. /**
  269. * Calls Twitter's /statuses/retweet/id.json API method
  270. *
  271. * @param int $id id of the notice to retweet
  272. *
  273. * @return retweeted status
  274. */
  275. function statusesRetweet($id)
  276. {
  277. $url = "https://api.twitter.com/1.1/statuses/retweet/$id.json";
  278. $response = $this->oAuthPost($url);
  279. $status = json_decode($response);
  280. return $status;
  281. }
  282. /**
  283. * Calls Twitter's /favorites/create API method
  284. *
  285. * @param int $id ID of the status to favorite
  286. *
  287. * @return object faved status
  288. */
  289. function favoritesCreate($id)
  290. {
  291. $url = "https://api.twitter.com/1.1/favorites/create.json";
  292. $params=array();
  293. $params['id'] = $id;
  294. $response = $this->oAuthPost($url, $params);
  295. $status = json_decode($response);
  296. return $status;
  297. }
  298. /**
  299. * Calls Twitter's /favorites/destroy API method
  300. *
  301. * @param int $id ID of the status to unfavorite
  302. *
  303. * @return object unfaved status
  304. */
  305. function favoritesDestroy($id)
  306. {
  307. $url = "https://api.twitter.com/1.1/favorites/destroy.json";
  308. $params=array();
  309. $params['id'] = $id;
  310. $response = $this->oAuthPost($url,$params);
  311. $status = json_decode($response);
  312. return $status;
  313. }
  314. /**
  315. * Calls Twitter's /statuses/destroy API method
  316. *
  317. * @param int $id ID of the status to destroy
  318. *
  319. * @return object destroyed
  320. */
  321. function statusesDestroy($id)
  322. {
  323. $url = "https://api.twitter.com/1.1/statuses/destroy/$id.json";
  324. $response = $this->oAuthPost($url);
  325. $status = json_decode($response);
  326. return $status;
  327. }
  328. }