2
2
3
3
/**
4
4
* Twitter-API-PHP : Simple PHP wrapper for the v1.1 API
5
- *
5
+ *
6
6
* PHP version 5.3.10
7
- *
7
+ *
8
8
* @category Awesomeness
9
9
* @package Twitter-API-PHP
10
10
* @author James Mallison <me@j7mbo.co.uk>
@@ -61,7 +61,7 @@ class TwitterAPIExchange
61
61
62
62
/**
63
63
* The HTTP status code from the previous request
64
- *
64
+ *
65
65
* @var int
66
66
*/
67
67
protected $ httpStatusCode ;
@@ -72,23 +72,24 @@ class TwitterAPIExchange
72
72
* These are all available by creating your own application on dev.twitter.com
73
73
* Requires the cURL library
74
74
*
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
76
77
*
77
78
* @param array $settings
78
79
*/
79
80
public function __construct (array $ settings )
80
81
{
81
- if (!in_array ( ' curl ' , get_loaded_extensions ()))
82
+ if (!function_exists ( ' curl_init ' ))
82
83
{
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 ' );
84
85
}
85
-
86
+
86
87
if (!isset ($ settings ['oauth_access_token ' ])
87
88
|| !isset ($ settings ['oauth_access_token_secret ' ])
88
89
|| !isset ($ settings ['consumer_key ' ])
89
90
|| !isset ($ settings ['consumer_secret ' ]))
90
91
{
91
- throw new Exception ( ' Make sure you are passing in the correct parameters ' );
92
+ throw new InvalidArgumentException ( ' Incomplete settings passed to TwitterAPIExchange ' );
92
93
}
93
94
94
95
$ this ->oauth_access_token = $ settings ['oauth_access_token ' ];
@@ -108,11 +109,11 @@ public function __construct(array $settings)
108
109
*/
109
110
public function setPostfields (array $ array )
110
111
{
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. ' );
114
115
}
115
-
116
+
116
117
if (isset ($ array ['status ' ]) && substr ($ array ['status ' ], 0 , 1 ) === '@ ' )
117
118
{
118
119
$ array ['status ' ] = sprintf ("\0%s " , $ array ['status ' ]);
@@ -125,33 +126,33 @@ public function setPostfields(array $array)
125
126
$ value = ($ value === true ) ? 'true ' : 'false ' ;
126
127
}
127
128
}
128
-
129
+
129
130
$ this ->postfields = $ array ;
130
-
131
+
131
132
// rebuild oAuth
132
133
if (isset ($ this ->oauth ['oauth_signature ' ])) {
133
134
$ this ->buildOauth ($ this ->url , $ this ->requestMethod );
134
135
}
135
136
136
137
return $ this ;
137
138
}
138
-
139
+
139
140
/**
140
141
* Set getfield string, example: '?screen_name=J7mbo'
141
- *
142
+ *
142
143
* @param string $string Get key and value pairs as string
143
144
*
144
145
* @throws \Exception
145
- *
146
+ *
146
147
* @return \TwitterAPIExchange Instance of self for method chaining
147
148
*/
148
149
public function setGetfield ($ string )
149
150
{
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. ' );
153
154
}
154
-
155
+
155
156
$ getfields = preg_replace ('/^\?/ ' , '' , explode ('& ' , $ string ));
156
157
$ params = array ();
157
158
@@ -165,30 +166,30 @@ public function setGetfield($string)
165
166
}
166
167
167
168
$ this ->getfield = '? ' . http_build_query ($ params );
168
-
169
+
169
170
return $ this ;
170
171
}
171
-
172
+
172
173
/**
173
174
* Get getfield string (simple getter)
174
- *
175
+ *
175
176
* @return string $this->getfields
176
177
*/
177
178
public function getGetfield ()
178
179
{
179
180
return $ this ->getfield ;
180
181
}
181
-
182
+
182
183
/**
183
184
* Get postfields array (simple getter)
184
- *
185
+ *
185
186
* @return array $this->postfields
186
187
*/
187
188
public function getPostfields ()
188
189
{
189
190
return $ this ->postfields ;
190
191
}
191
-
192
+
192
193
/**
193
194
* Build the Oauth object using params set in construct and additionals
194
195
* 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)
206
207
{
207
208
throw new Exception ('Request method must be either POST or GET ' );
208
209
}
209
-
210
+
210
211
$ consumer_key = $ this ->consumer_key ;
211
212
$ consumer_secret = $ this ->consumer_secret ;
212
213
$ oauth_access_token = $ this ->oauth_access_token ;
213
214
$ oauth_access_token_secret = $ this ->oauth_access_token_secret ;
214
-
215
+
215
216
$ oauth = array (
216
217
'oauth_consumer_key ' => $ consumer_key ,
217
218
'oauth_nonce ' => time (),
@@ -220,9 +221,9 @@ public function buildOauth($url, $requestMethod)
220
221
'oauth_timestamp ' => time (),
221
222
'oauth_version ' => '1.0 '
222
223
);
223
-
224
+
224
225
$ getfield = $ this ->getGetfield ();
225
-
226
+
226
227
if (!is_null ($ getfield ))
227
228
{
228
229
$ getfields = str_replace ('? ' , '' , explode ('& ' , $ getfield ));
@@ -238,7 +239,7 @@ public function buildOauth($url, $requestMethod)
238
239
}
239
240
}
240
241
}
241
-
242
+
242
243
$ postfields = $ this ->getPostfields ();
243
244
244
245
if (!is_null ($ postfields )) {
@@ -251,22 +252,22 @@ public function buildOauth($url, $requestMethod)
251
252
$ composite_key = rawurlencode ($ consumer_secret ) . '& ' . rawurlencode ($ oauth_access_token_secret );
252
253
$ oauth_signature = base64_encode (hash_hmac ('sha1 ' , $ base_info , $ composite_key , true ));
253
254
$ oauth ['oauth_signature ' ] = $ oauth_signature ;
254
-
255
+
255
256
$ this ->url = $ url ;
256
257
$ this ->requestMethod = $ requestMethod ;
257
258
$ this ->oauth = $ oauth ;
258
-
259
+
259
260
return $ this ;
260
261
}
261
-
262
+
262
263
/**
263
264
* Perform the actual data retrieval from the API
264
- *
265
+ *
265
266
* @param boolean $return If true, returns data. This is left in for backward compatibility reasons
266
267
* @param array $curlOptions Additional Curl options for this request
267
268
*
268
269
* @throws \Exception
269
- *
270
+ *
270
271
* @return string json If $return param is true, returns json data.
271
272
*/
272
273
public function performRequest ($ return = true , $ curlOptions = array ())
@@ -304,7 +305,7 @@ public function performRequest($return = true, $curlOptions = array())
304
305
$ feed = curl_init ();
305
306
curl_setopt_array ($ feed , $ options );
306
307
$ json = curl_exec ($ feed );
307
-
308
+
308
309
$ this ->httpStatusCode = curl_getinfo ($ feed , CURLINFO_HTTP_CODE );
309
310
310
311
if (($ error = curl_error ($ feed )) !== '' )
@@ -318,17 +319,17 @@ public function performRequest($return = true, $curlOptions = array())
318
319
319
320
return $ json ;
320
321
}
321
-
322
+
322
323
/**
323
324
* Private method to generate the base string used by cURL
324
- *
325
+ *
325
326
* @param string $baseURI
326
327
* @param string $method
327
328
* @param array $params
328
- *
329
+ *
329
330
* @return string Built base string
330
331
*/
331
- private function buildBaseString ($ baseURI , $ method , $ params )
332
+ private function buildBaseString ($ baseURI , $ method , $ params )
332
333
{
333
334
$ return = array ();
334
335
ksort ($ params );
@@ -337,30 +338,30 @@ private function buildBaseString($baseURI, $method, $params)
337
338
{
338
339
$ return [] = rawurlencode ($ key ) . '= ' . rawurlencode ($ value );
339
340
}
340
-
341
- return $ method . "& " . rawurlencode ($ baseURI ) . '& ' . rawurlencode (implode ('& ' , $ return ));
341
+
342
+ return $ method . "& " . rawurlencode ($ baseURI ) . '& ' . rawurlencode (implode ('& ' , $ return ));
342
343
}
343
-
344
+
344
345
/**
345
346
* Private method to generate authorization header used by cURL
346
- *
347
+ *
347
348
* @param array $oauth Array of oauth data generated by buildOauth()
348
- *
349
+ *
349
350
* @return string $return Header used by cURL for request
350
- */
351
+ */
351
352
private function buildAuthorizationHeader (array $ oauth )
352
353
{
353
354
$ return = 'Authorization: OAuth ' ;
354
355
$ values = array ();
355
-
356
+
356
357
foreach ($ oauth as $ key => $ value )
357
358
{
358
359
if (in_array ($ key , array ('oauth_consumer_key ' , 'oauth_nonce ' , 'oauth_signature ' ,
359
360
'oauth_signature_method ' , 'oauth_timestamp ' , 'oauth_token ' , 'oauth_version ' ))) {
360
361
$ values [] = "$ key= \"" . rawurlencode ($ value ) . "\"" ;
361
362
}
362
363
}
363
-
364
+
364
365
$ return .= implode (', ' , $ values );
365
366
return $ return ;
366
367
}
@@ -390,13 +391,13 @@ public function request($url, $method = 'get', $data = null, $curlOptions = arra
390
391
391
392
return $ this ->buildOauth ($ url , $ method )->performRequest (true , $ curlOptions );
392
393
}
393
-
394
+
394
395
/**
395
396
* Get the HTTP status code for the previous request
396
- *
397
+ *
397
398
* @return integer
398
399
*/
399
- public function getHttpStatusCode ()
400
+ public function getHttpStatusCode ()
400
401
{
401
402
return $ this ->httpStatusCode ;
402
403
}
0 commit comments