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