1
+ <?php
2
+ /**
3
+ * Twitter-API-PHP : Simple PHP wrapper for the v1.1 API
4
+ *
5
+ * PHP version 5.3.10
6
+ *
7
+ * @category Awesomeness
8
+ * @package Twitter-API-PHP
9
+ * @author James Mallison <[email protected] >
10
+ * @license MIT License
11
+ * @version 1.0.4
12
+ * @link http://github.com/j7mbo/twitter-api-php
13
+ */
14
+ class TwitterAPIExchange
15
+ {
16
+ /**
17
+ * @var string
18
+ */
19
+ private $ oauth_access_token ;
20
+ /**
21
+ * @var string
22
+ */
23
+ private $ oauth_access_token_secret ;
24
+ /**
25
+ * @var string
26
+ */
27
+ private $ consumer_key ;
28
+ /**
29
+ * @var string
30
+ */
31
+ private $ consumer_secret ;
32
+ /**
33
+ * @var array
34
+ */
35
+ private $ postfields ;
36
+ /**
37
+ * @var string
38
+ */
39
+ private $ getfield ;
40
+ /**
41
+ * @var mixed
42
+ */
43
+ protected $ oauth ;
44
+ /**
45
+ * @var string
46
+ */
47
+ public $ url ;
48
+ /**
49
+ * @var string
50
+ */
51
+ public $ requestMethod ;
52
+ /**
53
+ * The HTTP status code from the previous request
54
+ *
55
+ * @var int
56
+ */
57
+ protected $ httpStatusCode ;
58
+ /**
59
+ * Create the API access object. Requires an array of settings::
60
+ * oauth access token, oauth access token secret, consumer key, consumer secret
61
+ * These are all available by creating your own application on dev.twitter.com
62
+ * Requires the cURL library
63
+ *
64
+ * @throws \RuntimeException When cURL isn't loaded
65
+ * @throws \InvalidArgumentException When incomplete settings parameters are provided
66
+ *
67
+ * @param array $settings
68
+ */
69
+ public function __construct (array $ settings )
70
+ {
71
+ if (!function_exists ('curl_init ' ))
72
+ {
73
+ throw new RuntimeException ('TwitterAPIExchange requires cURL extension to be loaded, see: http://curl.haxx.se/docs/install.html ' );
74
+ }
75
+ if (!isset ($ settings ['oauth_access_token ' ])
76
+ || !isset ($ settings ['oauth_access_token_secret ' ])
77
+ || !isset ($ settings ['consumer_key ' ])
78
+ || !isset ($ settings ['consumer_secret ' ]))
79
+ {
80
+ throw new InvalidArgumentException ('Incomplete settings passed to TwitterAPIExchange ' );
81
+ }
82
+ $ this ->oauth_access_token = $ settings ['oauth_access_token ' ];
83
+ $ this ->oauth_access_token_secret = $ settings ['oauth_access_token_secret ' ];
84
+ $ this ->consumer_key = $ settings ['consumer_key ' ];
85
+ $ this ->consumer_secret = $ settings ['consumer_secret ' ];
86
+ }
87
+ /**
88
+ * Set postfields array, example: array('screen_name' => 'J7mbo')
89
+ *
90
+ * @param array $array Array of parameters to send to API
91
+ *
92
+ * @throws \Exception When you are trying to set both get and post fields
93
+ *
94
+ * @return TwitterAPIExchange Instance of self for method chaining
95
+ */
96
+ public function setPostfields (array $ array )
97
+ {
98
+ if (!is_null ($ this ->getGetfield ()))
99
+ {
100
+ throw new Exception ('You can only choose get OR post fields (post fields include put). ' );
101
+ }
102
+ if (isset ($ array ['status ' ]) && substr ($ array ['status ' ], 0 , 1 ) === '@ ' )
103
+ {
104
+ $ array ['status ' ] = sprintf ("\0%s " , $ array ['status ' ]);
105
+ }
106
+ foreach ($ array as $ key => &$ value )
107
+ {
108
+ if (is_bool ($ value ))
109
+ {
110
+ $ value = ($ value === true ) ? 'true ' : 'false ' ;
111
+ }
112
+ }
113
+ $ this ->postfields = $ array ;
114
+ // rebuild oAuth
115
+ if (isset ($ this ->oauth ['oauth_signature ' ]))
116
+ {
117
+ $ this ->buildOauth ($ this ->url , $ this ->requestMethod );
118
+ }
119
+ return $ this ;
120
+ }
121
+ /**
122
+ * Set getfield string, example: '?screen_name=J7mbo'
123
+ *
124
+ * @param string $string Get key and value pairs as string
125
+ *
126
+ * @throws \Exception
127
+ *
128
+ * @return \TwitterAPIExchange Instance of self for method chaining
129
+ */
130
+
131
+ public function setGetfield ($ string )
132
+ {
133
+ if (!is_null ($ this ->getPostfields ()))
134
+ {
135
+ throw new Exception ('You can only choose get OR post / post fields. ' );
136
+ }
137
+
138
+ if (is_array ($ string ))
139
+ $ string = http_build_query ($ string );
140
+
141
+ $ getfields = preg_replace ('/^\?/ ' , '' , explode ('& ' , $ string ));
142
+ $ params = array ();
143
+ foreach ($ getfields as $ field )
144
+ {
145
+ if ($ field !== '' )
146
+ {
147
+ list ($ key , $ value ) = explode ('= ' , $ field );
148
+ $ params [$ key ] = $ value ;
149
+ }
150
+ }
151
+ $ this ->getfield = '? ' . http_build_query ($ params , '' , '& ' );
152
+ return $ this ;
153
+ }
154
+
155
+ public function setGetfields ($ string )
156
+ {
157
+ return $ this ->setGetfield ($ string );
158
+ }
159
+
160
+ /**
161
+ * Get getfield string (simple getter)
162
+ *
163
+ * @return string $this->getfields
164
+ */
165
+ public function getGetfield ()
166
+ {
167
+ return $ this ->getfield ;
168
+ }
169
+ /**
170
+ * Get postfields array (simple getter)
171
+ *
172
+ * @return array $this->postfields
173
+ */
174
+ public function getPostfields ()
175
+ {
176
+ return $ this ->postfields ;
177
+ }
178
+ /**
179
+ * Build the Oauth object using params set in construct and additionals
180
+ * passed to this method. For v1.1, see: https://dev.twitter.com/docs/api/1.1
181
+ *
182
+ * @param string $url The API url to use. Example: https://api.twitter.com/1.1/search/tweets.json
183
+ * @param string $requestMethod Either POST or GET
184
+ *
185
+ * @throws \Exception
186
+ *
187
+ * @return \TwitterAPIExchange Instance of self for method chaining
188
+ */
189
+ public function buildOauth ($ url , $ requestMethod )
190
+ {
191
+ if (!in_array (strtolower ($ requestMethod ), array ('post ' , 'get ' , 'put ' , 'delete ' )))
192
+ {
193
+ throw new Exception ('Request method must be either POST, GET or PUT or DELETE ' );
194
+ }
195
+ $ consumer_key = $ this ->consumer_key ;
196
+ $ consumer_secret = $ this ->consumer_secret ;
197
+ $ oauth_access_token = $ this ->oauth_access_token ;
198
+ $ oauth_access_token_secret = $ this ->oauth_access_token_secret ;
199
+ $ oauth = array (
200
+ 'oauth_consumer_key ' => $ consumer_key ,
201
+ 'oauth_nonce ' => time (),
202
+ 'oauth_signature_method ' => 'HMAC-SHA1 ' ,
203
+ 'oauth_token ' => $ oauth_access_token ,
204
+ 'oauth_timestamp ' => time (),
205
+ 'oauth_version ' => '1.0 '
206
+ );
207
+ $ getfield = $ this ->getGetfield ();
208
+ if (!is_null ($ getfield ))
209
+ {
210
+ $ getfields = str_replace ('? ' , '' , explode ('& ' , $ getfield ));
211
+ foreach ($ getfields as $ g )
212
+ {
213
+ $ split = explode ('= ' , $ g );
214
+ /** In case a null is passed through **/
215
+ if (isset ($ split [1 ]))
216
+ {
217
+ $ oauth [$ split [0 ]] = urldecode ($ split [1 ]);
218
+ }
219
+ }
220
+ }
221
+ $ postfields = $ this ->getPostfields ();
222
+ if (!is_null ($ postfields )) {
223
+ foreach ($ postfields as $ key => $ value ) {
224
+ $ oauth [$ key ] = $ value ;
225
+ }
226
+ }
227
+ $ base_info = $ this ->buildBaseString ($ url , $ requestMethod , $ oauth );
228
+ $ composite_key = rawurlencode ($ consumer_secret ) . '& ' . rawurlencode ($ oauth_access_token_secret );
229
+ $ oauth_signature = base64_encode (hash_hmac ('sha1 ' , $ base_info , $ composite_key , true ));
230
+ $ oauth ['oauth_signature ' ] = $ oauth_signature ;
231
+ $ this ->url = $ url ;
232
+ $ this ->requestMethod = $ requestMethod ;
233
+ $ this ->oauth = $ oauth ;
234
+ return $ this ;
235
+ }
236
+ /**
237
+ * Perform the actual data retrieval from the API
238
+ *
239
+ * @param boolean $return If true, returns data. This is left in for backward compatibility reasons
240
+ * @param array $curlOptions Additional Curl options for this request
241
+ *
242
+ * @throws \Exception
243
+ *
244
+ * @return string json If $return param is true, returns json data.
245
+ */
246
+ public function performRequest ($ return = true , $ curlOptions = array ())
247
+ {
248
+ if (!is_bool ($ return ))
249
+ {
250
+ throw new Exception ('performRequest parameter must be true or false ' );
251
+ }
252
+ $ header = array ($ this ->buildAuthorizationHeader ($ this ->oauth ), 'Expect: ' );
253
+ $ getfield = $ this ->getGetfield ();
254
+ $ postfields = $ this ->getPostfields ();
255
+ if (in_array (strtolower ($ this ->requestMethod ), array ('put ' , 'delete ' )))
256
+ {
257
+ $ curlOptions [CURLOPT_CUSTOMREQUEST ] = $ this ->requestMethod ;
258
+ }
259
+ $ options = $ curlOptions + array (
260
+ CURLOPT_HTTPHEADER => $ header ,
261
+ CURLOPT_HEADER => false ,
262
+ CURLOPT_URL => $ this ->url ,
263
+ CURLOPT_RETURNTRANSFER => true ,
264
+ CURLOPT_TIMEOUT => 10 ,
265
+ );
266
+ if (!is_null ($ postfields ))
267
+ {
268
+ $ options [CURLOPT_POSTFIELDS ] = http_build_query ($ postfields , '' , '& ' );
269
+ }
270
+ else
271
+ {
272
+ if ($ getfield !== '' )
273
+ {
274
+ $ options [CURLOPT_URL ] .= $ getfield ;
275
+ }
276
+ }
277
+ $ feed = curl_init ();
278
+ curl_setopt_array ($ feed , $ options );
279
+ $ json = curl_exec ($ feed );
280
+ $ this ->httpStatusCode = curl_getinfo ($ feed , CURLINFO_HTTP_CODE );
281
+ if (($ error = curl_error ($ feed )) !== '' )
282
+ {
283
+ curl_close ($ feed );
284
+ throw new \Exception ($ error );
285
+ }
286
+ curl_close ($ feed );
287
+ return $ json ;
288
+ }
289
+ /**
290
+ * Private method to generate the base string used by cURL
291
+ *
292
+ * @param string $baseURI
293
+ * @param string $method
294
+ * @param array $params
295
+ *
296
+ * @return string Built base string
297
+ */
298
+ private function buildBaseString ($ baseURI , $ method , $ params )
299
+ {
300
+ $ return = array ();
301
+ ksort ($ params );
302
+ foreach ($ params as $ key => $ value )
303
+ {
304
+ $ return [] = rawurlencode ($ key ) . '= ' . rawurlencode ($ value );
305
+ }
306
+ return $ method . "& " . rawurlencode ($ baseURI ) . '& ' . rawurlencode (implode ('& ' , $ return ));
307
+ }
308
+ /**
309
+ * Private method to generate authorization header used by cURL
310
+ *
311
+ * @param array $oauth Array of oauth data generated by buildOauth()
312
+ *
313
+ * @return string $return Header used by cURL for request
314
+ */
315
+ private function buildAuthorizationHeader (array $ oauth )
316
+ {
317
+ $ return = 'Authorization: OAuth ' ;
318
+ $ values = array ();
319
+ foreach ($ oauth as $ key => $ value )
320
+ {
321
+ if (in_array ($ key , array ('oauth_consumer_key ' , 'oauth_nonce ' , 'oauth_signature ' ,
322
+ 'oauth_signature_method ' , 'oauth_timestamp ' , 'oauth_token ' , 'oauth_version ' ))) {
323
+ $ values [] = "$ key= \"" . rawurlencode ($ value ) . "\"" ;
324
+ }
325
+ }
326
+ $ return .= implode (', ' , $ values );
327
+ return $ return ;
328
+ }
329
+ /**
330
+ * Helper method to perform our request
331
+ *
332
+ * @param string $url
333
+ * @param string $method
334
+ * @param string $data
335
+ * @param array $curlOptions
336
+ *
337
+ * @throws \Exception
338
+ *
339
+ * @return string The json response from the server
340
+ */
341
+ public function request ($ url , $ method = 'get ' , $ data = null , $ curlOptions = array ())
342
+ {
343
+ if (strtolower ($ method ) === 'get ' )
344
+ {
345
+ $ this ->setGetfield ($ data );
346
+ }
347
+ else
348
+ {
349
+ $ this ->setPostfields ($ data );
350
+ }
351
+ return $ this ->buildOauth ($ url , $ method )->performRequest (true , $ curlOptions );
352
+ }
353
+ /**
354
+ * Get the HTTP status code for the previous request
355
+ *
356
+ * @return integer
357
+ */
358
+ public function getHttpStatusCode ()
359
+ {
360
+ return $ this ->httpStatusCode ;
361
+ }
362
+ }
0 commit comments