BioTorrents.de’s version of Gazelle
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TwitterAPIExchange.php 11KB

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