TwitterAPIExchange

Twitter-API-PHP : Simple PHP wrapper for the v1.1 API.

Defined (1)

The class is defined in the following location(s).

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