Skip to content

Commit 01026d2

Browse files
authored
Merge pull request #675 from abraham/json-request
Json request
2 parents ea135f2 + e4735d6 commit 01026d2

File tree

3 files changed

+69
-21
lines changed

3 files changed

+69
-21
lines changed

src/Request.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class Request
1010
protected $parameters;
1111
protected $httpMethod;
1212
protected $httpUrl;
13+
protected $json;
1314
public static $version = '1.0';
1415

1516
/**
@@ -43,7 +44,8 @@ public static function fromConsumerAndToken(
4344
Token $token = null,
4445
$httpMethod,
4546
$httpUrl,
46-
array $parameters = []
47+
array $parameters = [],
48+
$json = false
4749
) {
4850
$defaults = [
4951
"oauth_version" => Request::$version,
@@ -55,7 +57,13 @@ public static function fromConsumerAndToken(
5557
$defaults['oauth_token'] = $token->key;
5658
}
5759

58-
$parameters = array_merge($defaults, $parameters);
60+
// The json payload is not included in the signature on json requests,
61+
// therefore it shouldn't be included in the parameters array.
62+
if ($json) {
63+
$parameters = $defaults;
64+
} else {
65+
$parameters = array_merge($defaults, $parameters);
66+
}
5967

6068
return new Request($httpMethod, $httpUrl, $parameters);
6169
}

src/TwitterOAuth.php

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -206,20 +206,21 @@ public function oauth2($path, array $parameters = [])
206206
*/
207207
public function get($path, array $parameters = [])
208208
{
209-
return $this->http('GET', self::API_HOST, $path, $parameters);
209+
return $this->http('GET', self::API_HOST, $path, $parameters, false);
210210
}
211211

212212
/**
213213
* Make POST requests to the API.
214214
*
215215
* @param string $path
216216
* @param array $parameters
217+
* @param bool $json
217218
*
218219
* @return array|object
219220
*/
220-
public function post($path, array $parameters = [])
221+
public function post($path, array $parameters = [], $json = false)
221222
{
222-
return $this->http('POST', self::API_HOST, $path, $parameters);
223+
return $this->http('POST', self::API_HOST, $path, $parameters, $json);
223224
}
224225

225226
/**
@@ -232,7 +233,7 @@ public function post($path, array $parameters = [])
232233
*/
233234
public function delete($path, array $parameters = [])
234235
{
235-
return $this->http('DELETE', self::API_HOST, $path, $parameters);
236+
return $this->http('DELETE', self::API_HOST, $path, $parameters, false);
236237
}
237238

238239
/**
@@ -245,7 +246,7 @@ public function delete($path, array $parameters = [])
245246
*/
246247
public function put($path, array $parameters = [])
247248
{
248-
return $this->http('PUT', self::API_HOST, $path, $parameters);
249+
return $this->http('PUT', self::API_HOST, $path, $parameters, false);
249250
}
250251

251252
/**
@@ -278,7 +279,7 @@ public function mediaStatus($media_id)
278279
return $this->http('GET', self::UPLOAD_HOST, 'media/upload', [
279280
'command' => 'STATUS',
280281
'media_id' => $media_id
281-
]);
282+
], false);
282283
}
283284

284285
/**
@@ -296,7 +297,7 @@ private function uploadMediaNotChunked($path, array $parameters)
296297
throw new \InvalidArgumentException('You must supply a readable file');
297298
}
298299
$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);
300301
}
301302

302303
/**
@@ -309,7 +310,7 @@ private function uploadMediaNotChunked($path, array $parameters)
309310
*/
310311
private function uploadMediaChunked($path, array $parameters)
311312
{
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);
313314
// Append
314315
$segmentIndex = 0;
315316
$media = fopen($parameters['media'], 'rb');
@@ -319,14 +320,14 @@ private function uploadMediaChunked($path, array $parameters)
319320
'media_id' => $init->media_id_string,
320321
'segment_index' => $segmentIndex++,
321322
'media_data' => base64_encode(fread($media, $this->chunkSize))
322-
]);
323+
], false);
323324
}
324325
fclose($media);
325326
// Finalize
326327
$finalize = $this->http('POST', self::UPLOAD_HOST, 'media/upload', [
327328
'command' => 'FINALIZE',
328329
'media_id' => $init->media_id_string
329-
]);
330+
], false);
330331
return $finalize;
331332
}
332333

@@ -359,16 +360,17 @@ private function mediaInitParameters(array $parameters)
359360
* @param string $host
360361
* @param string $path
361362
* @param array $parameters
363+
* @param bool $json
362364
*
363365
* @return array|object
364366
*/
365-
private function http($method, $host, $path, array $parameters)
367+
private function http($method, $host, $path, array $parameters, $json)
366368
{
367369
$this->resetLastResponse();
368370
$this->resetAttemptsNumber();
369371
$url = sprintf('%s/%s/%s.json', $host, self::API_VERSION, $path);
370372
$this->response->setApiPath($path);
371-
return $this->makeRequests($url, $method, $parameters);
373+
return $this->makeRequests($url, $method, $parameters, $json);
372374
}
373375

374376
/**
@@ -379,14 +381,15 @@ private function http($method, $host, $path, array $parameters)
379381
* @param string $url
380382
* @param string $method
381383
* @param array $parameters
384+
* @param bool $json
382385
*
383386
* @return array|object
384387
*/
385-
private function makeRequests($url, $method, array $parameters)
388+
private function makeRequests($url, $method, array $parameters, $json)
386389
{
387390
do {
388391
$this->sleepIfNeeded();
389-
$result = $this->oAuthRequest($url, $method, $parameters);
392+
$result = $this->oAuthRequest($url, $method, $parameters, $json);
390393
$response = JsonDecoder::decode($result, $this->decodeJsonAsArray);
391394
$this->response->setBody($response);
392395
$this->attempts++;
@@ -412,13 +415,14 @@ private function requestsAvailable()
412415
* @param string $url
413416
* @param string $method
414417
* @param array $parameters
418+
* @param bool $json
415419
*
416420
* @return string
417421
* @throws TwitterOAuthException
418422
*/
419-
private function oAuthRequest($url, $method, array $parameters)
423+
private function oAuthRequest($url, $method, array $parameters, $json = false)
420424
{
421-
$request = Request::fromConsumerAndToken($this->consumer, $this->token, $method, $url, $parameters);
425+
$request = Request::fromConsumerAndToken($this->consumer, $this->token, $method, $url, $parameters, $json);
422426
if (array_key_exists('oauth_callback', $parameters)) {
423427
// Twitter doesn't like oauth_callback as a parameter.
424428
unset($parameters['oauth_callback']);
@@ -434,7 +438,7 @@ private function oAuthRequest($url, $method, array $parameters)
434438
} else {
435439
$authorization = 'Authorization: Bearer ' . $this->bearer;
436440
}
437-
return $this->request($request->getNormalizedHttpUrl(), $method, $authorization, $parameters);
441+
return $this->request($request->getNormalizedHttpUrl(), $method, $authorization, $parameters, $json);
438442
}
439443

440444
/**
@@ -481,11 +485,12 @@ private function curlOptions()
481485
* @param string $method
482486
* @param string $authorization
483487
* @param array $postfields
488+
* @param bool $json
484489
*
485490
* @return string
486491
* @throws TwitterOAuthException
487492
*/
488-
private function request($url, $method, $authorization, array $postfields)
493+
private function request($url, $method, $authorization, array $postfields, $json = false)
489494
{
490495
$options = $this->curlOptions();
491496
$options[CURLOPT_URL] = $url;
@@ -496,7 +501,12 @@ private function request($url, $method, $authorization, array $postfields)
496501
break;
497502
case 'POST':
498503
$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+
}
500510
break;
501511
case 'DELETE':
502512
$options[CURLOPT_CUSTOMREQUEST] = 'DELETE';

tests/TwitterOAuthTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class TwitterOAuthTest extends \PHPUnit_Framework_TestCase
1414
protected function setUp()
1515
{
1616
$this->twitter = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET);
17+
$this->userId = explode('-', ACCESS_TOKEN)[0];
1718
}
1819

1920
public function testBuildClient()
@@ -185,6 +186,35 @@ public function testPostFavoritesDestroy()
185186
$this->assertEquals(200, $this->twitter->getLastHttpCode());
186187
}
187188

189+
public function testPostDirectMessagesEventsNew()
190+
{
191+
$data = [
192+
'event' => [
193+
'type' => 'message_create',
194+
'message_create' => [
195+
'target' => [
196+
'recipient_id' => $this->userId
197+
],
198+
'message_data' => [
199+
'text' => 'Hello World!'
200+
]
201+
]
202+
]
203+
];
204+
$result = $this->twitter->post('direct_messages/events/new', $data, true);
205+
$this->assertEquals(200, $this->twitter->getLastHttpCode());
206+
return $result;
207+
}
208+
209+
/**
210+
* @depends testPostDirectMessagesEventsNew
211+
*/
212+
public function testDeleteDirectMessagesEventsDestroy($message)
213+
{
214+
$this->twitter->delete('direct_messages/events/destroy', ['id' => $message->event->id]);
215+
$this->assertEquals(204, $this->twitter->getLastHttpCode());
216+
}
217+
188218
public function testPostStatusesUpdateWithMedia()
189219
{
190220
$this->twitter->setTimeouts(60, 30);

0 commit comments

Comments
 (0)