PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/samina/TwitterAPIExchange.php

https://gitlab.com/samina100/saviour
PHP | 263 lines | 153 code | 36 blank | 74 comment | 15 complexity | 5b1c276f2b5c734d26e22da8efbbc156 MD5 | raw file
  1. <?php
  2. /**
  3. * Twitter-API-PHP : Simple PHP wrapper for the v1.1 API
  4. *
  5. * PHP version 5.3.10
  6. *
  7. * @category Awesomeness
  8. * @package Twitter-API-PHP
  9. * @author James Mallison <me@j7mbo.co.uk>
  10. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  11. * @link http://github.com/j7mbo/twitter-api-php
  12. */
  13. class TwitterAPIExchange
  14. {
  15. private $oauth_access_token;
  16. private $oauth_access_token_secret;
  17. private $consumer_key;
  18. private $consumer_secret;
  19. private $postfields;
  20. private $getfield;
  21. protected $oauth;
  22. public $url;
  23. /**
  24. * Create the API access object. Requires an array of settings::
  25. * oauth access token, oauth access token secret, consumer key, consumer secret
  26. * These are all available by creating your own application on dev.twitter.com
  27. * Requires the cURL library
  28. *
  29. * @param array $settings
  30. */
  31. public function __construct(array $settings)
  32. {
  33. if (!in_array('curl', get_loaded_extensions()))
  34. {
  35. throw new Exception('You need to install cURL, see: http://curl.haxx.se/docs/install.html');
  36. }
  37. if (!isset($settings['oauth_access_token'])
  38. || !isset($settings['oauth_access_token_secret'])
  39. || !isset($settings['consumer_key'])
  40. || !isset($settings['consumer_secret']))
  41. {
  42. throw new Exception('Make sure you are passing in the correct parameters');
  43. }
  44. $this->oauth_access_token = $settings['oauth_access_token'];
  45. $this->oauth_access_token_secret = $settings['oauth_access_token_secret'];
  46. $this->consumer_key = $settings['consumer_key'];
  47. $this->consumer_secret = $settings['consumer_secret'];
  48. }
  49. /**
  50. * Set postfields array, example: array('screen_name' => 'J7mbo')
  51. *
  52. * @param array $array Array of parameters to send to API
  53. *
  54. * @return TwitterAPIExchange Instance of self for method chaining
  55. */
  56. public function setPostfields(array $array)
  57. {
  58. if (!is_null($this->getGetfield()))
  59. {
  60. throw new Exception('You can only choose get OR post fields.');
  61. }
  62. if (isset($array['status']) && substr($array['status'], 0, 1) === '@')
  63. {
  64. $array['status'] = sprintf("\0%s", $array['status']);
  65. }
  66. $this->postfields = $array;
  67. return $this;
  68. }
  69. /**
  70. * Set getfield string, example: '?screen_name=J7mbo'
  71. *
  72. * @param string $string Get key and value pairs as string
  73. *
  74. * @return \TwitterAPIExchange Instance of self for method chaining
  75. */
  76. public function setGetfield($string)
  77. {
  78. if (!is_null($this->getPostfields()))
  79. {
  80. throw new Exception('You can only choose get OR post fields.');
  81. }
  82. $search = array('#', ',', '+', ':');
  83. $replace = array('%23', '%2C', '%2B', '%3A');
  84. $string = str_replace($search, $replace, $string);
  85. $this->getfield = $string;
  86. return $this;
  87. }
  88. /**
  89. * Get getfield string (simple getter)
  90. *
  91. * @return string $this->getfields
  92. */
  93. public function getGetfield()
  94. {
  95. return $this->getfield;
  96. }
  97. /**
  98. * Get postfields array (simple getter)
  99. *
  100. * @return array $this->postfields
  101. */
  102. public function getPostfields()
  103. {
  104. return $this->postfields;
  105. }
  106. /**
  107. * Build the Oauth object using params set in construct and additionals
  108. * passed to this method. For v1.1, see: https://dev.twitter.com/docs/api/1.1
  109. *
  110. * @param string $url The API url to use. Example: https://api.twitter.com/1.1/search/tweets.json
  111. * @param string $requestMethod Either POST or GET
  112. * @return \TwitterAPIExchange Instance of self for method chaining
  113. */
  114. public function buildOauth($url, $requestMethod)
  115. {
  116. if (!in_array(strtolower($requestMethod), array('post', 'get')))
  117. {
  118. throw new Exception('Request method must be either POST or GET');
  119. }
  120. $consumer_key = $this->consumer_key;
  121. $consumer_secret = $this->consumer_secret;
  122. $oauth_access_token = $this->oauth_access_token;
  123. $oauth_access_token_secret = $this->oauth_access_token_secret;
  124. $oauth = array(
  125. 'oauth_consumer_key' => $consumer_key,
  126. 'oauth_nonce' => time(),
  127. 'oauth_signature_method' => 'HMAC-SHA1',
  128. 'oauth_token' => $oauth_access_token,
  129. 'oauth_timestamp' => time(),
  130. 'oauth_version' => '1.0'
  131. );
  132. $getfield = $this->getGetfield();
  133. if (!is_null($getfield))
  134. {
  135. $getfields = str_replace('?', '', explode('&', $getfield));
  136. foreach ($getfields as $g)
  137. {
  138. $split = explode('=', $g);
  139. $oauth[$split[0]] = $split[1];
  140. }
  141. }
  142. $base_info = $this->buildBaseString($url, $requestMethod, $oauth);
  143. $composite_key = rawurlencode($consumer_secret) . '&' . rawurlencode($oauth_access_token_secret);
  144. $oauth_signature = base64_encode(hash_hmac('sha1', $base_info, $composite_key, true));
  145. $oauth['oauth_signature'] = $oauth_signature;
  146. $this->url = $url;
  147. $this->oauth = $oauth;
  148. return $this;
  149. }
  150. /**
  151. * Perform the acual data retrieval from the API
  152. *
  153. * @param boolean $return If true, returns data.
  154. *
  155. * @return json If $return param is true, returns json data.
  156. */
  157. public function performRequest($return = true)
  158. {
  159. if (!is_bool($return))
  160. {
  161. throw new Exception('performRequest parameter must be true or false');
  162. }
  163. $header = array($this->buildAuthorizationHeader($this->oauth), 'Expect:');
  164. $getfield = $this->getGetfield();
  165. $postfields = $this->getPostfields();
  166. $options = array(
  167. CURLOPT_HTTPHEADER => $header,
  168. CURLOPT_HEADER => false,
  169. CURLOPT_URL => $this->url,
  170. CURLOPT_RETURNTRANSFER => true
  171. );
  172. if (!is_null($postfields))
  173. {
  174. $options[CURLOPT_POSTFIELDS] = $postfields;
  175. }
  176. else
  177. {
  178. if ($getfield !== '')
  179. {
  180. $options[CURLOPT_URL] .= $getfield;
  181. }
  182. }
  183. $feed = curl_init();
  184. curl_setopt_array($feed, $options);
  185. $json = curl_exec($feed);
  186. curl_close($feed);
  187. if ($return) { return $json; }
  188. }
  189. /**
  190. * Private method to generate the base string used by cURL
  191. *
  192. * @param string $baseURI
  193. * @param string $method
  194. * @param string $params
  195. *
  196. * @return string Built base string
  197. */
  198. private function buildBaseString($baseURI, $method, $params)
  199. {
  200. $return = array();
  201. ksort($params);
  202. foreach($params as $key=>$value)
  203. {
  204. $return[] = "$key=" . $value;
  205. }
  206. return $method . "&" . rawurlencode($baseURI) . '&' . rawurlencode(implode('&', $return));
  207. }
  208. /**
  209. * Private method to generate authorization header used by cURL
  210. *
  211. * @param array $oauth Array of oauth data generated by buildOauth()
  212. *
  213. * @return string $return Header used by cURL for request
  214. */
  215. private function buildAuthorizationHeader($oauth)
  216. {
  217. $return = 'Authorization: OAuth ';
  218. $values = array();
  219. foreach($oauth as $key => $value)
  220. {
  221. $values[] = "$key=\"" . rawurlencode($value) . "\"";
  222. }
  223. $return .= implode(', ', $values);
  224. return $return;
  225. }
  226. }
  227. ?>