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

Commit 5a1ac48

Browse files
authored
Merge pull request #184 from J7mbo/develop
Develop -> Master
2 parents a89ce3e + 2673208 commit 5a1ac48

File tree

1 file changed

+68
-48
lines changed

1 file changed

+68
-48
lines changed

TwitterAPIExchange.php

Lines changed: 68 additions & 48 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>
@@ -59,29 +59,37 @@ class TwitterAPIExchange
5959
*/
6060
public $requestMethod;
6161

62+
/**
63+
* The HTTP status code from the previous request
64+
*
65+
* @var int
66+
*/
67+
protected $httpStatusCode;
68+
6269
/**
6370
* Create the API access object. Requires an array of settings::
6471
* oauth access token, oauth access token secret, consumer key, consumer secret
6572
* These are all available by creating your own application on dev.twitter.com
6673
* Requires the cURL library
6774
*
68-
* @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
6977
*
7078
* @param array $settings
7179
*/
7280
public function __construct(array $settings)
7381
{
74-
if (!in_array('curl', get_loaded_extensions()))
82+
if (!function_exists('curl_init'))
7583
{
76-
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');
7785
}
78-
86+
7987
if (!isset($settings['oauth_access_token'])
8088
|| !isset($settings['oauth_access_token_secret'])
8189
|| !isset($settings['consumer_key'])
8290
|| !isset($settings['consumer_secret']))
8391
{
84-
throw new Exception('Make sure you are passing in the correct parameters');
92+
throw new InvalidArgumentException('Incomplete settings passed to TwitterAPIExchange');
8593
}
8694

8795
$this->oauth_access_token = $settings['oauth_access_token'];
@@ -101,11 +109,11 @@ public function __construct(array $settings)
101109
*/
102110
public function setPostfields(array $array)
103111
{
104-
if (!is_null($this->getGetfield()))
105-
{
106-
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.');
107115
}
108-
116+
109117
if (isset($array['status']) && substr($array['status'], 0, 1) === '@')
110118
{
111119
$array['status'] = sprintf("\0%s", $array['status']);
@@ -118,33 +126,33 @@ public function setPostfields(array $array)
118126
$value = ($value === true) ? 'true' : 'false';
119127
}
120128
}
121-
129+
122130
$this->postfields = $array;
123-
131+
124132
// rebuild oAuth
125133
if (isset($this->oauth['oauth_signature'])) {
126134
$this->buildOauth($this->url, $this->requestMethod);
127135
}
128136

129137
return $this;
130138
}
131-
139+
132140
/**
133141
* Set getfield string, example: '?screen_name=J7mbo'
134-
*
142+
*
135143
* @param string $string Get key and value pairs as string
136144
*
137145
* @throws \Exception
138-
*
146+
*
139147
* @return \TwitterAPIExchange Instance of self for method chaining
140148
*/
141149
public function setGetfield($string)
142150
{
143-
if (!is_null($this->getPostfields()))
144-
{
145-
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.');
146154
}
147-
155+
148156
$getfields = preg_replace('/^\?/', '', explode('&', $string));
149157
$params = array();
150158

@@ -158,30 +166,30 @@ public function setGetfield($string)
158166
}
159167

160168
$this->getfield = '?' . http_build_query($params);
161-
169+
162170
return $this;
163171
}
164-
172+
165173
/**
166174
* Get getfield string (simple getter)
167-
*
175+
*
168176
* @return string $this->getfields
169177
*/
170178
public function getGetfield()
171179
{
172180
return $this->getfield;
173181
}
174-
182+
175183
/**
176184
* Get postfields array (simple getter)
177-
*
185+
*
178186
* @return array $this->postfields
179187
*/
180188
public function getPostfields()
181189
{
182190
return $this->postfields;
183191
}
184-
192+
185193
/**
186194
* Build the Oauth object using params set in construct and additionals
187195
* passed to this method. For v1.1, see: https://dev.twitter.com/docs/api/1.1
@@ -199,12 +207,12 @@ public function buildOauth($url, $requestMethod)
199207
{
200208
throw new Exception('Request method must be either POST or GET');
201209
}
202-
210+
203211
$consumer_key = $this->consumer_key;
204212
$consumer_secret = $this->consumer_secret;
205213
$oauth_access_token = $this->oauth_access_token;
206214
$oauth_access_token_secret = $this->oauth_access_token_secret;
207-
215+
208216
$oauth = array(
209217
'oauth_consumer_key' => $consumer_key,
210218
'oauth_nonce' => time(),
@@ -213,9 +221,9 @@ public function buildOauth($url, $requestMethod)
213221
'oauth_timestamp' => time(),
214222
'oauth_version' => '1.0'
215223
);
216-
224+
217225
$getfield = $this->getGetfield();
218-
226+
219227
if (!is_null($getfield))
220228
{
221229
$getfields = str_replace('?', '', explode('&', $getfield));
@@ -231,7 +239,7 @@ public function buildOauth($url, $requestMethod)
231239
}
232240
}
233241
}
234-
242+
235243
$postfields = $this->getPostfields();
236244

237245
if (!is_null($postfields)) {
@@ -244,22 +252,22 @@ public function buildOauth($url, $requestMethod)
244252
$composite_key = rawurlencode($consumer_secret) . '&' . rawurlencode($oauth_access_token_secret);
245253
$oauth_signature = base64_encode(hash_hmac('sha1', $base_info, $composite_key, true));
246254
$oauth['oauth_signature'] = $oauth_signature;
247-
255+
248256
$this->url = $url;
249257
$this->requestMethod = $requestMethod;
250258
$this->oauth = $oauth;
251-
259+
252260
return $this;
253261
}
254-
262+
255263
/**
256264
* Perform the actual data retrieval from the API
257-
*
265+
*
258266
* @param boolean $return If true, returns data. This is left in for backward compatibility reasons
259267
* @param array $curlOptions Additional Curl options for this request
260268
*
261269
* @throws \Exception
262-
*
270+
*
263271
* @return string json If $return param is true, returns json data.
264272
*/
265273
public function performRequest($return = true, $curlOptions = array())
@@ -298,6 +306,8 @@ public function performRequest($return = true, $curlOptions = array())
298306
curl_setopt_array($feed, $options);
299307
$json = curl_exec($feed);
300308

309+
$this->httpStatusCode = curl_getinfo($feed, CURLINFO_HTTP_CODE);
310+
301311
if (($error = curl_error($feed)) !== '')
302312
{
303313
curl_close($feed);
@@ -309,17 +319,17 @@ public function performRequest($return = true, $curlOptions = array())
309319

310320
return $json;
311321
}
312-
322+
313323
/**
314324
* Private method to generate the base string used by cURL
315-
*
325+
*
316326
* @param string $baseURI
317327
* @param string $method
318328
* @param array $params
319-
*
329+
*
320330
* @return string Built base string
321331
*/
322-
private function buildBaseString($baseURI, $method, $params)
332+
private function buildBaseString($baseURI, $method, $params)
323333
{
324334
$return = array();
325335
ksort($params);
@@ -328,30 +338,30 @@ private function buildBaseString($baseURI, $method, $params)
328338
{
329339
$return[] = rawurlencode($key) . '=' . rawurlencode($value);
330340
}
331-
332-
return $method . "&" . rawurlencode($baseURI) . '&' . rawurlencode(implode('&', $return));
341+
342+
return $method . "&" . rawurlencode($baseURI) . '&' . rawurlencode(implode('&', $return));
333343
}
334-
344+
335345
/**
336346
* Private method to generate authorization header used by cURL
337-
*
347+
*
338348
* @param array $oauth Array of oauth data generated by buildOauth()
339-
*
349+
*
340350
* @return string $return Header used by cURL for request
341-
*/
351+
*/
342352
private function buildAuthorizationHeader(array $oauth)
343353
{
344354
$return = 'Authorization: OAuth ';
345355
$values = array();
346-
356+
347357
foreach($oauth as $key => $value)
348358
{
349359
if (in_array($key, array('oauth_consumer_key', 'oauth_nonce', 'oauth_signature',
350360
'oauth_signature_method', 'oauth_timestamp', 'oauth_token', 'oauth_version'))) {
351361
$values[] = "$key=\"" . rawurlencode($value) . "\"";
352362
}
353363
}
354-
364+
355365
$return .= implode(', ', $values);
356366
return $return;
357367
}
@@ -381,4 +391,14 @@ public function request($url, $method = 'get', $data = null, $curlOptions = arra
381391

382392
return $this->buildOauth($url, $method)->performRequest(true, $curlOptions);
383393
}
394+
395+
/**
396+
* Get the HTTP status code for the previous request
397+
*
398+
* @return integer
399+
*/
400+
public function getHttpStatusCode()
401+
{
402+
return $this->httpStatusCode;
403+
}
384404
}

0 commit comments

Comments
 (0)