13
13
*/
14
14
class TwitterAPIExchange
15
15
{
16
+ /**
17
+ * @var string
18
+ */
16
19
private $ oauth_access_token ;
20
+
21
+ /**
22
+ * @var string
23
+ */
17
24
private $ oauth_access_token_secret ;
25
+
26
+ /**
27
+ * @var string
28
+ */
18
29
private $ consumer_key ;
30
+
31
+ /**
32
+ * @var string
33
+ */
19
34
private $ consumer_secret ;
35
+
36
+ /**
37
+ * @var array
38
+ */
20
39
private $ postfields ;
40
+
41
+ /**
42
+ * @var string
43
+ */
21
44
private $ getfield ;
45
+
46
+ /**
47
+ * @var mixed
48
+ */
22
49
protected $ oauth ;
50
+
51
+ /**
52
+ * @var string
53
+ */
23
54
public $ url ;
24
55
56
+ /**
57
+ * @var string
58
+ */
59
+ public $ requestMethod ;
60
+
25
61
/**
26
62
* Create the API access object. Requires an array of settings::
27
63
* oauth access token, oauth access token secret, consumer key, consumer secret
28
64
* These are all available by creating your own application on dev.twitter.com
29
65
* Requires the cURL library
30
- *
66
+ *
67
+ * @throws \Exception When cURL isn't installed or incorrect settings parameters are provided
68
+ *
31
69
* @param array $settings
32
70
*/
33
71
public function __construct (array $ settings )
@@ -50,12 +88,14 @@ public function __construct(array $settings)
50
88
$ this ->consumer_key = $ settings ['consumer_key ' ];
51
89
$ this ->consumer_secret = $ settings ['consumer_secret ' ];
52
90
}
53
-
91
+
54
92
/**
55
93
* Set postfields array, example: array('screen_name' => 'J7mbo')
56
- *
94
+ *
57
95
* @param array $array Array of parameters to send to API
58
- *
96
+ *
97
+ * @throws \Exception When you are trying to set both get and post fields
98
+ *
59
99
* @return TwitterAPIExchange Instance of self for method chaining
60
100
*/
61
101
public function setPostfields (array $ array )
@@ -72,13 +112,20 @@ public function setPostfields(array $array)
72
112
73
113
$ this ->postfields = $ array ;
74
114
115
+ // rebuild oAuth
116
+ if (isset ($ this ->oauth ['oauth_signature ' ])) {
117
+ $ this ->buildOauth ($ this ->url , $ this ->requestMethod );
118
+ }
119
+
75
120
return $ this ;
76
121
}
77
122
78
123
/**
79
124
* Set getfield string, example: '?screen_name=J7mbo'
80
125
*
81
126
* @param string $string Get key and value pairs as string
127
+ *
128
+ * @throws \Exception
82
129
*
83
130
* @return \TwitterAPIExchange Instance of self for method chaining
84
131
*/
@@ -121,9 +168,12 @@ public function getPostfields()
121
168
/**
122
169
* Build the Oauth object using params set in construct and additionals
123
170
* passed to this method. For v1.1, see: https://dev.twitter.com/docs/api/1.1
124
- *
171
+ *
125
172
* @param string $url The API url to use. Example: https://api.twitter.com/1.1/search/tweets.json
126
173
* @param string $requestMethod Either POST or GET
174
+ *
175
+ * @throws \Exception
176
+ *
127
177
* @return \TwitterAPIExchange Instance of self for method chaining
128
178
*/
129
179
public function buildOauth ($ url , $ requestMethod )
@@ -133,12 +183,12 @@ public function buildOauth($url, $requestMethod)
133
183
throw new Exception ('Request method must be either POST or GET ' );
134
184
}
135
185
136
- $ consumer_key = $ this ->consumer_key ;
137
- $ consumer_secret = $ this ->consumer_secret ;
138
- $ oauth_access_token = $ this ->oauth_access_token ;
186
+ $ consumer_key = $ this ->consumer_key ;
187
+ $ consumer_secret = $ this ->consumer_secret ;
188
+ $ oauth_access_token = $ this ->oauth_access_token ;
139
189
$ oauth_access_token_secret = $ this ->oauth_access_token_secret ;
140
190
141
- $ oauth = array (
191
+ $ oauth = array (
142
192
'oauth_consumer_key ' => $ consumer_key ,
143
193
'oauth_nonce ' => time (),
144
194
'oauth_signature_method ' => 'HMAC-SHA1 ' ,
@@ -152,19 +202,34 @@ public function buildOauth($url, $requestMethod)
152
202
if (!is_null ($ getfield ))
153
203
{
154
204
$ getfields = str_replace ('? ' , '' , explode ('& ' , $ getfield ));
205
+
155
206
foreach ($ getfields as $ g )
156
207
{
157
208
$ split = explode ('= ' , $ g );
158
- $ oauth [$ split [0 ]] = $ split [1 ];
209
+
210
+ /** In case a null is passed through **/
211
+ if (isset ($ split [1 ]))
212
+ {
213
+ $ oauth [$ split [0 ]] = $ split [1 ];
214
+ }
159
215
}
160
216
}
161
217
218
+ $ postfields = $ this ->getPostfields ();
219
+
220
+ if (!is_null ($ postfields )) {
221
+ foreach ($ postfields as $ key => $ value ) {
222
+ $ oauth [$ key ] = $ value ;
223
+ }
224
+ }
225
+
162
226
$ base_info = $ this ->buildBaseString ($ url , $ requestMethod , $ oauth );
163
227
$ composite_key = rawurlencode ($ consumer_secret ) . '& ' . rawurlencode ($ oauth_access_token_secret );
164
228
$ oauth_signature = base64_encode (hash_hmac ('sha1 ' , $ base_info , $ composite_key , true ));
165
229
$ oauth ['oauth_signature ' ] = $ oauth_signature ;
166
230
167
231
$ this ->url = $ url ;
232
+ $ this ->requestMethod = $ requestMethod ;
168
233
$ this ->oauth = $ oauth ;
169
234
170
235
return $ this ;
@@ -173,7 +238,9 @@ public function buildOauth($url, $requestMethod)
173
238
/**
174
239
* Perform the actual data retrieval from the API
175
240
*
176
- * @param boolean $return If true, returns data.
241
+ * @param boolean $return If true, returns data. This is left in for backward compatibility reasons
242
+ *
243
+ * @throws \Exception
177
244
*
178
245
* @return string json If $return param is true, returns json data.
179
246
*/
@@ -183,13 +250,13 @@ public function performRequest($return = true)
183
250
{
184
251
throw new Exception ('performRequest parameter must be true or false ' );
185
252
}
186
-
187
- $ header = array ($ this ->buildAuthorizationHeader ($ this ->oauth ), 'Expect: ' );
253
+
254
+ $ header = array ($ this ->buildAuthorizationHeader ($ this ->oauth ), 'Expect: ' );
188
255
189
256
$ getfield = $ this ->getGetfield ();
190
257
$ postfields = $ this ->getPostfields ();
191
258
192
- $ options = array (
259
+ $ options = array (
193
260
CURLOPT_HTTPHEADER => $ header ,
194
261
CURLOPT_HEADER => false ,
195
262
CURLOPT_URL => $ this ->url ,
@@ -199,7 +266,7 @@ public function performRequest($return = true)
199
266
200
267
if (!is_null ($ postfields ))
201
268
{
202
- $ options [CURLOPT_POSTFIELDS ] = $ postfields ;
269
+ $ options [CURLOPT_POSTFIELDS ] = http_build_query ( $ postfields) ;
203
270
}
204
271
else
205
272
{
@@ -214,15 +281,15 @@ public function performRequest($return = true)
214
281
$ json = curl_exec ($ feed );
215
282
curl_close ($ feed );
216
283
217
- if ( $ return) { return $ json ; }
284
+ return $ json ;
218
285
}
219
286
220
287
/**
221
288
* Private method to generate the base string used by cURL
222
289
*
223
290
* @param string $baseURI
224
291
* @param string $method
225
- * @param array $params
292
+ * @param array $params
226
293
*
227
294
* @return string Built base string
228
295
*/
@@ -231,9 +298,9 @@ private function buildBaseString($baseURI, $method, $params)
231
298
$ return = array ();
232
299
ksort ($ params );
233
300
234
- foreach ($ params as $ key=> $ value )
301
+ foreach ($ params as $ key => $ value )
235
302
{
236
- $ return [] = " $ key= " . $ value ;
303
+ $ return [] = rawurlencode ( $ key) . ' = ' . rawurlencode ( $ value) ;
237
304
}
238
305
239
306
return $ method . "& " . rawurlencode ($ baseURI ) . '& ' . rawurlencode (implode ('& ' , $ return ));
@@ -246,18 +313,45 @@ private function buildBaseString($baseURI, $method, $params)
246
313
*
247
314
* @return string $return Header used by cURL for request
248
315
*/
249
- private function buildAuthorizationHeader ($ oauth )
316
+ private function buildAuthorizationHeader (array $ oauth )
250
317
{
251
318
$ return = 'Authorization: OAuth ' ;
252
319
$ values = array ();
253
320
254
321
foreach ($ oauth as $ key => $ value )
255
322
{
256
- $ values [] = "$ key= \"" . rawurlencode ($ value ) . "\"" ;
323
+ if (in_array ($ key , array ('oauth_consumer_key ' , 'oauth_nonce ' , 'oauth_signature ' ,
324
+ 'oauth_signature_method ' , 'oauth_timestamp ' , 'oauth_token ' , 'oauth_version ' ))) {
325
+ $ values [] = "$ key= \"" . rawurlencode ($ value ) . "\"" ;
326
+ }
257
327
}
258
328
259
329
$ return .= implode (', ' , $ values );
260
330
return $ return ;
261
331
}
262
332
333
+ /**
334
+ * Helper method to perform our request
335
+ *
336
+ * @param string $url
337
+ * @param string $method
338
+ * @param string $data
339
+ *
340
+ * @throws \Exception
341
+ *
342
+ * @return string The json response from the server
343
+ */
344
+ public function request ($ url , $ method = 'get ' , $ data = null )
345
+ {
346
+ if (strtolower ($ method ) === 'get ' )
347
+ {
348
+ $ this ->setGetfield ($ data );
349
+ }
350
+ else
351
+ {
352
+ $ this ->setPostfields ($ data );
353
+ }
354
+
355
+ return $ this ->buildOauth ($ url , $ method )->performRequest ();
356
+ }
263
357
}
0 commit comments