4
* Abraham Williams (abraham@abrah.am) http://abrah.am
6
* The first PHP Library to support TwitterOAuth for Twitter's REST API.
9
/* Load TwitterOAuth lib. You can find it at http://Twitteroauth.net */
10
require_once('OAuth.php');
13
* Twitter TwitterOAuth class
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';
36
/* Immediately retry the API call if the response was not successful. */
37
//public $retry = TRUE;
45
function accessTokenURL() { return 'https://api.twitter.com/oauth/access_token'; }
46
function authenticateURL() { return 'https://api.twitter.com/oauth/authenticate'; }
47
function authorizeURL() { return 'https://api.twitter.com/oauth/authorize'; }
48
function requestTokenURL() { return 'https://api.twitter.com/oauth/request_token'; }
53
function lastStatusCode() { return $this->http_status; }
54
function lastAPICall() { return $this->last_api_call; }
57
* construct TwitterTwitterOAuth object
59
function __construct($consumer_key, $consumer_secret, $oauth_token = NULL, $oauth_token_secret = NULL) {
60
$this->sha1_method = new TwitterOAuthSignatureMethod_HMAC_SHA1();
61
$this->consumer = new TwitterOAuthConsumer($consumer_key, $consumer_secret);
62
if (!empty($oauth_token) && !empty($oauth_token_secret)) {
63
$this->token = new TwitterOAuthConsumer($oauth_token, $oauth_token_secret);
71
* Get a request_token from Twitter
73
* @returns a key/value array containing oauth_token and oauth_token_secret
75
function getRequestToken($oauth_callback) {
76
$parameters = array();
77
$parameters['oauth_callback'] = $oauth_callback;
78
$request = $this->TwitteroAuthRequest($this->requestTokenURL(), 'GET', $parameters);
79
$token = TwitterOAuthUtil::parse_parameters($request);
80
Debug::Audit(var_export($token, true));
81
$this->token = new TwitterOAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
86
* Get the authorize URL
90
function getAuthorizeURL($token, $sign_in_with_twitter = TRUE) {
91
if (is_array($token)) {
92
$token = $token['oauth_token'];
94
if (empty($sign_in_with_twitter)) {
95
return $this->authorizeURL() . "?oauth_token={$token}";
97
return $this->authenticateURL() . "?oauth_token={$token}";
102
* Exchange request token and secret for an access token and
103
* secret, to sign API calls.
105
* @returns array("oauth_token" => "the-access-token",
106
* "oauth_token_secret" => "the-access-secret",
107
* "user_id" => "9436992",
108
* "screen_name" => "abraham")
110
function getAccessToken($Twitteroauth_verifier) {
111
$parameters = array();
112
$parameters['oauth_verifier'] = $Twitteroauth_verifier;
113
$request = $this->TwitteroAuthRequest($this->accessTokenURL(), 'GET', $parameters);
114
$token = TwitterOAuthUtil::parse_parameters($request);
115
Debug::Audit(var_export($token, true));
116
$this->token = new TwitterOAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
121
* One time exchange of username and password for access token and secret.
123
* @returns array("oauth_token" => "the-access-token",
124
* "oauth_token_secret" => "the-access-secret",
125
* "user_id" => "9436992",
126
* "screen_name" => "abraham",
127
* "x_auth_expires" => "0")
129
function getXAuthToken($username, $password) {
130
$parameters = array();
131
$parameters['x_auth_username'] = $username;
132
$parameters['x_auth_password'] = $password;
133
$parameters['x_auth_mode'] = 'client_auth';
134
$request = $this->TwitteroAuthRequest($this->accessTokenURL(), 'POST', $parameters);
135
$token = TwitterOAuthUtil::parse_parameters($request);
136
$this->token = new TwitterOAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
141
* GET wrapper for TwitteroAuthRequest.
143
function get($url, $parameters = array()) {
144
$response = $this->TwitteroAuthRequest($url, 'GET', $parameters);
145
if ($this->format === 'json' && $this->decode_json) {
146
return json_decode($response);
152
* POST wrapper for TwitteroAuthRequest.
154
function post($url, $parameters = array()) {
155
$response = $this->TwitteroAuthRequest($url, 'POST', $parameters);
156
if ($this->format === 'json' && $this->decode_json) {
157
return json_decode($response);
163
* DELETE wrapper for TwitteroAuthReqeust.
165
function delete($url, $parameters = array()) {
166
$response = $this->TwitteroAuthRequest($url, 'DELETE', $parameters);
167
if ($this->format === 'json' && $this->decode_json) {
168
return json_decode($response);
174
* Format and sign an TwitterOAuth / API request
176
function TwitteroAuthRequest($url, $method, $parameters) {
177
if (strrpos($url, 'https://') !== 0 && strrpos($url, 'http://') !== 0) {
178
$url = "{$this->host}{$url}.{$this->format}";
180
$request = TwitterOAuthRequest::from_consumer_and_token($this->consumer, $this->token, $method, $url, $parameters);
181
$request->sign_request($this->sha1_method, $this->consumer, $this->token);
184
return $this->http($request->to_url(), 'GET');
186
return $this->http($request->get_normalized_http_url(), $method, $request->to_postdata());
191
* Make an HTTP request
193
* @return API results
195
function http($url, $method, $postfields = NULL) {
196
$this->http_info = array();
199
curl_setopt($ci, CURLOPT_USERAGENT, $this->useragent);
200
curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, $this->connecttimeout);
201
curl_setopt($ci, CURLOPT_TIMEOUT, $this->timeout);
202
curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);
203
curl_setopt($ci, CURLOPT_HTTPHEADER, array('Expect:'));
204
curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, $this->ssl_verifypeer);
205
curl_setopt($ci, CURLOPT_HEADERFUNCTION, array($this, 'getHeader'));
206
curl_setopt($ci, CURLOPT_HEADER, FALSE);
210
curl_setopt($ci, CURLOPT_POST, TRUE);
211
if (!empty($postfields)) {
212
curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
216
curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE');
217
if (!empty($postfields)) {
218
$url = "{$url}?{$postfields}";
222
curl_setopt($ci, CURLOPT_URL, $url);
223
$response = curl_exec($ci);
224
$this->http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);
225
$this->http_info = array_merge($this->http_info, curl_getinfo($ci));
232
* Get the header info to store.
234
function getHeader($ch, $header) {
235
$i = strpos($header, ':');
237
$key = str_replace('-', '_', strtolower(substr($header, 0, $i)));
238
$value = trim(substr($header, $i + 2));
239
$this->http_header[$key] = $value;
241
return strlen($header);