· 6 years ago · Apr 04, 2019, 02:46 AM
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()) {
153 $response = $this->oAuthRequest($url, 'POST', $parameters);
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) {
175 if (strrpos($url, 'https://') !== 0 && strrpos($url, 'http://') !== 0) {
176 $url = "{$this->host}{$url}.{$this->format}";
177 }
178 $request = OAuthRequest::from_consumer_and_token($this->consumer, $this->token, $method, $url, $parameters);
179 $request->sign_request($this->sha1_method, $this->consumer, $this->token);
180 switch ($method) {
181 case 'GET':
182 return $this->http($request->to_url(), 'GET');
183 default:
184 return $this->http($request->get_normalized_http_url(), $method, $request->to_postdata());
185 }
186 }
187
188 /**
189 * Make an HTTP request
190 *
191 * @return API results
192 */
193 function http($url, $method, $postfields = NULL) {
194 $this->http_info = array();
195 $ci = curl_init();
196 /* Curl settings */
197 curl_setopt($ci, CURLOPT_USERAGENT, $this->useragent);
198 curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, $this->connecttimeout);
199 curl_setopt($ci, CURLOPT_TIMEOUT, $this->timeout);
200 curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);
201 curl_setopt($ci, CURLOPT_HTTPHEADER, array('Expect:'));
202 curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, $this->ssl_verifypeer);
203 curl_setopt($ci, CURLOPT_HEADERFUNCTION, array($this, 'getHeader'));
204 curl_setopt($ci, CURLOPT_HEADER, FALSE);
205
206 switch ($method) {
207 case 'POST':
208 curl_setopt($ci, CURLOPT_POST, TRUE);
209 if (!empty($postfields)) {
210 curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
211 }
212 break;
213 case 'DELETE':
214 curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE');
215 if (!empty($postfields)) {
216 $url = "{$url}?{$postfields}";
217 }
218 }
219
220 curl_setopt($ci, CURLOPT_URL, $url);
221 $response = curl_exec($ci);
222 $this->http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);
223 $this->http_info = array_merge($this->http_info, curl_getinfo($ci));
224 $this->url = $url;
225 curl_close ($ci);
226 return $response;
227 }
228
229 /**
230 * Get the header info to store.
231 */
232 function getHeader($ch, $header) {
233 $i = strpos($header, ':');
234 if (!empty($i)) {
235 $key = str_replace('-', '_', strtolower(substr($header, 0, $i)));
236 $value = trim(substr($header, $i + 2));
237 $this->http_header[$key] = $value;
238 }
239 return strlen($header);
240 }
241}