4 * Abraham Williams (abraham@abrah.am) http://abrah.am
6 * The first PHP Library to support OAuth for Twitter's REST API.
9 /* Load OAuth lib. You can find it at http://oauth.net */
11 if (strpos($_SERVER['SERVER_NAME'], 'localhost')) {
12 $abs_path = $_SERVER['DOCUMENT_ROOT'];
14 $abs_path = '/var/www/static';
17 require_once($abs_path . '/2015/assets/js/tweetie/api/twitteroauth/OAuth.php');
23 /* Contains the last HTTP status code returned. */
25 /* Contains the last API call. */
27 /* Set up the API root URL. */
28 public $host = "https://api.twitter.com/1.1/";
29 /* Set timeout default. */
31 /* Set connect timeout. */
32 public $connecttimeout = 30;
33 /* Verify SSL Cert. */
34 public $ssl_verifypeer = FALSE;
36 public $format = 'json';
37 /* Decode returned json data. */
38 public $decode_json = TRUE;
39 /* Contains the last HTTP headers returned. */
41 /* Set the useragnet. */
42 public $useragent = 'TwitterOAuth v0.2.0-beta2';
43 /* Immediately retry the API call if the response was not successful. */
44 //public $retry = TRUE;
52 function accessTokenURL() { return 'https://api.twitter.com/oauth/access_token'; }
53 function authenticateURL() { return 'https://api.twitter.com/oauth/authenticate'; }
54 function authorizeURL() { return 'https://api.twitter.com/oauth/authorize'; }
55 function requestTokenURL() { return 'https://api.twitter.com/oauth/request_token'; }
60 function lastStatusCode() { return $this->http_status
; }
61 function lastAPICall() { return $this->last_api_call
; }
64 * construct TwitterOAuth object
66 function __construct($consumer_key, $consumer_secret, $oauth_token = NULL, $oauth_token_secret = NULL) {
67 $this->sha1_method
= new OAuthSignatureMethod_HMAC_SHA1();
68 $this->consumer
= new OAuthConsumer($consumer_key, $consumer_secret);
69 if (!empty($oauth_token) && !empty($oauth_token_secret)) {
70 $this->token
= new OAuthConsumer($oauth_token, $oauth_token_secret);
78 * Get a request_token from Twitter
80 * @returns a key/value array containing oauth_token and oauth_token_secret
82 function getRequestToken($oauth_callback) {
83 $parameters = array();
84 $parameters['oauth_callback'] = $oauth_callback;
85 $request = $this->oAuthRequest($this->requestTokenURL(), 'GET', $parameters);
86 $token = OAuthUtil
::parse_parameters($request);
87 $this->token
= new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
92 * Get the authorize URL
96 function getAuthorizeURL($token, $sign_in_with_twitter = TRUE) {
97 if (is_array($token)) {
98 $token = $token['oauth_token'];
100 if (empty($sign_in_with_twitter)) {
101 return $this->authorizeURL() . "?oauth_token={$token}";
103 return $this->authenticateURL() . "?oauth_token={$token}";
108 * Exchange request token and secret for an access token and
109 * secret, to sign API calls.
111 * @returns array("oauth_token" => "the-access-token",
112 * "oauth_token_secret" => "the-access-secret",
113 * "user_id" => "9436992",
114 * "screen_name" => "abraham")
116 function getAccessToken($oauth_verifier) {
117 $parameters = array();
118 $parameters['oauth_verifier'] = $oauth_verifier;
119 $request = $this->oAuthRequest($this->accessTokenURL(), 'GET', $parameters);
120 $token = OAuthUtil
::parse_parameters($request);
121 $this->token
= new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
126 * One time exchange of username and password for access token and secret.
128 * @returns array("oauth_token" => "the-access-token",
129 * "oauth_token_secret" => "the-access-secret",
130 * "user_id" => "9436992",
131 * "screen_name" => "abraham",
132 * "x_auth_expires" => "0")
134 function getXAuthToken($username, $password) {
135 $parameters = array();
136 $parameters['x_auth_username'] = $username;
137 $parameters['x_auth_password'] = $password;
138 $parameters['x_auth_mode'] = 'client_auth';
139 $request = $this->oAuthRequest($this->accessTokenURL(), 'POST', $parameters);
140 $token = OAuthUtil
::parse_parameters($request);
141 $this->token
= new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
146 * GET wrapper for oAuthRequest.
148 function get($url, $parameters = array()) {
149 $response = $this->oAuthRequest($url, 'GET', $parameters);
150 if ($this->format
=== 'json' && $this->decode_json
) {
151 return json_decode($response);
157 * POST wrapper for oAuthRequest.
159 function post($url, $parameters = array()) {
160 $response = $this->oAuthRequest($url, 'POST', $parameters);
161 if ($this->format
=== 'json' && $this->decode_json
) {
162 return json_decode($response);
168 * DELETE wrapper for oAuthReqeust.
170 function delete($url, $parameters = array()) {
171 $response = $this->oAuthRequest($url, 'DELETE', $parameters);
172 if ($this->format
=== 'json' && $this->decode_json
) {
173 return json_decode($response);
179 * Format and sign an OAuth / API request
181 function oAuthRequest($url, $method, $parameters) {
182 if (strrpos($url, 'https://') !== 0 && strrpos($url, 'http://') !== 0) {
183 $url = "{$this->host}{$url}.{$this->format}";
185 $request = OAuthRequest
::from_consumer_and_token($this->consumer
, $this->token
, $method, $url, $parameters);
186 $request->sign_request($this->sha1_method
, $this->consumer
, $this->token
);
189 return $this->http($request->to_url(), 'GET');
191 return $this->http($request->get_normalized_http_url(), $method, $request->to_postdata());
196 * Make an HTTP request
198 * @return API results
200 function http($url, $method, $postfields = NULL) {
201 $this->http_info
= array();
204 curl_setopt($ci, CURLOPT_USERAGENT
, $this->useragent
);
205 curl_setopt($ci, CURLOPT_CONNECTTIMEOUT
, $this->connecttimeout
);
206 curl_setopt($ci, CURLOPT_TIMEOUT
, $this->timeout
);
207 curl_setopt($ci, CURLOPT_RETURNTRANSFER
, TRUE);
208 curl_setopt($ci, CURLOPT_HTTPHEADER
, array('Expect:'));
209 curl_setopt($ci, CURLOPT_SSL_VERIFYPEER
, $this->ssl_verifypeer
);
210 curl_setopt($ci, CURLOPT_HEADERFUNCTION
, array($this, 'getHeader'));
211 curl_setopt($ci, CURLOPT_HEADER
, FALSE);
215 curl_setopt($ci, CURLOPT_POST
, TRUE);
216 if (!empty($postfields)) {
217 curl_setopt($ci, CURLOPT_POSTFIELDS
, $postfields);
221 curl_setopt($ci, CURLOPT_CUSTOMREQUEST
, 'DELETE');
222 if (!empty($postfields)) {
223 $url = "{$url}?{$postfields}";
227 curl_setopt($ci, CURLOPT_URL
, $url);
228 $response = curl_exec($ci);
229 $this->http_code
= curl_getinfo($ci, CURLINFO_HTTP_CODE
);
230 $this->http_info
= array_merge($this->http_info
, curl_getinfo($ci));
237 * Get the header info to store.
239 function getHeader($ch, $header) {
240 $i = strpos($header, ':');
242 $key = str_replace('-', '_', strtolower(substr($header, 0, $i)));
243 $value = trim(substr($header, $i +
2));
244 $this->http_header
[$key] = $value;
246 return strlen($header);