4 * Abraham Williams (abraham@abrah.am) http://abrah.am
6 * The first PHP Library to support OAuth for Twitter's REST API.
8 * Version 0.2.0 kept for compatibility purpose with StatusNetOAuth
16 /* Contains the last HTTP status code returned. */
18 /* Contains the last API call. */
20 /* Set up the API root URL. */
21 public $host = "https://api.twitter.com/1.1/";
22 /* Set timeout default. */
24 /* Set connect timeout. */
25 public $connecttimeout = 30;
26 /* Verify SSL Cert. */
27 public $ssl_verifypeer = FALSE;
29 public $format = 'json';
30 /* Decode returned json data. */
31 public $decode_json = TRUE;
32 /* Contains the last HTTP headers returned. */
34 /* Set the useragnet. */
35 public $useragent = 'TwitterOAuth v0.2.0-beta2';
37 /* Immediately retry the API call if the response was not successful. */
38 //public $retry = TRUE;
43 function accessTokenURL()
45 return 'https://api.twitter.com/oauth/access_token';
48 function authenticateURL()
50 return 'https://twitter.com/oauth/authenticate';
53 function authorizeURL()
55 return 'https://twitter.com/oauth/authorize';
58 function requestTokenURL()
60 return 'https://api.twitter.com/oauth/request_token';
66 function lastStatusCode()
68 return $this->http_status;
71 function lastAPICall()
73 return $this->last_api_call;
77 * construct TwitterOAuth object
79 function __construct($consumer_key, $consumer_secret, $oauth_token = NULL, $oauth_token_secret = NULL)
81 $this->sha1_method = new OAuthSignatureMethod_HMAC_SHA1();
82 $this->consumer = new OAuthConsumer($consumer_key, $consumer_secret);
83 if (!empty($oauth_token) && !empty($oauth_token_secret)) {
84 $this->token = new OAuthConsumer($oauth_token, $oauth_token_secret);
91 * Get a request_token from Twitter
93 * @returns a key/value array containing oauth_token and oauth_token_secret
95 function getRequestToken($oauth_callback = NULL)
97 $parameters = array();
98 if (!empty($oauth_callback)) {
99 $parameters['oauth_callback'] = $oauth_callback;
101 $request = $this->oAuthRequest($this->requestTokenURL(), 'GET', $parameters);
102 $token = OAuthUtil::parse_parameters($request);
103 $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
108 * Get the authorize URL
112 function getAuthorizeURL($token, $sign_in_with_twitter = TRUE)
114 if (is_array($token)) {
115 $token = $token['oauth_token'];
117 if (empty($sign_in_with_twitter)) {
118 return $this->authorizeURL() . "?oauth_token={$token}";
120 return $this->authenticateURL() . "?oauth_token={$token}";
125 * Exchange request token and secret for an access token and
126 * secret, to sign API calls.
128 * @returns array("oauth_token" => "the-access-token",
129 * "oauth_token_secret" => "the-access-secret",
130 * "user_id" => "9436992",
131 * "screen_name" => "abraham")
133 function getAccessToken($oauth_verifier = FALSE)
135 $parameters = array();
136 if (!empty($oauth_verifier)) {
137 $parameters['oauth_verifier'] = $oauth_verifier;
139 $request = $this->oAuthRequest($this->accessTokenURL(), 'GET', $parameters);
140 $token = OAuthUtil::parse_parameters($request);
141 $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
146 * One time exchange of username and password for access token and secret.
148 * @returns array("oauth_token" => "the-access-token",
149 * "oauth_token_secret" => "the-access-secret",
150 * "user_id" => "9436992",
151 * "screen_name" => "abraham",
152 * "x_auth_expires" => "0")
154 function getXAuthToken($username, $password)
156 $parameters = array();
157 $parameters['x_auth_username'] = $username;
158 $parameters['x_auth_password'] = $password;
159 $parameters['x_auth_mode'] = 'client_auth';
160 $request = $this->oAuthRequest($this->accessTokenURL(), 'POST', $parameters);
161 $token = OAuthUtil::parse_parameters($request);
162 $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
167 * GET wrapper for oAuthRequest.
169 function get($url, $parameters = array())
171 $response = $this->oAuthRequest($url, 'GET', $parameters);
172 if ($this->format === 'json' && $this->decode_json) {
173 return json_decode($response);
179 * POST wrapper for oAuthRequest.
181 function post($url, $parameters = array())
183 $response = $this->oAuthRequest($url, 'POST', $parameters);
184 if ($this->format === 'json' && $this->decode_json) {
185 return json_decode($response);
191 * DELETE wrapper for oAuthReqeust.
193 function delete($url, $parameters = array())
195 $response = $this->oAuthRequest($url, 'DELETE', $parameters);
196 if ($this->format === 'json' && $this->decode_json) {
197 return json_decode($response);
203 * Format and sign an OAuth / API request
205 function oAuthRequest($url, $method, $parameters)
207 if (strrpos($url, 'https://') !== 0 && strrpos($url, 'http://') !== 0) {
208 $url = "{$this->host}{$url}.{$this->format}";
210 $request = OAuthRequest::from_consumer_and_token($this->consumer, $this->token, $method, $url, $parameters);
211 $request->sign_request($this->sha1_method, $this->consumer, $this->token);
214 return $this->http($request->to_url(), 'GET');
216 return $this->http($request->get_normalized_http_url(), 'POST', $request->to_postdata(true));
218 return $this->http($request->get_normalized_http_url(), $method, $request->to_postdata());
223 * Make an HTTP request
225 * @return API results
227 function http($url, $method, $postfields = NULL)
229 $this->http_info = array();
232 curl_setopt($ci, CURLOPT_USERAGENT, $this->useragent);
233 curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, $this->connecttimeout);
234 curl_setopt($ci, CURLOPT_TIMEOUT, $this->timeout);
235 curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);
236 curl_setopt($ci, CURLOPT_HTTPHEADER, array('Expect:'));
237 curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, $this->ssl_verifypeer);
238 curl_setopt($ci, CURLOPT_HEADERFUNCTION, array($this, 'getHeader'));
239 curl_setopt($ci, CURLOPT_HEADER, FALSE);
243 curl_setopt($ci, CURLOPT_POST, TRUE);
244 if (!empty($postfields)) {
245 curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
249 curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE');
250 if (!empty($postfields)) {
251 $url = "{$url}?{$postfields}";
255 curl_setopt($ci, CURLOPT_URL, $url);
256 $response = curl_exec($ci);
257 $this->http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);
258 $this->http_info = array_merge($this->http_info, curl_getinfo($ci));
265 * Get the header info to store.
267 function getHeader($ch, $header)
269 $i = strpos($header, ':');
271 $key = str_replace('-', '_', strtolower(substr($header, 0, $i)));
272 $value = trim(substr($header, $i + 2));
273 $this->http_header[$key] = $value;
275 return strlen($header);