1313 */
1414class TwitterAPIExchange
1515{
16+ /**
17+ * @var string
18+ */
1619 private $ oauth_access_token ;
20+
21+ /**
22+ * @var string
23+ */
1724 private $ oauth_access_token_secret ;
25+
26+ /**
27+ * @var string
28+ */
1829 private $ consumer_key ;
30+
31+ /**
32+ * @var string
33+ */
1934 private $ consumer_secret ;
35+
36+ /**
37+ * @var array
38+ */
2039 private $ postfields ;
40+
41+ /**
42+ * @var string
43+ */
2144 private $ getfield ;
45+
46+ /**
47+ * @var mixed
48+ */
2249 protected $ oauth ;
50+
51+ /**
52+ * @var string
53+ */
2354 public $ url ;
2455
56+ /**
57+ * @var string
58+ */
59+ public $ requestMethod ;
60+
2561 /**
2662 * Create the API access object. Requires an array of settings::
2763 * oauth access token, oauth access token secret, consumer key, consumer secret
2864 * These are all available by creating your own application on dev.twitter.com
2965 * Requires the cURL library
30- *
66+ *
67+ * @throws \Exception When cURL isn't installed or incorrect settings parameters are provided
68+ *
3169 * @param array $settings
3270 */
3371 public function __construct (array $ settings )
@@ -50,12 +88,14 @@ public function __construct(array $settings)
5088 $ this ->consumer_key = $ settings ['consumer_key ' ];
5189 $ this ->consumer_secret = $ settings ['consumer_secret ' ];
5290 }
53-
91+
5492 /**
5593 * Set postfields array, example: array('screen_name' => 'J7mbo')
56- *
94+ *
5795 * @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+ *
5999 * @return TwitterAPIExchange Instance of self for method chaining
60100 */
61101 public function setPostfields (array $ array )
@@ -72,13 +112,20 @@ public function setPostfields(array $array)
72112
73113 $ this ->postfields = $ array ;
74114
115+ // rebuild oAuth
116+ if (isset ($ this ->oauth ['oauth_signature ' ])) {
117+ $ this ->buildOauth ($ this ->url , $ this ->requestMethod );
118+ }
119+
75120 return $ this ;
76121 }
77122
78123 /**
79124 * Set getfield string, example: '?screen_name=J7mbo'
80125 *
81126 * @param string $string Get key and value pairs as string
127+ *
128+ * @throws \Exception
82129 *
83130 * @return \TwitterAPIExchange Instance of self for method chaining
84131 */
@@ -121,9 +168,12 @@ public function getPostfields()
121168 /**
122169 * Build the Oauth object using params set in construct and additionals
123170 * passed to this method. For v1.1, see: https://dev.twitter.com/docs/api/1.1
124- *
171+ *
125172 * @param string $url The API url to use. Example: https://api.twitter.com/1.1/search/tweets.json
126173 * @param string $requestMethod Either POST or GET
174+ *
175+ * @throws \Exception
176+ *
127177 * @return \TwitterAPIExchange Instance of self for method chaining
128178 */
129179 public function buildOauth ($ url , $ requestMethod )
@@ -133,12 +183,12 @@ public function buildOauth($url, $requestMethod)
133183 throw new Exception ('Request method must be either POST or GET ' );
134184 }
135185
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 ;
139189 $ oauth_access_token_secret = $ this ->oauth_access_token_secret ;
140190
141- $ oauth = array (
191+ $ oauth = array (
142192 'oauth_consumer_key ' => $ consumer_key ,
143193 'oauth_nonce ' => time (),
144194 'oauth_signature_method ' => 'HMAC-SHA1 ' ,
@@ -152,19 +202,34 @@ public function buildOauth($url, $requestMethod)
152202 if (!is_null ($ getfield ))
153203 {
154204 $ getfields = str_replace ('? ' , '' , explode ('& ' , $ getfield ));
205+
155206 foreach ($ getfields as $ g )
156207 {
157208 $ 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+ }
159215 }
160216 }
161217
218+ $ postfields = $ this ->getPostfields ();
219+
220+ if (!is_null ($ postfields )) {
221+ foreach ($ postfields as $ key => $ value ) {
222+ $ oauth [$ key ] = $ value ;
223+ }
224+ }
225+
162226 $ base_info = $ this ->buildBaseString ($ url , $ requestMethod , $ oauth );
163227 $ composite_key = rawurlencode ($ consumer_secret ) . '& ' . rawurlencode ($ oauth_access_token_secret );
164228 $ oauth_signature = base64_encode (hash_hmac ('sha1 ' , $ base_info , $ composite_key , true ));
165229 $ oauth ['oauth_signature ' ] = $ oauth_signature ;
166230
167231 $ this ->url = $ url ;
232+ $ this ->requestMethod = $ requestMethod ;
168233 $ this ->oauth = $ oauth ;
169234
170235 return $ this ;
@@ -173,7 +238,9 @@ public function buildOauth($url, $requestMethod)
173238 /**
174239 * Perform the actual data retrieval from the API
175240 *
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
177244 *
178245 * @return string json If $return param is true, returns json data.
179246 */
@@ -183,13 +250,13 @@ public function performRequest($return = true)
183250 {
184251 throw new Exception ('performRequest parameter must be true or false ' );
185252 }
186-
187- $ header = array ($ this ->buildAuthorizationHeader ($ this ->oauth ), 'Expect: ' );
253+
254+ $ header = array ($ this ->buildAuthorizationHeader ($ this ->oauth ), 'Expect: ' );
188255
189256 $ getfield = $ this ->getGetfield ();
190257 $ postfields = $ this ->getPostfields ();
191258
192- $ options = array (
259+ $ options = array (
193260 CURLOPT_HTTPHEADER => $ header ,
194261 CURLOPT_HEADER => false ,
195262 CURLOPT_URL => $ this ->url ,
@@ -199,7 +266,7 @@ public function performRequest($return = true)
199266
200267 if (!is_null ($ postfields ))
201268 {
202- $ options [CURLOPT_POSTFIELDS ] = $ postfields ;
269+ $ options [CURLOPT_POSTFIELDS ] = http_build_query ( $ postfields) ;
203270 }
204271 else
205272 {
@@ -214,15 +281,15 @@ public function performRequest($return = true)
214281 $ json = curl_exec ($ feed );
215282 curl_close ($ feed );
216283
217- if ( $ return) { return $ json ; }
284+ return $ json ;
218285 }
219286
220287 /**
221288 * Private method to generate the base string used by cURL
222289 *
223290 * @param string $baseURI
224291 * @param string $method
225- * @param array $params
292+ * @param array $params
226293 *
227294 * @return string Built base string
228295 */
@@ -231,9 +298,9 @@ private function buildBaseString($baseURI, $method, $params)
231298 $ return = array ();
232299 ksort ($ params );
233300
234- foreach ($ params as $ key=> $ value )
301+ foreach ($ params as $ key => $ value )
235302 {
236- $ return [] = " $ key= " . $ value ;
303+ $ return [] = rawurlencode ( $ key) . ' = ' . rawurlencode ( $ value) ;
237304 }
238305
239306 return $ method . "& " . rawurlencode ($ baseURI ) . '& ' . rawurlencode (implode ('& ' , $ return ));
@@ -246,18 +313,45 @@ private function buildBaseString($baseURI, $method, $params)
246313 *
247314 * @return string $return Header used by cURL for request
248315 */
249- private function buildAuthorizationHeader ($ oauth )
316+ private function buildAuthorizationHeader (array $ oauth )
250317 {
251318 $ return = 'Authorization: OAuth ' ;
252319 $ values = array ();
253320
254321 foreach ($ oauth as $ key => $ value )
255322 {
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+ }
257327 }
258328
259329 $ return .= implode (', ' , $ values );
260330 return $ return ;
261331 }
262332
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+ }
263357}
0 commit comments