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
11 /* Load OAuth lib. You can find it at http://oauth.net */
12 require_once __DIR__ . DIRECTORY_SEPARATOR . 'OAuth1.php';
19 /* Contains the last HTTP status code returned. */
21 /* Contains the last API call. */
23 /* Set up the API root URL. */
24 public $host = "https://api.twitter.com/1.1/";
25 /* Set timeout default. */
27 /* Set connect timeout. */
28 public $connecttimeout = 30;
29 /* Verify SSL Cert. */
30 public $ssl_verifypeer = FALSE;
32 public $format = 'json';
33 /* Decode returned json data. */
34 public $decode_json = TRUE;
35 /* Contains the last HTTP headers returned. */
37 /* Set the useragnet. */
38 public $useragent = 'TwitterOAuth v0.2.0-beta2';
40 /* Immediately retry the API call if the response was not successful. */
41 //public $retry = TRUE;
46 function accessTokenURL()
48 return 'https://api.twitter.com/oauth/access_token';
51 function authenticateURL()
53 return 'https://twitter.com/oauth/authenticate';
56 function authorizeURL()
58 return 'https://twitter.com/oauth/authorize';
61 function requestTokenURL()
63 return 'https://api.twitter.com/oauth/request_token';
69 function lastStatusCode()
71 return $this->http_status;
74 function lastAPICall()
76 return $this->last_api_call;
80 * construct TwitterOAuth object
82 function __construct($consumer_key, $consumer_secret, $oauth_token = NULL, $oauth_token_secret = NULL)
84 $this->sha1_method = new OAuthSignatureMethod_HMAC_SHA1();
85 $this->consumer = new OAuthConsumer($consumer_key, $consumer_secret);
86 if (!empty($oauth_token) && !empty($oauth_token_secret)) {
87 $this->token = new OAuthConsumer($oauth_token, $oauth_token_secret);
94 * Get a request_token from Twitter
96 * @returns a key/value array containing oauth_token and oauth_token_secret
98 function getRequestToken($oauth_callback = NULL)
100 $parameters = array();
101 if (!empty($oauth_callback)) {
102 $parameters['oauth_callback'] = $oauth_callback;
104 $request = $this->oAuthRequest($this->requestTokenURL(), 'GET', $parameters);
105 $token = OAuthUtil::parse_parameters($request);
106 $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
111 * Get the authorize URL
115 function getAuthorizeURL($token, $sign_in_with_twitter = TRUE)
117 if (is_array($token)) {
118 $token = $token['oauth_token'];
120 if (empty($sign_in_with_twitter)) {
121 return $this->authorizeURL() . "?oauth_token={$token}";
123 return $this->authenticateURL() . "?oauth_token={$token}";
128 * Exchange request token and secret for an access token and
129 * secret, to sign API calls.
131 * @returns array("oauth_token" => "the-access-token",
132 * "oauth_token_secret" => "the-access-secret",
133 * "user_id" => "9436992",
134 * "screen_name" => "abraham")
136 function getAccessToken($oauth_verifier = FALSE)
138 $parameters = array();
139 if (!empty($oauth_verifier)) {
140 $parameters['oauth_verifier'] = $oauth_verifier;
142 $request = $this->oAuthRequest($this->accessTokenURL(), 'GET', $parameters);
143 $token = OAuthUtil::parse_parameters($request);
144 $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
149 * One time exchange of username and password for access token and secret.
151 * @returns array("oauth_token" => "the-access-token",
152 * "oauth_token_secret" => "the-access-secret",
153 * "user_id" => "9436992",
154 * "screen_name" => "abraham",
155 * "x_auth_expires" => "0")
157 function getXAuthToken($username, $password)
159 $parameters = array();
160 $parameters['x_auth_username'] = $username;
161 $parameters['x_auth_password'] = $password;
162 $parameters['x_auth_mode'] = 'client_auth';
163 $request = $this->oAuthRequest($this->accessTokenURL(), 'POST', $parameters);
164 $token = OAuthUtil::parse_parameters($request);
165 $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
170 * GET wrapper for oAuthRequest.
172 function get($url, $parameters = array())
174 $response = $this->oAuthRequest($url, 'GET', $parameters);
175 if ($this->format === 'json' && $this->decode_json) {
176 return json_decode($response);
182 * POST wrapper for oAuthRequest.
184 function post($url, $parameters = array())
186 $response = $this->oAuthRequest($url, 'POST', $parameters);
187 if ($this->format === 'json' && $this->decode_json) {
188 return json_decode($response);
194 * DELETE wrapper for oAuthReqeust.
196 function delete($url, $parameters = array())
198 $response = $this->oAuthRequest($url, 'DELETE', $parameters);
199 if ($this->format === 'json' && $this->decode_json) {
200 return json_decode($response);
206 * Format and sign an OAuth / API request
208 function oAuthRequest($url, $method, $parameters)
210 if (strrpos($url, 'https://') !== 0 && strrpos($url, 'http://') !== 0) {
211 $url = "{$this->host}{$url}.{$this->format}";
213 $request = OAuthRequest::from_consumer_and_token($this->consumer, $this->token, $method, $url, $parameters);
214 $request->sign_request($this->sha1_method, $this->consumer, $this->token);
217 return $this->http($request->to_url(), 'GET');
219 return $this->http($request->get_normalized_http_url(), 'POST', $request->to_postdata(true));
221 return $this->http($request->get_normalized_http_url(), $method, $request->to_postdata());
226 * Make an HTTP request
228 * @return API results
230 function http($url, $method, $postfields = NULL)
232 $this->http_info = array();
235 curl_setopt($ci, CURLOPT_USERAGENT, $this->useragent);
236 curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, $this->connecttimeout);
237 curl_setopt($ci, CURLOPT_TIMEOUT, $this->timeout);
238 curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);
239 curl_setopt($ci, CURLOPT_HTTPHEADER, array('Expect:'));
240 curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, $this->ssl_verifypeer);
241 curl_setopt($ci, CURLOPT_HEADERFUNCTION, array($this, 'getHeader'));
242 curl_setopt($ci, CURLOPT_HEADER, FALSE);
246 curl_setopt($ci, CURLOPT_POST, TRUE);
247 if (!empty($postfields)) {
248 curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
252 curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE');
253 if (!empty($postfields)) {
254 $url = "{$url}?{$postfields}";
258 curl_setopt($ci, CURLOPT_URL, $url);
259 $response = curl_exec($ci);
260 $this->http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);
261 $this->http_info = array_merge($this->http_info, curl_getinfo($ci));
268 * Get the header info to store.
270 function getHeader($ch, $header)
272 $i = strpos($header, ':');
274 $key = str_replace('-', '_', strtolower(substr($header, 0, $i)));
275 $value = trim(substr($header, $i + 2));
276 $this->http_header[$key] = $value;
278 return strlen($header);