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