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>
@@ -59,29 +59,37 @@ class TwitterAPIExchange
59
59
*/
60
60
public $ requestMethod ;
61
61
62
+ /**
63
+ * The HTTP status code from the previous request
64
+ *
65
+ * @var int
66
+ */
67
+ protected $ httpStatusCode ;
68
+
62
69
/**
63
70
* Create the API access object. Requires an array of settings::
64
71
* oauth access token, oauth access token secret, consumer key, consumer secret
65
72
* These are all available by creating your own application on dev.twitter.com
66
73
* Requires the cURL library
67
74
*
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
69
77
*
70
78
* @param array $settings
71
79
*/
72
80
public function __construct (array $ settings )
73
81
{
74
- if (!in_array ( ' curl ' , get_loaded_extensions ()))
82
+ if (!function_exists ( ' curl_init ' ))
75
83
{
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 ' );
77
85
}
78
-
86
+
79
87
if (!isset ($ settings ['oauth_access_token ' ])
80
88
|| !isset ($ settings ['oauth_access_token_secret ' ])
81
89
|| !isset ($ settings ['consumer_key ' ])
82
90
|| !isset ($ settings ['consumer_secret ' ]))
83
91
{
84
- throw new Exception ( ' Make sure you are passing in the correct parameters ' );
92
+ throw new InvalidArgumentException ( ' Incomplete settings passed to TwitterAPIExchange ' );
85
93
}
86
94
87
95
$ this ->oauth_access_token = $ settings ['oauth_access_token ' ];
@@ -101,11 +109,11 @@ public function __construct(array $settings)
101
109
*/
102
110
public function setPostfields (array $ array )
103
111
{
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. ' );
107
115
}
108
-
116
+
109
117
if (isset ($ array ['status ' ]) && substr ($ array ['status ' ], 0 , 1 ) === '@ ' )
110
118
{
111
119
$ array ['status ' ] = sprintf ("\0%s " , $ array ['status ' ]);
@@ -118,33 +126,33 @@ public function setPostfields(array $array)
118
126
$ value = ($ value === true ) ? 'true ' : 'false ' ;
119
127
}
120
128
}
121
-
129
+
122
130
$ this ->postfields = $ array ;
123
-
131
+
124
132
// rebuild oAuth
125
133
if (isset ($ this ->oauth ['oauth_signature ' ])) {
126
134
$ this ->buildOauth ($ this ->url , $ this ->requestMethod );
127
135
}
128
136
129
137
return $ this ;
130
138
}
131
-
139
+
132
140
/**
133
141
* Set getfield string, example: '?screen_name=J7mbo'
134
- *
142
+ *
135
143
* @param string $string Get key and value pairs as string
136
144
*
137
145
* @throws \Exception
138
- *
146
+ *
139
147
* @return \TwitterAPIExchange Instance of self for method chaining
140
148
*/
141
149
public function setGetfield ($ string )
142
150
{
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. ' );
146
154
}
147
-
155
+
148
156
$ getfields = preg_replace ('/^\?/ ' , '' , explode ('& ' , $ string ));
149
157
$ params = array ();
150
158
@@ -158,30 +166,30 @@ public function setGetfield($string)
158
166
}
159
167
160
168
$ this ->getfield = '? ' . http_build_query ($ params );
161
-
169
+
162
170
return $ this ;
163
171
}
164
-
172
+
165
173
/**
166
174
* Get getfield string (simple getter)
167
- *
175
+ *
168
176
* @return string $this->getfields
169
177
*/
170
178
public function getGetfield ()
171
179
{
172
180
return $ this ->getfield ;
173
181
}
174
-
182
+
175
183
/**
176
184
* Get postfields array (simple getter)
177
- *
185
+ *
178
186
* @return array $this->postfields
179
187
*/
180
188
public function getPostfields ()
181
189
{
182
190
return $ this ->postfields ;
183
191
}
184
-
192
+
185
193
/**
186
194
* Build the Oauth object using params set in construct and additionals
187
195
* 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)
199
207
{
200
208
throw new Exception ('Request method must be either POST or GET ' );
201
209
}
202
-
210
+
203
211
$ consumer_key = $ this ->consumer_key ;
204
212
$ consumer_secret = $ this ->consumer_secret ;
205
213
$ oauth_access_token = $ this ->oauth_access_token ;
206
214
$ oauth_access_token_secret = $ this ->oauth_access_token_secret ;
207
-
215
+
208
216
$ oauth = array (
209
217
'oauth_consumer_key ' => $ consumer_key ,
210
218
'oauth_nonce ' => time (),
@@ -213,9 +221,9 @@ public function buildOauth($url, $requestMethod)
213
221
'oauth_timestamp ' => time (),
214
222
'oauth_version ' => '1.0 '
215
223
);
216
-
224
+
217
225
$ getfield = $ this ->getGetfield ();
218
-
226
+
219
227
if (!is_null ($ getfield ))
220
228
{
221
229
$ getfields = str_replace ('? ' , '' , explode ('& ' , $ getfield ));
@@ -231,7 +239,7 @@ public function buildOauth($url, $requestMethod)
231
239
}
232
240
}
233
241
}
234
-
242
+
235
243
$ postfields = $ this ->getPostfields ();
236
244
237
245
if (!is_null ($ postfields )) {
@@ -244,22 +252,22 @@ public function buildOauth($url, $requestMethod)
244
252
$ composite_key = rawurlencode ($ consumer_secret ) . '& ' . rawurlencode ($ oauth_access_token_secret );
245
253
$ oauth_signature = base64_encode (hash_hmac ('sha1 ' , $ base_info , $ composite_key , true ));
246
254
$ oauth ['oauth_signature ' ] = $ oauth_signature ;
247
-
255
+
248
256
$ this ->url = $ url ;
249
257
$ this ->requestMethod = $ requestMethod ;
250
258
$ this ->oauth = $ oauth ;
251
-
259
+
252
260
return $ this ;
253
261
}
254
-
262
+
255
263
/**
256
264
* Perform the actual data retrieval from the API
257
- *
265
+ *
258
266
* @param boolean $return If true, returns data. This is left in for backward compatibility reasons
259
267
* @param array $curlOptions Additional Curl options for this request
260
268
*
261
269
* @throws \Exception
262
- *
270
+ *
263
271
* @return string json If $return param is true, returns json data.
264
272
*/
265
273
public function performRequest ($ return = true , $ curlOptions = array ())
@@ -298,6 +306,8 @@ public function performRequest($return = true, $curlOptions = array())
298
306
curl_setopt_array ($ feed , $ options );
299
307
$ json = curl_exec ($ feed );
300
308
309
+ $ this ->httpStatusCode = curl_getinfo ($ feed , CURLINFO_HTTP_CODE );
310
+
301
311
if (($ error = curl_error ($ feed )) !== '' )
302
312
{
303
313
curl_close ($ feed );
@@ -309,17 +319,17 @@ public function performRequest($return = true, $curlOptions = array())
309
319
310
320
return $ json ;
311
321
}
312
-
322
+
313
323
/**
314
324
* Private method to generate the base string used by cURL
315
- *
325
+ *
316
326
* @param string $baseURI
317
327
* @param string $method
318
328
* @param array $params
319
- *
329
+ *
320
330
* @return string Built base string
321
331
*/
322
- private function buildBaseString ($ baseURI , $ method , $ params )
332
+ private function buildBaseString ($ baseURI , $ method , $ params )
323
333
{
324
334
$ return = array ();
325
335
ksort ($ params );
@@ -328,30 +338,30 @@ private function buildBaseString($baseURI, $method, $params)
328
338
{
329
339
$ return [] = rawurlencode ($ key ) . '= ' . rawurlencode ($ value );
330
340
}
331
-
332
- return $ method . "& " . rawurlencode ($ baseURI ) . '& ' . rawurlencode (implode ('& ' , $ return ));
341
+
342
+ return $ method . "& " . rawurlencode ($ baseURI ) . '& ' . rawurlencode (implode ('& ' , $ return ));
333
343
}
334
-
344
+
335
345
/**
336
346
* Private method to generate authorization header used by cURL
337
- *
347
+ *
338
348
* @param array $oauth Array of oauth data generated by buildOauth()
339
- *
349
+ *
340
350
* @return string $return Header used by cURL for request
341
- */
351
+ */
342
352
private function buildAuthorizationHeader (array $ oauth )
343
353
{
344
354
$ return = 'Authorization: OAuth ' ;
345
355
$ values = array ();
346
-
356
+
347
357
foreach ($ oauth as $ key => $ value )
348
358
{
349
359
if (in_array ($ key , array ('oauth_consumer_key ' , 'oauth_nonce ' , 'oauth_signature ' ,
350
360
'oauth_signature_method ' , 'oauth_timestamp ' , 'oauth_token ' , 'oauth_version ' ))) {
351
361
$ values [] = "$ key= \"" . rawurlencode ($ value ) . "\"" ;
352
362
}
353
363
}
354
-
364
+
355
365
$ return .= implode (', ' , $ values );
356
366
return $ return ;
357
367
}
@@ -381,4 +391,14 @@ public function request($url, $method = 'get', $data = null, $curlOptions = arra
381
391
382
392
return $ this ->buildOauth ($ url , $ method )->performRequest (true , $ curlOptions );
383
393
}
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
+ }
384
404
}
0 commit comments