· 7 years ago · Nov 17, 2017, 04:32 PM
1<?php
2
3namespace Drupal\twitter_profile_widget\Resources\j7mbo\twitter_api_php;
4
5/**
6 * Twitter-API-PHP : Simple PHP wrapper for the v1.1 API
7 *
8 * PHP version 5.3.10
9 *
10 * @category Awesomeness
11 * @package Twitter-API-PHP
12 * @author James Mallison <me@j7mbo.co.uk>
13 * @license MIT License
14 * @version 1.0.4
15 * @link http://github.com/j7mbo/twitter-api-php
16 */
17class TwitterAPIExchange
18{
19 /**
20 * @var string
21 */
22 private $oauth_access_token;
23
24 /**
25 * @var string
26 */
27 private $oauth_access_token_secret;
28
29 /**
30 * @var string
31 */
32 private $consumer_key;
33
34 /**
35 * @var string
36 */
37 private $consumer_secret;
38
39 /**
40 * @var array
41 */
42 private $postfields;
43
44 /**
45 * @var string
46 */
47 private $getfield;
48
49 /**
50 * @var mixed
51 */
52 protected $oauth;
53
54 /**
55 * @var string
56 */
57 public $url;
58
59 /**
60 * @var string
61 */
62 public $requestMethod;
63
64 /**
65 * The HTTP status code from the previous request
66 *
67 * @var int
68 */
69 protected $httpStatusCode;
70
71 /**
72 * Create the API access object. Requires an array of settings::
73 * oauth access token, oauth access token secret, consumer key, consumer secret
74 * These are all available by creating your own application on dev.twitter.com
75 * Requires the cURL library
76 *
77 * @throws \RuntimeException When cURL isn't loaded
78 * @throws \InvalidArgumentException When incomplete settings parameters are provided
79 *
80 * @param array $settings
81 */
82 public function __construct(array $settings)
83 {
84 if (!function_exists('curl_init'))
85 {
86 throw new RuntimeException('TwitterAPIExchange requires cURL extension to be loaded, see: http://curl.haxx.se/docs/install.html');
87 }
88
89 if (!isset($settings['oauth_access_token'])
90 || !isset($settings['oauth_access_token_secret'])
91 || !isset($settings['consumer_key'])
92 || !isset($settings['consumer_secret']))
93 {
94 throw new InvalidArgumentException('Incomplete settings passed to TwitterAPIExchange');
95 }
96
97 $this->oauth_access_token = $settings['oauth_access_token'];
98 $this->oauth_access_token_secret = $settings['oauth_access_token_secret'];
99 $this->consumer_key = $settings['consumer_key'];
100 $this->consumer_secret = $settings['consumer_secret'];
101 }
102
103 /**
104 * Set postfields array, example: ['screen_name' => 'J7mbo']
105 *
106 * @param array $array Array of parameters to send to API
107 *
108 * @throws \Exception When you are trying to set both get and post fields
109 *
110 * @return TwitterAPIExchange Instance of self for method chaining
111 */
112 public function setPostfields(array $array)
113 {
114 if (!is_null($this->getGetfield()))
115 {
116 throw new Exception('You can only choose get OR post fields.');
117 }
118
119 if (isset($array['status']) && substr($array['status'], 0, 1) === '@')
120 {
121 $array['status'] = sprintf("\0%s", $array['status']);
122 }
123
124 foreach ($array as $key => &$value)
125 {
126 if (is_bool($value))
127 {
128 $value = ($value === true) ? 'true' : 'false';
129 }
130 }
131
132 $this->postfields = $array;
133
134 // rebuild oAuth
135 if (isset($this->oauth['oauth_signature'])) {
136 $this->buildOauth($this->url, $this->requestMethod);
137 }
138
139 return $this;
140 }
141
142 /**
143 * Set getfield string, example: '?screen_name=J7mbo'
144 *
145 * @param string $string Get key and value pairs as string
146 *
147 * @throws \Exception
148 *
149 * @return \TwitterAPIExchange Instance of self for method chaining
150 */
151 public function setGetfield($string)
152 {
153 if (!is_null($this->getPostfields()))
154 {
155 throw new Exception('You can only choose get OR post fields.');
156 }
157
158 $getfields = preg_replace('/^\?/', '', explode('&', $string));
159 $params = [];
160
161 foreach ($getfields as $field)
162 {
163 if ($field !== '')
164 {
165 list($key, $value) = explode('=', $field);
166 $params[$key] = $value;
167 }
168 }
169
170 $this->getfield = '?' . http_build_query($params);
171
172 return $this;
173 }
174
175 /**
176 * Get getfield string (simple getter)
177 *
178 * @return string $this->getfields
179 */
180 public function getGetfield()
181 {
182 return $this->getfield;
183 }
184
185 /**
186 * Get postfields array (simple getter)
187 *
188 * @return array $this->postfields
189 */
190 public function getPostfields()
191 {
192 return $this->postfields;
193 }
194
195 /**
196 * Build the Oauth object using params set in construct and additionals
197 * passed to this method. For v1.1, see: https://dev.twitter.com/docs/api/1.1
198 *
199 * @param string $url The API url to use. Example: https://api.twitter.com/1.1/search/tweets.json
200 * @param string $requestMethod Either POST or GET
201 *
202 * @throws \Exception
203 *
204 * @return \TwitterAPIExchange Instance of self for method chaining
205 */
206 public function buildOauth($url, $requestMethod)
207 {
208 if (!in_array(strtolower($requestMethod), ['post', 'get']))
209 {
210 throw new Exception('Request method must be either POST or GET');
211 }
212
213 $consumer_key = $this->consumer_key;
214 $consumer_secret = $this->consumer_secret;
215 $oauth_access_token = $this->oauth_access_token;
216 $oauth_access_token_secret = $this->oauth_access_token_secret;
217
218 $oauth = [
219 'oauth_consumer_key' => $consumer_key,
220 'oauth_nonce' => time(),
221 'oauth_signature_method' => 'HMAC-SHA1',
222 'oauth_token' => $oauth_access_token,
223 'oauth_timestamp' => time(),
224 'oauth_version' => '1.0'
225 ];
226
227 $getfield = $this->getGetfield();
228
229 if (!is_null($getfield))
230 {
231 $getfields = str_replace('?', '', explode('&', $getfield));
232
233 foreach ($getfields as $g)
234 {
235 $split = explode('=', $g);
236
237 /** In case a null is passed through **/
238 if (isset($split[1]))
239 {
240 $oauth[$split[0]] = urldecode($split[1]);
241 }
242 }
243 }
244
245 $postfields = $this->getPostfields();
246
247 if (!is_null($postfields)) {
248 foreach ($postfields as $key => $value) {
249 $oauth[$key] = $value;
250 }
251 }
252
253 $base_info = $this->buildBaseString($url, $requestMethod, $oauth);
254 $composite_key = rawurlencode($consumer_secret) . '&' . rawurlencode($oauth_access_token_secret);
255 $oauth_signature = base64_encode(hash_hmac('sha1', $base_info, $composite_key, true));
256 $oauth['oauth_signature'] = $oauth_signature;
257
258 $this->url = $url;
259 $this->requestMethod = $requestMethod;
260 $this->oauth = $oauth;
261
262 return $this;
263 }
264
265 /**
266 * Perform the actual data retrieval from the API
267 *
268 * @param boolean $return If true, returns data. This is left in for backward compatibility reasons
269 * @param array $curlOptions Additional Curl options for this request
270 *
271 * @throws \Exception
272 *
273 * @return string json If $return param is true, returns json data.
274 */
275 public function performRequest($return = true, $curlOptions = [])
276 {
277 if (!is_bool($return))
278 {
279 throw new Exception('performRequest parameter must be true or false');
280 }
281
282 $header = [$this->buildAuthorizationHeader($this->oauth), 'Expect:'];
283
284 $getfield = $this->getGetfield();
285 $postfields = $this->getPostfields();
286
287 $options = [
288 CURLOPT_HTTPHEADER => $header,
289 CURLOPT_HEADER => false,
290 CURLOPT_URL => $this->url,
291 CURLOPT_RETURNTRANSFER => true,
292 CURLOPT_TIMEOUT => 10,
293 CURLOPT_SSL_VERIFYPEER => false
294 ] + $curlOptions;
295
296 if (!is_null($postfields))
297 {
298 $options[CURLOPT_POSTFIELDS] = http_build_query($postfields);
299 }
300 else
301 {
302 if ($getfield !== '')
303 {
304 $options[CURLOPT_URL] .= $getfield;
305 }
306 }
307
308 $feed = curl_init();
309 curl_setopt_array($feed, $options);
310 $json = curl_exec($feed);
311
312 $this->httpStatusCode = curl_getinfo($feed, CURLINFO_HTTP_CODE);
313
314 if (($error = curl_error($feed)) !== '')
315 {
316 curl_close($feed);
317
318 throw new \Exception($error);
319 }
320
321 curl_close($feed);
322
323 return $json;
324 }
325
326 /**
327 * Private method to generate the base string used by cURL
328 *
329 * @param string $baseURI
330 * @param string $method
331 * @param array $params
332 *
333 * @return string Built base string
334 */
335 private function buildBaseString($baseURI, $method, $params)
336 {
337 $return = [];
338 ksort($params);
339
340 foreach($params as $key => $value)
341 {
342 $return[] = rawurlencode($key) . '=' . rawurlencode($value);
343 }
344
345 return $method . "&" . rawurlencode($baseURI) . '&' . rawurlencode(implode('&', $return));
346 }
347
348 /**
349 * Private method to generate authorization header used by cURL
350 *
351 * @param array $oauth Array of oauth data generated by buildOauth()
352 *
353 * @return string $return Header used by cURL for request
354 */
355 private function buildAuthorizationHeader(array $oauth)
356 {
357 $return = 'Authorization: OAuth ';
358 $values = [];
359
360 foreach($oauth as $key => $value)
361 {
362 if (in_array($key, ['oauth_consumer_key', 'oauth_nonce', 'oauth_signature',
363 'oauth_signature_method', 'oauth_timestamp', 'oauth_token', 'oauth_version'])) {
364 $values[] = "$key=\"" . rawurlencode($value) . "\"";
365 }
366 }
367
368 $return .= implode(', ', $values);
369 return $return;
370 }
371
372 /**
373 * Helper method to perform our request
374 *
375 * @param string $url
376 * @param string $method
377 * @param string $data
378 * @param array $curlOptions
379 *
380 * @throws \Exception
381 *
382 * @return string The json response from the server
383 */
384 public function request($url, $method = 'get', $data = null, $curlOptions = [])
385 {
386 if (strtolower($method) === 'get')
387 {
388 $this->setGetfield($data);
389 }
390 else
391 {
392 $this->setPostfields($data);
393 }
394
395 return $this->buildOauth($url, $method)->performRequest(true, $curlOptions);
396 }
397
398 /**
399 * Get the HTTP status code for the previous request
400 *
401 * @return integer
402 */
403 public function getHttpStatusCode()
404 {
405 return $this->httpStatusCode;
406 }
407}