4 * Abraham Williams (abraham@abrah.am) http://abrah.am
6 * The first PHP Library to support OAuth for Twitter's REST API.
8 * Version 0.2.0 kept for compatibility purpose with StatusNetOAuth
11 use Friendica\Security\OAuth1\OAuthConsumer;
12 use Friendica\Security\OAuth1\OAuthRequest;
13 use Friendica\Security\OAuth1\Signature\OAuthSignatureMethod_HMAC_SHA1;
14 use Friendica\Security\OAuth1\OAuthToken;
15 use Friendica\Security\OAuth1\OAuthUtil;
22 /* Set up the API root URL. */
23 public $host = "https://api.twitter.com/1.1/";
24 /* Set timeout default. */
26 /* Set connect timeout. */
27 public $connecttimeout = 30;
28 /* Verify SSL Cert. */
29 public $ssl_verifypeer = FALSE;
30 /* Response format. */
31 public $format = 'json';
32 /* Decode returned json data. */
33 public $decode_json = TRUE;
34 /* Set the useragent. */
35 public $useragent = 'TwitterOAuth v0.2.0-beta2';
37 /* Contains the last HTTP status code returned. */
39 /* Contains the last API call. */
42 * Contains the last HTTP headers returned.
47 * Contains the last HTTP request info
52 /** @var OAuthToken */
54 /** @var OAuthConsumer */
56 /** @var \Friendica\Security\OAuth1\Signature\OAuthSignatureMethod_HMAC_SHA1 */
62 function accessTokenURL()
64 return 'https://api.twitter.com/oauth/access_token';
67 function authenticateURL()
69 return 'https://twitter.com/oauth/authenticate';
72 function authorizeURL()
74 return 'https://twitter.com/oauth/authorize';
77 function requestTokenURL()
79 return 'https://api.twitter.com/oauth/request_token';
82 function __construct($consumer_key, $consumer_secret, $oauth_token = null, $oauth_token_secret = null)
84 $this->sha1_method = new OAuthSignatureMethod_HMAC_SHA1();
85 $this->consumer = new OAuthConsumer($consumer_key, $consumer_secret);
86 if (!empty($oauth_token) && !empty($oauth_token_secret)) {
87 $this->token = new OAuthToken($oauth_token, $oauth_token_secret);
96 * @param callback $oauth_callback
99 function getRequestToken($oauth_callback = null)
102 if (!empty($oauth_callback)) {
103 $parameters['oauth_callback'] = $oauth_callback;
106 $request = $this->oAuthRequest($this->requestTokenURL(), 'GET', $parameters);
107 $token = OAuthUtil::parse_parameters($request);
108 $this->token = new OAuthToken($token['oauth_token'], $token['oauth_token_secret']);
113 * Get the authorize URL
115 * @param array $token
116 * @param bool $sign_in_with_tumblr
119 function getAuthorizeURL($token, $sign_in_with_twitter = TRUE)
121 if (is_array($token)) {
122 $token = $token['oauth_token'];
125 if (empty($sign_in_with_twitter)) {
126 return $this->authorizeURL() . "?oauth_token={$token}";
128 return $this->authenticateURL() . "?oauth_token={$token}";
133 * Exchange request token and secret for an access token and
134 * secret, to sign API calls.
136 * @param bool $oauth_verifier
137 * @return array ("oauth_token" => "the-access-token",
138 * "oauth_token_secret" => "the-access-secret",
139 * "user_id" => "9436992",
140 * "screen_name" => "abraham")
142 function getAccessToken($oauth_verifier = FALSE)
145 if (!empty($oauth_verifier)) {
146 $parameters['oauth_verifier'] = $oauth_verifier;
149 $request = $this->oAuthRequest($this->accessTokenURL(), 'GET', $parameters);
150 $token = OAuthUtil::parse_parameters($request);
151 $this->token = new OAuthToken($token['oauth_token'], $token['oauth_token_secret']);
157 * One time exchange of username and password for access token and secret.
159 * @param string $username
160 * @param string $password
161 * @return array ("oauth_token" => "the-access-token",
162 * "oauth_token_secret" => "the-access-secret",
163 * "user_id" => "9436992",
164 * "screen_name" => "abraham",
165 * "x_auth_expires" => "0")
167 function getXAuthToken($username, $password)
170 $parameters['x_auth_username'] = $username;
171 $parameters['x_auth_password'] = $password;
172 $parameters['x_auth_mode'] = 'client_auth';
173 $request = $this->oAuthRequest($this->accessTokenURL(), 'POST', $parameters);
174 $token = OAuthUtil::parse_parameters($request);
175 $this->token = new OAuthToken($token['oauth_token'], $token['oauth_token_secret']);
181 * GET wrapper for oAuthRequest.
184 * @param array $parameters
185 * @return mixed|string
187 function get($url, $parameters = [])
189 $response = $this->oAuthRequest($url, 'GET', $parameters);
190 if ($this->format === 'json' && $this->decode_json) {
191 return json_decode($response);
198 * POST wrapper for oAuthRequest.
201 * @param array $parameters
202 * @return mixed|string
204 function post($url, $parameters = [])
206 $response = $this->oAuthRequest($url, 'POST', $parameters);
207 if ($this->format === 'json' && $this->decode_json) {
208 return json_decode($response);
215 * DELETE wrapper for oAuthReqeust.
218 * @param array $parameters
219 * @return mixed|string
221 function delete($url, $parameters = [])
223 $response = $this->oAuthRequest($url, 'DELETE', $parameters);
224 if ($this->format === 'json' && $this->decode_json) {
225 return json_decode($response);
232 * Format and sign an OAuth / API request
235 * @param string $method
236 * @param array $parameters
237 * @return mixed|string
239 function oAuthRequest($url, $method, $parameters)
241 if (strrpos($url, 'https://') !== 0 && strrpos($url, 'http://') !== 0) {
242 $url = "{$this->host}{$url}.{$this->format}";
245 $request = OAuthRequest::from_consumer_and_token($this->consumer, $method, $url, $parameters, $this->token);
246 $request->sign_request($this->sha1_method, $this->consumer, $this->token);
249 return $this->http($request->to_url(), 'GET');
251 return $this->http($request->get_normalized_http_url(), 'POST', $request->to_postdata(true));
253 return $this->http($request->get_normalized_http_url(), $method, $request->to_postdata());
258 * Make an HTTP request
261 * @param string $method
262 * @param mixed $postfields
263 * @return string API results
265 function http($url, $method, $postfields = null)
267 $this->http_info = [];
270 curl_setopt($ci, CURLOPT_USERAGENT, $this->useragent);
271 curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, $this->connecttimeout);
272 curl_setopt($ci, CURLOPT_TIMEOUT, $this->timeout);
273 curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);
274 curl_setopt($ci, CURLOPT_HTTPHEADER, array('Expect:'));
275 curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, $this->ssl_verifypeer);
276 curl_setopt($ci, CURLOPT_HEADERFUNCTION, array($this, 'getHeader'));
277 curl_setopt($ci, CURLOPT_HEADER, FALSE);
281 curl_setopt($ci, CURLOPT_POST, TRUE);
282 if (!empty($postfields)) {
283 curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
287 curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE');
288 if (!empty($postfields)) {
289 $url = "{$url}?{$postfields}";
293 curl_setopt($ci, CURLOPT_URL, $url);
294 $response = curl_exec($ci);
295 $this->http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);
296 $this->http_info = array_merge($this->http_info, curl_getinfo($ci));
304 * Get the header info to store.
306 * @param resource $ch
307 * @param string $header
310 function getHeader($ch, $header)
312 $i = strpos($header, ':');
314 $key = str_replace('-', '_', strtolower(substr($header, 0, $i)));
315 $value = trim(substr($header, $i + 2));
316 $this->http_header[$key] = $value;
319 return strlen($header);