@@ -206,20 +206,21 @@ public function oauth2($path, array $parameters = [])
206
206
*/
207
207
public function get ($ path , array $ parameters = [])
208
208
{
209
- return $ this ->http ('GET ' , self ::API_HOST , $ path , $ parameters );
209
+ return $ this ->http ('GET ' , self ::API_HOST , $ path , $ parameters, false );
210
210
}
211
211
212
212
/**
213
213
* Make POST requests to the API.
214
214
*
215
215
* @param string $path
216
216
* @param array $parameters
217
+ * @param bool $json
217
218
*
218
219
* @return array|object
219
220
*/
220
- public function post ($ path , array $ parameters = [])
221
+ public function post ($ path , array $ parameters = [], $ json = false )
221
222
{
222
- return $ this ->http ('POST ' , self ::API_HOST , $ path , $ parameters );
223
+ return $ this ->http ('POST ' , self ::API_HOST , $ path , $ parameters, $ json );
223
224
}
224
225
225
226
/**
@@ -232,7 +233,7 @@ public function post($path, array $parameters = [])
232
233
*/
233
234
public function delete ($ path , array $ parameters = [])
234
235
{
235
- return $ this ->http ('DELETE ' , self ::API_HOST , $ path , $ parameters );
236
+ return $ this ->http ('DELETE ' , self ::API_HOST , $ path , $ parameters, false );
236
237
}
237
238
238
239
/**
@@ -245,7 +246,7 @@ public function delete($path, array $parameters = [])
245
246
*/
246
247
public function put ($ path , array $ parameters = [])
247
248
{
248
- return $ this ->http ('PUT ' , self ::API_HOST , $ path , $ parameters );
249
+ return $ this ->http ('PUT ' , self ::API_HOST , $ path , $ parameters, false );
249
250
}
250
251
251
252
/**
@@ -278,7 +279,7 @@ public function mediaStatus($media_id)
278
279
return $ this ->http ('GET ' , self ::UPLOAD_HOST , 'media/upload ' , [
279
280
'command ' => 'STATUS ' ,
280
281
'media_id ' => $ media_id
281
- ]);
282
+ ], false );
282
283
}
283
284
284
285
/**
@@ -296,7 +297,7 @@ private function uploadMediaNotChunked($path, array $parameters)
296
297
throw new \InvalidArgumentException ('You must supply a readable file ' );
297
298
}
298
299
$ parameters ['media ' ] = base64_encode ($ file );
299
- return $ this ->http ('POST ' , self ::UPLOAD_HOST , $ path , $ parameters );
300
+ return $ this ->http ('POST ' , self ::UPLOAD_HOST , $ path , $ parameters, false );
300
301
}
301
302
302
303
/**
@@ -309,7 +310,7 @@ private function uploadMediaNotChunked($path, array $parameters)
309
310
*/
310
311
private function uploadMediaChunked ($ path , array $ parameters )
311
312
{
312
- $ init = $ this ->http ('POST ' , self ::UPLOAD_HOST , $ path , $ this ->mediaInitParameters ($ parameters ));
313
+ $ init = $ this ->http ('POST ' , self ::UPLOAD_HOST , $ path , $ this ->mediaInitParameters ($ parameters ), false );
313
314
// Append
314
315
$ segmentIndex = 0 ;
315
316
$ media = fopen ($ parameters ['media ' ], 'rb ' );
@@ -319,14 +320,14 @@ private function uploadMediaChunked($path, array $parameters)
319
320
'media_id ' => $ init ->media_id_string ,
320
321
'segment_index ' => $ segmentIndex ++,
321
322
'media_data ' => base64_encode (fread ($ media , $ this ->chunkSize ))
322
- ]);
323
+ ], false );
323
324
}
324
325
fclose ($ media );
325
326
// Finalize
326
327
$ finalize = $ this ->http ('POST ' , self ::UPLOAD_HOST , 'media/upload ' , [
327
328
'command ' => 'FINALIZE ' ,
328
329
'media_id ' => $ init ->media_id_string
329
- ]);
330
+ ], false );
330
331
return $ finalize ;
331
332
}
332
333
@@ -359,16 +360,17 @@ private function mediaInitParameters(array $parameters)
359
360
* @param string $host
360
361
* @param string $path
361
362
* @param array $parameters
363
+ * @param bool $json
362
364
*
363
365
* @return array|object
364
366
*/
365
- private function http ($ method , $ host , $ path , array $ parameters )
367
+ private function http ($ method , $ host , $ path , array $ parameters, $ json )
366
368
{
367
369
$ this ->resetLastResponse ();
368
370
$ this ->resetAttemptsNumber ();
369
371
$ url = sprintf ('%s/%s/%s.json ' , $ host , self ::API_VERSION , $ path );
370
372
$ this ->response ->setApiPath ($ path );
371
- return $ this ->makeRequests ($ url , $ method , $ parameters );
373
+ return $ this ->makeRequests ($ url , $ method , $ parameters, $ json );
372
374
}
373
375
374
376
/**
@@ -379,14 +381,15 @@ private function http($method, $host, $path, array $parameters)
379
381
* @param string $url
380
382
* @param string $method
381
383
* @param array $parameters
384
+ * @param bool $json
382
385
*
383
386
* @return array|object
384
387
*/
385
- private function makeRequests ($ url , $ method , array $ parameters )
388
+ private function makeRequests ($ url , $ method , array $ parameters, $ json )
386
389
{
387
390
do {
388
391
$ this ->sleepIfNeeded ();
389
- $ result = $ this ->oAuthRequest ($ url , $ method , $ parameters );
392
+ $ result = $ this ->oAuthRequest ($ url , $ method , $ parameters, $ json );
390
393
$ response = JsonDecoder::decode ($ result , $ this ->decodeJsonAsArray );
391
394
$ this ->response ->setBody ($ response );
392
395
$ this ->attempts ++;
@@ -412,13 +415,14 @@ private function requestsAvailable()
412
415
* @param string $url
413
416
* @param string $method
414
417
* @param array $parameters
418
+ * @param bool $json
415
419
*
416
420
* @return string
417
421
* @throws TwitterOAuthException
418
422
*/
419
- private function oAuthRequest ($ url , $ method , array $ parameters )
423
+ private function oAuthRequest ($ url , $ method , array $ parameters, $ json = false )
420
424
{
421
- $ request = Request::fromConsumerAndToken ($ this ->consumer , $ this ->token , $ method , $ url , $ parameters );
425
+ $ request = Request::fromConsumerAndToken ($ this ->consumer , $ this ->token , $ method , $ url , $ parameters, $ json );
422
426
if (array_key_exists ('oauth_callback ' , $ parameters )) {
423
427
// Twitter doesn't like oauth_callback as a parameter.
424
428
unset($ parameters ['oauth_callback ' ]);
@@ -434,7 +438,7 @@ private function oAuthRequest($url, $method, array $parameters)
434
438
} else {
435
439
$ authorization = 'Authorization: Bearer ' . $ this ->bearer ;
436
440
}
437
- return $ this ->request ($ request ->getNormalizedHttpUrl (), $ method , $ authorization , $ parameters );
441
+ return $ this ->request ($ request ->getNormalizedHttpUrl (), $ method , $ authorization , $ parameters, $ json );
438
442
}
439
443
440
444
/**
@@ -481,11 +485,12 @@ private function curlOptions()
481
485
* @param string $method
482
486
* @param string $authorization
483
487
* @param array $postfields
488
+ * @param bool $json
484
489
*
485
490
* @return string
486
491
* @throws TwitterOAuthException
487
492
*/
488
- private function request ($ url , $ method , $ authorization , array $ postfields )
493
+ private function request ($ url , $ method , $ authorization , array $ postfields, $ json = false )
489
494
{
490
495
$ options = $ this ->curlOptions ();
491
496
$ options [CURLOPT_URL ] = $ url ;
@@ -496,7 +501,12 @@ private function request($url, $method, $authorization, array $postfields)
496
501
break ;
497
502
case 'POST ' :
498
503
$ options [CURLOPT_POST ] = true ;
499
- $ options [CURLOPT_POSTFIELDS ] = Util::buildHttpQuery ($ postfields );
504
+ if ($ json ) {
505
+ $ options [CURLOPT_HTTPHEADER ][] = 'Content-type: application/json ' ;
506
+ $ options [CURLOPT_POSTFIELDS ] = json_encode ($ postfields );
507
+ } else {
508
+ $ options [CURLOPT_POSTFIELDS ] = Util::buildHttpQuery ($ postfields );
509
+ }
500
510
break ;
501
511
case 'DELETE ' :
502
512
$ options [CURLOPT_CUSTOMREQUEST ] = 'DELETE ' ;
0 commit comments