PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/inc/twitteroauth/twitteroauth.php

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