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