Skip to content
This repository was archived by the owner on Jan 15, 2025. It is now read-only.

Commit 2673208

Browse files
committed
Merge pull request #158 from finwe/constructor-exceptions
Better constructor exceptions and messages, faster curl detection
2 parents 05651bc + 45e59c7 commit 2673208

File tree

1 file changed

+54
-53
lines changed

1 file changed

+54
-53
lines changed

TwitterAPIExchange.php

Lines changed: 54 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
/**
44
* Twitter-API-PHP : Simple PHP wrapper for the v1.1 API
5-
*
5+
*
66
* PHP version 5.3.10
7-
*
7+
*
88
* @category Awesomeness
99
* @package Twitter-API-PHP
1010
* @author James Mallison <me@j7mbo.co.uk>
@@ -61,7 +61,7 @@ class TwitterAPIExchange
6161

6262
/**
6363
* The HTTP status code from the previous request
64-
*
64+
*
6565
* @var int
6666
*/
6767
protected $httpStatusCode;
@@ -72,23 +72,24 @@ class TwitterAPIExchange
7272
* These are all available by creating your own application on dev.twitter.com
7373
* Requires the cURL library
7474
*
75-
* @throws \Exception When cURL isn't installed or incorrect settings parameters are provided
75+
* @throws \RuntimeException When cURL isn't loaded
76+
* @throws \InvalidArgumentException When incomplete settings parameters are provided
7677
*
7778
* @param array $settings
7879
*/
7980
public function __construct(array $settings)
8081
{
81-
if (!in_array('curl', get_loaded_extensions()))
82+
if (!function_exists('curl_init'))
8283
{
83-
throw new Exception('You need to install cURL, see: http://curl.haxx.se/docs/install.html');
84+
throw new RuntimeException('TwitterAPIExchange requires cURL extension to be loaded, see: http://curl.haxx.se/docs/install.html');
8485
}
85-
86+
8687
if (!isset($settings['oauth_access_token'])
8788
|| !isset($settings['oauth_access_token_secret'])
8889
|| !isset($settings['consumer_key'])
8990
|| !isset($settings['consumer_secret']))
9091
{
91-
throw new Exception('Make sure you are passing in the correct parameters');
92+
throw new InvalidArgumentException('Incomplete settings passed to TwitterAPIExchange');
9293
}
9394

9495
$this->oauth_access_token = $settings['oauth_access_token'];
@@ -108,11 +109,11 @@ public function __construct(array $settings)
108109
*/
109110
public function setPostfields(array $array)
110111
{
111-
if (!is_null($this->getGetfield()))
112-
{
113-
throw new Exception('You can only choose get OR post fields.');
112+
if (!is_null($this->getGetfield()))
113+
{
114+
throw new Exception('You can only choose get OR post fields.');
114115
}
115-
116+
116117
if (isset($array['status']) && substr($array['status'], 0, 1) === '@')
117118
{
118119
$array['status'] = sprintf("\0%s", $array['status']);
@@ -125,33 +126,33 @@ public function setPostfields(array $array)
125126
$value = ($value === true) ? 'true' : 'false';
126127
}
127128
}
128-
129+
129130
$this->postfields = $array;
130-
131+
131132
// rebuild oAuth
132133
if (isset($this->oauth['oauth_signature'])) {
133134
$this->buildOauth($this->url, $this->requestMethod);
134135
}
135136

136137
return $this;
137138
}
138-
139+
139140
/**
140141
* Set getfield string, example: '?screen_name=J7mbo'
141-
*
142+
*
142143
* @param string $string Get key and value pairs as string
143144
*
144145
* @throws \Exception
145-
*
146+
*
146147
* @return \TwitterAPIExchange Instance of self for method chaining
147148
*/
148149
public function setGetfield($string)
149150
{
150-
if (!is_null($this->getPostfields()))
151-
{
152-
throw new Exception('You can only choose get OR post fields.');
151+
if (!is_null($this->getPostfields()))
152+
{
153+
throw new Exception('You can only choose get OR post fields.');
153154
}
154-
155+
155156
$getfields = preg_replace('/^\?/', '', explode('&', $string));
156157
$params = array();
157158

@@ -165,30 +166,30 @@ public function setGetfield($string)
165166
}
166167

167168
$this->getfield = '?' . http_build_query($params);
168-
169+
169170
return $this;
170171
}
171-
172+
172173
/**
173174
* Get getfield string (simple getter)
174-
*
175+
*
175176
* @return string $this->getfields
176177
*/
177178
public function getGetfield()
178179
{
179180
return $this->getfield;
180181
}
181-
182+
182183
/**
183184
* Get postfields array (simple getter)
184-
*
185+
*
185186
* @return array $this->postfields
186187
*/
187188
public function getPostfields()
188189
{
189190
return $this->postfields;
190191
}
191-
192+
192193
/**
193194
* Build the Oauth object using params set in construct and additionals
194195
* passed to this method. For v1.1, see: https://dev.twitter.com/docs/api/1.1
@@ -206,12 +207,12 @@ public function buildOauth($url, $requestMethod)
206207
{
207208
throw new Exception('Request method must be either POST or GET');
208209
}
209-
210+
210211
$consumer_key = $this->consumer_key;
211212
$consumer_secret = $this->consumer_secret;
212213
$oauth_access_token = $this->oauth_access_token;
213214
$oauth_access_token_secret = $this->oauth_access_token_secret;
214-
215+
215216
$oauth = array(
216217
'oauth_consumer_key' => $consumer_key,
217218
'oauth_nonce' => time(),
@@ -220,9 +221,9 @@ public function buildOauth($url, $requestMethod)
220221
'oauth_timestamp' => time(),
221222
'oauth_version' => '1.0'
222223
);
223-
224+
224225
$getfield = $this->getGetfield();
225-
226+
226227
if (!is_null($getfield))
227228
{
228229
$getfields = str_replace('?', '', explode('&', $getfield));
@@ -238,7 +239,7 @@ public function buildOauth($url, $requestMethod)
238239
}
239240
}
240241
}
241-
242+
242243
$postfields = $this->getPostfields();
243244

244245
if (!is_null($postfields)) {
@@ -251,22 +252,22 @@ public function buildOauth($url, $requestMethod)
251252
$composite_key = rawurlencode($consumer_secret) . '&' . rawurlencode($oauth_access_token_secret);
252253
$oauth_signature = base64_encode(hash_hmac('sha1', $base_info, $composite_key, true));
253254
$oauth['oauth_signature'] = $oauth_signature;
254-
255+
255256
$this->url = $url;
256257
$this->requestMethod = $requestMethod;
257258
$this->oauth = $oauth;
258-
259+
259260
return $this;
260261
}
261-
262+
262263
/**
263264
* Perform the actual data retrieval from the API
264-
*
265+
*
265266
* @param boolean $return If true, returns data. This is left in for backward compatibility reasons
266267
* @param array $curlOptions Additional Curl options for this request
267268
*
268269
* @throws \Exception
269-
*
270+
*
270271
* @return string json If $return param is true, returns json data.
271272
*/
272273
public function performRequest($return = true, $curlOptions = array())
@@ -304,7 +305,7 @@ public function performRequest($return = true, $curlOptions = array())
304305
$feed = curl_init();
305306
curl_setopt_array($feed, $options);
306307
$json = curl_exec($feed);
307-
308+
308309
$this->httpStatusCode = curl_getinfo($feed, CURLINFO_HTTP_CODE);
309310

310311
if (($error = curl_error($feed)) !== '')
@@ -318,17 +319,17 @@ public function performRequest($return = true, $curlOptions = array())
318319

319320
return $json;
320321
}
321-
322+
322323
/**
323324
* Private method to generate the base string used by cURL
324-
*
325+
*
325326
* @param string $baseURI
326327
* @param string $method
327328
* @param array $params
328-
*
329+
*
329330
* @return string Built base string
330331
*/
331-
private function buildBaseString($baseURI, $method, $params)
332+
private function buildBaseString($baseURI, $method, $params)
332333
{
333334
$return = array();
334335
ksort($params);
@@ -337,30 +338,30 @@ private function buildBaseString($baseURI, $method, $params)
337338
{
338339
$return[] = rawurlencode($key) . '=' . rawurlencode($value);
339340
}
340-
341-
return $method . "&" . rawurlencode($baseURI) . '&' . rawurlencode(implode('&', $return));
341+
342+
return $method . "&" . rawurlencode($baseURI) . '&' . rawurlencode(implode('&', $return));
342343
}
343-
344+
344345
/**
345346
* Private method to generate authorization header used by cURL
346-
*
347+
*
347348
* @param array $oauth Array of oauth data generated by buildOauth()
348-
*
349+
*
349350
* @return string $return Header used by cURL for request
350-
*/
351+
*/
351352
private function buildAuthorizationHeader(array $oauth)
352353
{
353354
$return = 'Authorization: OAuth ';
354355
$values = array();
355-
356+
356357
foreach($oauth as $key => $value)
357358
{
358359
if (in_array($key, array('oauth_consumer_key', 'oauth_nonce', 'oauth_signature',
359360
'oauth_signature_method', 'oauth_timestamp', 'oauth_token', 'oauth_version'))) {
360361
$values[] = "$key=\"" . rawurlencode($value) . "\"";
361362
}
362363
}
363-
364+
364365
$return .= implode(', ', $values);
365366
return $return;
366367
}
@@ -390,13 +391,13 @@ public function request($url, $method = 'get', $data = null, $curlOptions = arra
390391

391392
return $this->buildOauth($url, $method)->performRequest(true, $curlOptions);
392393
}
393-
394+
394395
/**
395396
* Get the HTTP status code for the previous request
396-
*
397+
*
397398
* @return integer
398399
*/
399-
public function getHttpStatusCode()
400+
public function getHttpStatusCode()
400401
{
401402
return $this->httpStatusCode;
402403
}

0 commit comments

Comments
 (0)