PageRenderTime 22ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/lib/twitter/twitteroauth.php

https://bitbucket.org/pooshonk/esw
PHP | 245 lines | 143 code | 24 blank | 78 comment | 21 complexity | 1db6c3991bdb20732c05a18a9c42c458 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /*
  3. * Abraham Williams (abraham@abrah.am) http://abrah.am
  4. *
  5. * The first PHP Library to support OAuth for Twitter's REST API.
  6. */
  7. /**
  8. * Twitter OAuth class
  9. */
  10. class TwitterOAuth {
  11. /* Contains the last HTTP status code returned. */
  12. public $http_code;
  13. /* Contains the last API call. */
  14. public $url;
  15. /* Set up the API root URL. */
  16. public $host = "https://api.twitter.com/1/";
  17. /* Set timeout default. */
  18. public $timeout = 30;
  19. /* Set connect timeout. */
  20. public $connecttimeout = 30;
  21. /* Verify SSL Cert. */
  22. public $ssl_verifypeer = FALSE;
  23. /* Respons format. */
  24. public $format = 'json';
  25. /* Decode returned json data. */
  26. public $decode_json = TRUE;
  27. /* Contains the last HTTP headers returned. */
  28. public $http_info;
  29. /* Set the useragnet. */
  30. public $useragent = 'TwitterOAuth v0.2.0-beta2';
  31. /* Immediately retry the API call if the response was not successful. */
  32. //public $retry = TRUE;
  33. /**
  34. * Set API URLS
  35. */
  36. function accessTokenURL() { return 'https://api.twitter.com/oauth/access_token'; }
  37. function authenticateURL() { return 'https://twitter.com/oauth/authenticate'; }
  38. function authorizeURL() { return 'https://twitter.com/oauth/authorize'; }
  39. function requestTokenURL() { return 'https://api.twitter.com/oauth/request_token'; }
  40. /**
  41. * Debug helpers
  42. */
  43. function lastStatusCode() { return $this->http_status; }
  44. function lastAPICall() { return $this->last_api_call; }
  45. /**
  46. * construct TwitterOAuth object
  47. */
  48. function __construct($consumer_key, $consumer_secret, $oauth_token = NULL, $oauth_token_secret = NULL) {
  49. $this->sha1_method = new OAuthSignatureMethod_HMAC_SHA1();
  50. $this->consumer = new OAuthConsumer($consumer_key, $consumer_secret);
  51. if (!empty($oauth_token) && !empty($oauth_token_secret)) {
  52. $this->token = new OAuthConsumer($oauth_token, $oauth_token_secret);
  53. } else {
  54. $this->token = NULL;
  55. }
  56. }
  57. /**
  58. * Get a request_token from Twitter
  59. *
  60. * @returns a key/value array containing oauth_token and oauth_token_secret
  61. */
  62. function getRequestToken($oauth_callback = NULL) {
  63. $parameters = array();
  64. if (!empty($oauth_callback)) {
  65. $parameters['oauth_callback'] = $oauth_callback;
  66. }
  67. $request = $this->oAuthRequest($this->requestTokenURL(), 'GET', $parameters);
  68. $token = OAuthUtil::parse_parameters($request);
  69. $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
  70. return $token;
  71. }
  72. /**
  73. * Get the authorize URL
  74. *
  75. * @returns a string
  76. */
  77. function getAuthorizeURL($token, $sign_in_with_twitter = TRUE) {
  78. if (is_array($token)) {
  79. $token = $token['oauth_token'];
  80. }
  81. if (empty($sign_in_with_twitter)) {
  82. return $this->authorizeURL() . "?oauth_token={$token}";
  83. } else {
  84. return $this->authenticateURL() . "?oauth_token={$token}";
  85. }
  86. }
  87. /**
  88. * Exchange request token and secret for an access token and
  89. * secret, to sign API calls.
  90. *
  91. * @returns array("oauth_token" => "the-access-token",
  92. * "oauth_token_secret" => "the-access-secret",
  93. * "user_id" => "9436992",
  94. * "screen_name" => "abraham")
  95. */
  96. function getAccessToken($oauth_verifier = FALSE) {
  97. $parameters = array();
  98. if (!empty($oauth_verifier)) {
  99. $parameters['oauth_verifier'] = $oauth_verifier;
  100. }
  101. $request = $this->oAuthRequest($this->accessTokenURL(), 'GET', $parameters);
  102. $token = OAuthUtil::parse_parameters($request);
  103. $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
  104. return $token;
  105. }
  106. /**
  107. * One time exchange of username and password for access token and secret.
  108. *
  109. * @returns array("oauth_token" => "the-access-token",
  110. * "oauth_token_secret" => "the-access-secret",
  111. * "user_id" => "9436992",
  112. * "screen_name" => "abraham",
  113. * "x_auth_expires" => "0")
  114. */
  115. function getXAuthToken($username, $password) {
  116. $parameters = array();
  117. $parameters['x_auth_username'] = $username;
  118. $parameters['x_auth_password'] = $password;
  119. $parameters['x_auth_mode'] = 'client_auth';
  120. $request = $this->oAuthRequest($this->accessTokenURL(), 'POST', $parameters);
  121. $token = OAuthUtil::parse_parameters($request);
  122. $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
  123. return $token;
  124. }
  125. /**
  126. * GET wrapper for oAuthRequest.
  127. */
  128. function get($url, $parameters = array()) {
  129. $response = $this->oAuthRequest($url, 'GET', $parameters);
  130. if ($this->format === 'json' && $this->decode_json) {
  131. return json_decode($response);
  132. }
  133. return $response;
  134. }
  135. /**
  136. * POST wrapper for oAuthRequest.
  137. */
  138. function post($url, $parameters = array()) {
  139. $response = $this->oAuthRequest($url, 'POST', $parameters);
  140. if ($this->format === 'json' && $this->decode_json) {
  141. return json_decode($response);
  142. }
  143. return $response;
  144. }
  145. /**
  146. * DELETE wrapper for oAuthReqeust.
  147. */
  148. function delete($url, $parameters = array()) {
  149. $response = $this->oAuthRequest($url, 'DELETE', $parameters);
  150. if ($this->format === 'json' && $this->decode_json) {
  151. return json_decode($response);
  152. }
  153. return $response;
  154. }
  155. /**
  156. * Format and sign an OAuth / API request
  157. */
  158. function oAuthRequest($url, $method, $parameters) {
  159. if (strrpos($url, 'https://') !== 0 && strrpos($url, 'http://') !== 0) {
  160. $url = "{$this->host}{$url}.{$this->format}";
  161. }
  162. $request = OAuthRequest::from_consumer_and_token($this->consumer, $this->token, $method, $url, $parameters);
  163. $request->sign_request($this->sha1_method, $this->consumer, $this->token);
  164. switch ($method) {
  165. case 'GET':
  166. return $this->http($request->to_url(), 'GET');
  167. default:
  168. return $this->http($request->get_normalized_http_url(), $method, $request->to_postdata());
  169. }
  170. }
  171. /**
  172. * Make an HTTP request
  173. *
  174. * @return API results
  175. */
  176. function http($url, $method, $postfields = NULL) {
  177. $this->http_info = array();
  178. $ci = curl_init();
  179. /* Curl settings */
  180. curl_setopt($ci, CURLOPT_USERAGENT, $this->useragent);
  181. curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, $this->connecttimeout);
  182. curl_setopt($ci, CURLOPT_TIMEOUT, $this->timeout);
  183. curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);
  184. curl_setopt($ci, CURLOPT_HTTPHEADER, array('Expect:'));
  185. curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, $this->ssl_verifypeer);
  186. curl_setopt($ci, CURLOPT_HEADERFUNCTION, array($this, 'getHeader'));
  187. curl_setopt($ci, CURLOPT_HEADER, FALSE);
  188. switch ($method) {
  189. case 'POST':
  190. curl_setopt($ci, CURLOPT_POST, TRUE);
  191. if (!empty($postfields)) {
  192. curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
  193. }
  194. break;
  195. case 'DELETE':
  196. curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE');
  197. if (!empty($postfields)) {
  198. $url = "{$url}?{$postfields}";
  199. }
  200. }
  201. curl_setopt($ci, CURLOPT_URL, $url);
  202. $response = curl_exec($ci);
  203. $this->http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);
  204. $this->http_info = array_merge($this->http_info, curl_getinfo($ci));
  205. $this->url = $url;
  206. curl_close ($ci);
  207. return $response;
  208. }
  209. /**
  210. * Get the header info to store.
  211. */
  212. function getHeader($ch, $header) {
  213. $i = strpos($header, ':');
  214. if (!empty($i)) {
  215. $key = str_replace('-', '_', strtolower(substr($header, 0, $i)));
  216. $value = trim(substr($header, $i + 2));
  217. $this->http_header[$key] = $value;
  218. }
  219. return strlen($header);
  220. }
  221. }