/2010/plugins/community/twitter/api_class.php

https://bitbucket.org/elijahvsjesus/tandava · PHP · 272 lines · 194 code · 31 blank · 47 comment · 36 complexity · 79d9425eed453870778edfd12baf0ad3 MD5 · raw file

  1. <?php
  2. /**
  3. * Twitter interface class
  4. * Nov 26 2007 Nick Beam
  5. * Bugs, comments, questions: winkerbeam@gmail.com
  6. * http://rbrw.net -- http://tinydinosaur.com
  7. *
  8. * This is a simple interface to the Twitter API.
  9. * I've tried to keep as close as possible to the real API
  10. * calls (some had to be changed due to ambiguity), but all
  11. * of the arguments are as they are in the official docs.
  12. *
  13. * Usage:
  14. * $twitter = new Twitter("username", "password");
  15. * $public_timeline_xml = $twitter->getPublicTimeline("xml");
  16. *
  17. * Methods:
  18. * getPublicTimeline($format [, $since_id])
  19. * getFriendsTimeline($format [, $id [, $since ]])
  20. * getUserTimeline($format [, $id [, $count [, $since ]]])
  21. * showStatus($format, $id)
  22. * updateStatus($status)
  23. * destroyStatus($format, $id)
  24. * getReplies($format [, $page ])
  25. * getFriends($format [, $id ])
  26. * getFollowers($format [, $lite ])
  27. * getFeatured($format)
  28. * showUser($format [, $id [, $email ]])
  29. * getMessages($format [, $since [, $since_id [, $page ]]])
  30. * getSentMessages($format [, $since [, $since_id [, $page ]]])
  31. * newMessage($format, $user, $text)
  32. * destroyMessage($format, $id)
  33. * createFriendship($format, $id)
  34. * destroyFriendship($format, $id)
  35. * verifyCredentials([$format])
  36. * endSession()
  37. * getArchive($format [, $page ])
  38. * getFavorites($format [, $id [, $page ]])
  39. * createFavorite($format, $id)
  40. * destroyFavorite($format, $id)
  41. * lastStatusCode()
  42. * lastAPICall()
  43. */
  44. if(!class_exists('Twitter'))
  45. {
  46. class Twitter {
  47. /* Username:password format string */
  48. private $credentials;
  49. /* Contains the last HTTP status code returned */
  50. private $http_status;
  51. /* Contains the last API call */
  52. private $last_api_call;
  53. /* Twitter class constructor */
  54. function Twitter($username, $password) {
  55. $this->credentials = sprintf("%s:%s", $username, $password);
  56. }
  57. function getPublicTimeline($format, $since_id = 0) {
  58. $api_call = sprintf("http://twitter.com/statuses/public_timeline.%s", $format);
  59. if ($since_id > 0) {
  60. $api_call .= sprintf("?since_id=%d", $since_id);
  61. }
  62. return $this->APICall($api_call);
  63. }
  64. function getFriendsTimeline($format, $id = NULL, $since = NULL) {
  65. if ($id != NULL) {
  66. $api_call = sprintf("http://twitter.com/statuses/friends_timeline/%s.%s", $id, $format);
  67. }
  68. else {
  69. $api_call = sprintf("http://twitter.com/statuses/friends_timeline.%s", $format);
  70. }
  71. if ($since != NULL) {
  72. $api_call .= sprintf("?since=%s", urlencode($since));
  73. }
  74. return $this->APICall($api_call, true);
  75. }
  76. function getUserTimeline($format, $id = NULL, $count = 20, $since = NULL) {
  77. if ($id != NULL) {
  78. $api_call = sprintf("http://twitter.com/statuses/user_timeline/%s.%s", $id, $format);
  79. }
  80. else {
  81. $api_call = sprintf("http://twitter.com/statuses/user_timeline.%s", $format);
  82. }
  83. if ($count != 20) {
  84. $api_call .= sprintf("?count=%d", $count);
  85. }
  86. if ($since != NULL) {
  87. $api_call .= sprintf("%ssince=%s", (JString::strpos($api_call, "?count=") === false) ? "?" : "&", urlencode($since));
  88. }
  89. return $this->APICall($api_call, true);
  90. }
  91. function showStatus($format, $id) {
  92. $api_call = sprintf("http://twitter.com/statuses/show/%d.%s", $id, $format);
  93. return $this->APICall($api_call);
  94. }
  95. function updateStatus($status) {
  96. $status = urlencode(stripslashes(urldecode($status)));
  97. $api_call = sprintf("http://twitter.com/statuses/update.xml?status=%s", $status);
  98. return $this->APICall($api_call, true, true);
  99. }
  100. function getReplies($format, $page = 0) {
  101. $api_call = sprintf("http://twitter.com/statuses/replies.%s", $format);
  102. if ($page) {
  103. $api_call .= sprintf("?page=%d", $page);
  104. }
  105. return $this->APICall($api_call, true);
  106. }
  107. function destroyStatus($format, $id) {
  108. $api_call = sprintf("http://twitter.com/statuses/destroy/%d.%s", $id, $format);
  109. return $this->APICall($api_call, true);
  110. }
  111. function getFriends($format, $id = NULL) {
  112. // take care of the id parameter
  113. if ($id != NULL) {
  114. $api_call = sprintf("http://twitter.com/statuses/friends/%s.%s", $id, $format);
  115. }
  116. else {
  117. $api_call = sprintf("http://twitter.com/statuses/friends.%s", $format);
  118. }
  119. return $this->APICall($api_call, true);
  120. }
  121. function getFollowers($format, $lite = NULL) {
  122. $api_call = sprintf("http://twitter.com/statuses/followers.%s%s", $format, ($lite) ? "?lite=true" : NULL);
  123. return $this->APICall($api_call, true);
  124. }
  125. function getFeatured($format) {
  126. $api_call = sprintf("http://twitter.com/statuses/featured.%s", $format);
  127. return $this->APICall($api_call);
  128. }
  129. function showUser($format, $id, $email = NULL) {
  130. if ($email == NULL) {
  131. $api_call = sprintf("http://twitter.com/users/show/%s.%s", $id, $format);
  132. }
  133. else {
  134. $api_call = sprintf("http://twitter.com/users/show.xml?email=%s", $email);
  135. }
  136. return $this->APICall($api_call, true);
  137. }
  138. function getMessages($format, $since = NULL, $since_id = 0, $page = 1) {
  139. $api_call = sprintf("http://twitter.com/direct_messages.%s", $format);
  140. if ($since != NULL) {
  141. $api_call .= sprintf("?since=%s", urlencode($since));
  142. }
  143. if ($since_id > 0) {
  144. $api_call .= sprintf("%ssince_id=%d", (JString::strpos($api_call, "?since") === false) ? "?" : "&", $since_id);
  145. }
  146. if ($page > 1) {
  147. $api_call .= sprintf("%spage=%d", (JString::strpos($api_call, "?since") === false) ? "?" : "&", $page);
  148. }
  149. return $this->APICall($api_call, true);
  150. }
  151. function getSentMessages($format, $since = NULL, $since_id = 0, $page = 1) {
  152. $api_call = sprintf("http://twitter.com/direct_messages/sent.%s", $format);
  153. if ($since != NULL) {
  154. $api_call .= sprintf("?since=%s", urlencode($since));
  155. }
  156. if ($since_id > 0) {
  157. $api_call .= sprintf("%ssince_id=%d", (JString::strpos($api_call, "?since") === false) ? "?" : "&", $since_id);
  158. }
  159. if ($page > 1) {
  160. $api_call .= sprintf("%spage=%d", (JString::strpos($api_call, "?since") === false) ? "?" : "&", $page);
  161. }
  162. return $this->APICall($api_call, true);
  163. }
  164. function newMessage($format, $user, $text) {
  165. $text = urlencode(stripslashes(urldecode($text)));
  166. $api_call = sprintf("http://twitter.com/direct_messages/new.%s?user=%s&text=%s", $format, $user, $text);
  167. return $this->APICall($api_call, true, true);
  168. }
  169. function destroyMessage($format, $id) {
  170. $api_call = sprintf("http://twitter.com/direct_messages/destroy/%s.%s", $id, $format);
  171. return $this->APICall($api_call, true);
  172. }
  173. function createFriendship($format, $id) {
  174. $api_call = sprintf("http://twitter.com/friendships/create/%s.%s", $format, $id);
  175. return $this->APICall($api_call, true);
  176. }
  177. function destroyFriendship($format, $id) {
  178. $api_call = sprintf("http://twitter.com/friendships/destroy/%s.%s", $format, $id);
  179. return $this->APICall($api_call, true);
  180. }
  181. function verifyCredentials($format = NULL) {
  182. $api_call = sprintf("http://twitter.com/account/verify_credentials%s", ($format != NULL) ? sprintf(".%s", $format) : NULL);
  183. return $this->APICall($api_call, true);
  184. }
  185. function endSession() {
  186. $api_call = "http://twitter.com/account/end_session";
  187. return $this->APICall($api_call, true);
  188. }
  189. function getArchive($format, $page = 1) {
  190. $api_call = sprintf("http://twitter.com/account/archive.%s", $format);
  191. if ($page > 1) {
  192. $api_call .= sprintf("?page=%d", $page);
  193. }
  194. return $this->APICall($api_call, true);
  195. }
  196. function getFavorites($format, $id = NULL, $page = 1) {
  197. if ($id == NULL) {
  198. $api_call = sprintf("http://twitter.com/favourings.%s", $format);
  199. }
  200. else {
  201. $api_call = sprintf("http://twitter.com/favourings/%s.%s", $id, $format);
  202. }
  203. if ($page > 1) {
  204. $api_call .= sprintf("?page=%d", $page);
  205. }
  206. return $this->APICall($api_call, true);
  207. }
  208. function createFavorite($format, $id) {
  209. $api_call = sprintf("http://twitter.com/favourings/create/%d.%s", $id, $format);
  210. return $this->APICall($api_call, true);
  211. }
  212. function destroyFavorite($format, $id) {
  213. $api_call = sprintf("http://twitter.com/favourings/destroy/%d.%s", $id, $format);
  214. return $this->APICall($api_call, true);
  215. }
  216. private function APICall($api_url, $require_credentials = false, $http_post = false) {
  217. $curl_handle = curl_init();
  218. curl_setopt($curl_handle, CURLOPT_HTTPHEADER, array('Expect:'));
  219. curl_setopt($curl_handle, CURLOPT_URL, $api_url);
  220. if ($require_credentials) {
  221. curl_setopt($curl_handle, CURLOPT_USERPWD, $this->credentials);
  222. }
  223. if ($http_post) {
  224. curl_setopt($curl_handle, CURLOPT_POST, true);
  225. }
  226. curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, TRUE);
  227. $twitter_data = curl_exec($curl_handle);
  228. $this->http_status = curl_getinfo($curl_handle, CURLINFO_HTTP_CODE);
  229. $this->last_api_call = $api_url;
  230. curl_close($curl_handle);
  231. return $twitter_data;
  232. }
  233. function lastStatusCode() {
  234. return $this->http_status;
  235. }
  236. function lastAPICall() {
  237. return $this->last_api_call;
  238. }
  239. }
  240. }
  241. ?>