4 * Twitter-API-PHP : Simple PHP wrapper for the v1.1 API
8 * @category Awesomeness
9 * @package Twitter-API-PHP
10 * @author James Mallison <me@j7mbo.co.uk>
11 * @license MIT License
13 * @link http://github.com/j7mbo/twitter-api-php
15 class TwitterAPIExchange
20 private $oauth_access_token;
25 private $oauth_access_token_secret;
30 private $consumer_key;
35 private $consumer_secret;
60 public $requestMethod;
63 * Create the API access object. Requires an array of settings::
64 * oauth access token, oauth access token secret, consumer key, consumer secret
65 * These are all available by creating your own application on dev.twitter.com
66 * Requires the cURL library
68 * @throws \Exception When cURL isn't installed or incorrect settings parameters are provided
70 * @param array $settings
72 public function __construct(array $settings)
74 if (!in_array('curl', get_loaded_extensions()))
76 throw new Exception('You need to install cURL, see: http://curl.haxx.se/docs/install.html');
79 if (!isset($settings['oauth_access_token'])
80 || !isset($settings['oauth_access_token_secret'])
81 || !isset($settings['consumer_key'])
82 || !isset($settings['consumer_secret']))
84 throw new Exception('Make sure you are passing in the correct parameters');
87 $this->oauth_access_token = $settings['oauth_access_token'];
88 $this->oauth_access_token_secret = $settings['oauth_access_token_secret'];
89 $this->consumer_key = $settings['consumer_key'];
90 $this->consumer_secret = $settings['consumer_secret'];
94 * Set postfields array, example: array('screen_name' => 'J7mbo')
96 * @param array $array Array of parameters to send to API
98 * @throws \Exception When you are trying to set both get and post fields
100 * @return TwitterAPIExchange Instance of self for method chaining
102 public function setPostfields(array $array)
104 if (!is_null($this->getGetfield()))
106 throw new Exception('You can only choose get OR post fields.');
109 if (isset($array['status']) && substr($array['status'], 0, 1) === '@')
111 $array['status'] = sprintf("\0%s", $array['status']);
114 foreach ($array as $key => &$value)
118 $value = ($value === true) ? 'true' : 'false';
122 $this->postfields = $array;
125 if (isset($this->oauth['oauth_signature'])) {
126 $this->buildOauth($this->url, $this->requestMethod);
133 * Set getfield string, example: '?screen_name=J7mbo'
135 * @param string $string Get key and value pairs as string
139 * @return \TwitterAPIExchange Instance of self for method chaining
141 public function setGetfield($string)
143 if (!is_null($this->getPostfields()))
145 throw new Exception('You can only choose get OR post fields.');
148 $getfields = preg_replace('/^\?/', '', explode('&', $string));
151 foreach ($getfields as $field)
155 list($key, $value) = explode('=', $field);
156 $params[$key] = $value;
160 $this->getfield = '?' . http_build_query($params);
166 * Get getfield string (simple getter)
168 * @return string $this->getfields
170 public function getGetfield()
172 return $this->getfield;
176 * Get postfields array (simple getter)
178 * @return array $this->postfields
180 public function getPostfields()
182 return $this->postfields;
186 * Build the Oauth object using params set in construct and additionals
187 * passed to this method. For v1.1, see: https://dev.twitter.com/docs/api/1.1
189 * @param string $url The API url to use. Example: https://api.twitter.com/1.1/search/tweets.json
190 * @param string $requestMethod Either POST or GET
194 * @return \TwitterAPIExchange Instance of self for method chaining
196 public function buildOauth($url, $requestMethod)
198 if (!in_array(strtolower($requestMethod), array('post', 'get')))
200 throw new Exception('Request method must be either POST or GET');
203 $consumer_key = $this->consumer_key;
204 $consumer_secret = $this->consumer_secret;
205 $oauth_access_token = $this->oauth_access_token;
206 $oauth_access_token_secret = $this->oauth_access_token_secret;
209 'oauth_consumer_key' => $consumer_key,
210 'oauth_nonce' => time(),
211 'oauth_signature_method' => 'HMAC-SHA1',
212 'oauth_token' => $oauth_access_token,
213 'oauth_timestamp' => time(),
214 'oauth_version' => '1.0'
217 $getfield = $this->getGetfield();
219 if (!is_null($getfield))
221 $getfields = str_replace('?', '', explode('&', $getfield));
223 foreach ($getfields as $g)
225 $split = explode('=', $g);
227 /** In case a null is passed through **/
228 if (isset($split[1]))
230 $oauth[$split[0]] = urldecode($split[1]);
235 $postfields = $this->getPostfields();
237 if (!is_null($postfields)) {
238 foreach ($postfields as $key => $value) {
239 $oauth[$key] = $value;
243 $base_info = $this->buildBaseString($url, $requestMethod, $oauth);
244 $composite_key = rawurlencode($consumer_secret) . '&' . rawurlencode($oauth_access_token_secret);
245 $oauth_signature = base64_encode(hash_hmac('sha1', $base_info, $composite_key, true));
246 $oauth['oauth_signature'] = $oauth_signature;
249 $this->requestMethod = $requestMethod;
250 $this->oauth = $oauth;
256 * Perform the actual data retrieval from the API
258 * @param boolean $return If true, returns data. This is left in for backward compatibility reasons
259 * @param array $curlOptions Additional Curl options for this request
263 * @return string json If $return param is true, returns json data.
265 public function performRequest($return = true, $curlOptions = array())
267 if (!is_bool($return))
269 throw new Exception('performRequest parameter must be true or false');
272 $header = array($this->buildAuthorizationHeader($this->oauth), 'Expect:');
274 $getfield = $this->getGetfield();
275 $postfields = $this->getPostfields();
278 CURLOPT_HTTPHEADER => $header,
279 CURLOPT_HEADER => false,
280 CURLOPT_URL => $this->url,
281 CURLOPT_RETURNTRANSFER => true,
282 CURLOPT_TIMEOUT => 10,
285 if (!is_null($postfields))
287 $options[CURLOPT_POSTFIELDS] = http_build_query($postfields);
291 if ($getfield !== '')
293 $options[CURLOPT_URL] .= $getfield;
298 curl_setopt_array($feed, $options);
299 $json = curl_exec($feed);
301 if (($error = curl_error($feed)) !== '')
305 throw new \Exception($error);
314 * Private method to generate the base string used by cURL
316 * @param string $baseURI
317 * @param string $method
318 * @param array $params
320 * @return string Built base string
322 private function buildBaseString($baseURI, $method, $params)
327 foreach($params as $key => $value)
329 $return[] = rawurlencode($key) . '=' . rawurlencode($value);
332 return $method . "&" . rawurlencode($baseURI) . '&' . rawurlencode(implode('&', $return));
336 * Private method to generate authorization header used by cURL
338 * @param array $oauth Array of oauth data generated by buildOauth()
340 * @return string $return Header used by cURL for request
342 private function buildAuthorizationHeader(array $oauth)
344 $return = 'Authorization: OAuth ';
347 foreach($oauth as $key => $value)
349 if (in_array($key, array('oauth_consumer_key', 'oauth_nonce', 'oauth_signature',
350 'oauth_signature_method', 'oauth_timestamp', 'oauth_token', 'oauth_version'))) {
351 $values[] = "$key=\"" . rawurlencode($value) . "\"";
355 $return .= implode(', ', $values);
360 * Helper method to perform our request
363 * @param string $method
364 * @param string $data
365 * @param array $curlOptions
369 * @return string The json response from the server
371 public function request($url, $method = 'get', $data = null, $curlOptions = array())
373 if (strtolower($method) === 'get')
375 $this->setGetfield($data);
379 $this->setPostfields($data);
382 return $this->buildOauth($url, $method)->performRequest(true, $curlOptions);