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 /* Set up the API root URL. */
17 public $host = "https://api.twitter.com/1.1/";
18 /* Set timeout default. */
20 /* Set connect timeout. */
21 public $connecttimeout = 30;
22 /* Verify SSL Cert. */
23 public $ssl_verifypeer = FALSE;
24 /* Response format. */
25 public $format = 'json';
26 /* Decode returned json data. */
27 public $decode_json = TRUE;
28 /* Set the useragent. */
29 public $useragent = 'TwitterOAuth v0.2.0-beta2';
31 /* Contains the last HTTP status code returned. */
33 /* Contains the last API call. */
36 * Contains the last HTTP headers returned.
41 * Contains the last HTTP request info
46 /** @var OAuthToken */
48 /** @var OAuthConsumer */
50 /** @var OAuthSignatureMethod_HMAC_SHA1 */
56 function accessTokenURL()
58 return 'https://api.twitter.com/oauth/access_token';
61 function authenticateURL()
63 return 'https://twitter.com/oauth/authenticate';
66 function authorizeURL()
68 return 'https://twitter.com/oauth/authorize';
71 function requestTokenURL()
73 return 'https://api.twitter.com/oauth/request_token';
76 function __construct($consumer_key, $consumer_secret, $oauth_token = null, $oauth_token_secret = null)
78 $this->sha1_method = new OAuthSignatureMethod_HMAC_SHA1();
79 $this->consumer = new OAuthConsumer($consumer_key, $consumer_secret);
80 if (!empty($oauth_token) && !empty($oauth_token_secret)) {
81 $this->token = new OAuthToken($oauth_token, $oauth_token_secret);
90 * @param callback $oauth_callback
93 function getRequestToken($oauth_callback = null)
96 if (!empty($oauth_callback)) {
97 $parameters['oauth_callback'] = $oauth_callback;
100 $request = $this->oAuthRequest($this->requestTokenURL(), 'GET', $parameters);
101 $token = OAuthUtil::parse_parameters($request);
102 $this->token = new OAuthToken($token['oauth_token'], $token['oauth_token_secret']);
107 * Get the authorize URL
109 * @param array $token
110 * @param bool $sign_in_with_tumblr
113 function getAuthorizeURL($token, $sign_in_with_twitter = TRUE)
115 if (is_array($token)) {
116 $token = $token['oauth_token'];
119 if (empty($sign_in_with_twitter)) {
120 return $this->authorizeURL() . "?oauth_token={$token}";
122 return $this->authenticateURL() . "?oauth_token={$token}";
127 * Exchange request token and secret for an access token and
128 * secret, to sign API calls.
130 * @param bool $oauth_verifier
131 * @return 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)
139 if (!empty($oauth_verifier)) {
140 $parameters['oauth_verifier'] = $oauth_verifier;
143 $request = $this->oAuthRequest($this->accessTokenURL(), 'GET', $parameters);
144 $token = OAuthUtil::parse_parameters($request);
145 $this->token = new OAuthToken($token['oauth_token'], $token['oauth_token_secret']);
151 * One time exchange of username and password for access token and secret.
153 * @param string $username
154 * @param string $password
155 * @return array ("oauth_token" => "the-access-token",
156 * "oauth_token_secret" => "the-access-secret",
157 * "user_id" => "9436992",
158 * "screen_name" => "abraham",
159 * "x_auth_expires" => "0")
161 function getXAuthToken($username, $password)
164 $parameters['x_auth_username'] = $username;
165 $parameters['x_auth_password'] = $password;
166 $parameters['x_auth_mode'] = 'client_auth';
167 $request = $this->oAuthRequest($this->accessTokenURL(), 'POST', $parameters);
168 $token = OAuthUtil::parse_parameters($request);
169 $this->token = new OAuthToken($token['oauth_token'], $token['oauth_token_secret']);
175 * GET wrapper for oAuthRequest.
178 * @param array $parameters
179 * @return mixed|string
181 function get($url, $parameters = [])
183 $response = $this->oAuthRequest($url, 'GET', $parameters);
184 if ($this->format === 'json' && $this->decode_json) {
185 return json_decode($response);
192 * POST wrapper for oAuthRequest.
195 * @param array $parameters
196 * @return mixed|string
198 function post($url, $parameters = [])
200 $response = $this->oAuthRequest($url, 'POST', $parameters);
201 if ($this->format === 'json' && $this->decode_json) {
202 return json_decode($response);
209 * DELETE wrapper for oAuthReqeust.
212 * @param array $parameters
213 * @return mixed|string
215 function delete($url, $parameters = [])
217 $response = $this->oAuthRequest($url, 'DELETE', $parameters);
218 if ($this->format === 'json' && $this->decode_json) {
219 return json_decode($response);
226 * Format and sign an OAuth / API request
229 * @param string $method
230 * @param array $parameters
231 * @return mixed|string
233 function oAuthRequest($url, $method, $parameters)
235 if (strrpos($url, 'https://') !== 0 && strrpos($url, 'http://') !== 0) {
236 $url = "{$this->host}{$url}.{$this->format}";
239 $request = OAuthRequest::from_consumer_and_token($this->consumer, $this->token, $method, $url, $parameters);
240 $request->sign_request($this->sha1_method, $this->consumer, $this->token);
243 return $this->http($request->to_url(), 'GET');
245 return $this->http($request->get_normalized_http_url(), 'POST', $request->to_postdata(true));
247 return $this->http($request->get_normalized_http_url(), $method, $request->to_postdata());
252 * Make an HTTP request
255 * @param string $method
256 * @param mixed $postfields
257 * @return string API results
259 function http($url, $method, $postfields = null)
261 $this->http_info = [];
264 curl_setopt($ci, CURLOPT_USERAGENT, $this->useragent);
265 curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, $this->connecttimeout);
266 curl_setopt($ci, CURLOPT_TIMEOUT, $this->timeout);
267 curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);
268 curl_setopt($ci, CURLOPT_HTTPHEADER, array('Expect:'));
269 curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, $this->ssl_verifypeer);
270 curl_setopt($ci, CURLOPT_HEADERFUNCTION, array($this, 'getHeader'));
271 curl_setopt($ci, CURLOPT_HEADER, FALSE);
275 curl_setopt($ci, CURLOPT_POST, TRUE);
276 if (!empty($postfields)) {
277 curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
281 curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE');
282 if (!empty($postfields)) {
283 $url = "{$url}?{$postfields}";
287 curl_setopt($ci, CURLOPT_URL, $url);
288 $response = curl_exec($ci);
289 $this->http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);
290 $this->http_info = array_merge($this->http_info, curl_getinfo($ci));
298 * Get the header info to store.
300 * @param resource $ch
301 * @param string $header
304 function getHeader($ch, $header)
306 $i = strpos($header, ':');
308 $key = str_replace('-', '_', strtolower(substr($header, 0, $i)));
309 $value = trim(substr($header, $i + 2));
310 $this->http_header[$key] = $value;
313 return strlen($header);