Skip to content

Commit 269a7ee

Browse files
authored
Merge pull request #7 from socialdog-inc/ST-9009
2 parents 24f6344 + cc87381 commit 269a7ee

File tree

1 file changed

+25
-8
lines changed

1 file changed

+25
-8
lines changed

src/TwitterOAuth.php

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,7 @@ public function uploadV2($path, array $parameters = [])
332332
'media' => fread($media, $this->chunkSize)
333333
],
334334
false,
335+
true,
335336
);
336337
}
337338
fclose($media);
@@ -579,7 +580,8 @@ private function http(
579580
string $host,
580581
string $path,
581582
array $parameters,
582-
bool $json
583+
bool $json,
584+
bool $isBinaryMultipart = false
583585
) {
584586
$this->resetLastResponse();
585587
$this->resetAttemptsNumber();
@@ -593,6 +595,7 @@ private function http(
593595
$method,
594596
$parameters,
595597
$json,
598+
$isBinaryMultipart
596599
);
597600
}
598601

@@ -648,11 +651,12 @@ private function makeRequests(
648651
string $url,
649652
string $method,
650653
array $parameters,
651-
bool $json
654+
bool $json,
655+
bool $isBinaryMultipart = false
652656
) {
653657
do {
654658
$this->sleepIfNeeded();
655-
$result = $this->oAuthRequest($url, $method, $parameters, $json);
659+
$result = $this->oAuthRequest($url, $method, $parameters, $json, $isBinaryMultipart);
656660
$response = JsonDecoder::decode($result, $this->decodeJsonAsArray);
657661
$this->response->setBody($response);
658662
$this->attempts++;
@@ -689,15 +693,18 @@ private function oAuthRequest(
689693
string $url,
690694
string $method,
691695
array $parameters,
692-
bool $json = false
696+
bool $json = false,
697+
bool $isBinaryMultipart = false
693698
) {
694699
$request = Request::fromConsumerAndToken(
695700
$this->consumer,
696701
$this->token,
697702
$method,
698703
$url,
699704
$parameters,
700-
$json,
705+
// jsonに加えて、binary multipartリクエストの場合でもbodyをoauthのsignatureの生成に利用しない
706+
// @see https://developer.x.com/en/docs/x-api/v1/media/upload-media/uploading-media/media-best-practices
707+
$json || $isBinaryMultipart
701708
);
702709
if (array_key_exists('oauth_callback', $parameters)) {
703710
// Twitter doesn't like oauth_callback as a parameter.
@@ -724,6 +731,7 @@ private function oAuthRequest(
724731
$authorization,
725732
$parameters,
726733
$json,
734+
$isBinaryMultipart
727735
);
728736
}
729737

@@ -780,7 +788,8 @@ private function request(
780788
string $method,
781789
string $authorization,
782790
array $postfields,
783-
bool $json = false
791+
bool $json = false,
792+
bool $isBinaryMultipart = false
784793
): string {
785794
$options = $this->curlOptions();
786795
$options[CURLOPT_URL] = $url;
@@ -799,6 +808,7 @@ private function request(
799808
$options,
800809
$postfields,
801810
$json,
811+
$isBinaryMultipart
802812
);
803813
break;
804814
case 'DELETE':
@@ -903,7 +913,8 @@ private function curlCaOpt(string $path): int
903913
private function setPostfieldsOptions(
904914
array $options,
905915
array $postfields,
906-
bool $json
916+
bool $json,
917+
bool $isBinaryMultipart = false
907918
): array {
908919
if ($json) {
909920
$options[CURLOPT_HTTPHEADER][] = 'Content-type: application/json';
@@ -912,7 +923,13 @@ private function setPostfieldsOptions(
912923
JSON_THROW_ON_ERROR,
913924
);
914925
} else {
915-
$options[CURLOPT_POSTFIELDS] = Util::buildHttpQuery($postfields);
926+
if ($isBinaryMultipart) {
927+
// binary multipartリクエストの場合headerを設定し、postデータをエンコードせず直接設定(X API v2 メディアアップロードの対応)
928+
$options[CURLOPT_HTTPHEADER][] = 'Content-type: multipart/form-data';
929+
$options[CURLOPT_POSTFIELDS] = $postfields;
930+
} else {
931+
$options[CURLOPT_POSTFIELDS] = Util::buildHttpQuery($postfields);
932+
}
916933
}
917934

918935
return $options;

0 commit comments

Comments
 (0)