Skip to content

Commit 9f8c5b8

Browse files
Merge pull request #5 from Codeinwp/fix/image-upload
fix: Share as image not working for Twitter
2 parents 5fa8704 + 493acb1 commit 9f8c5b8

1 file changed

Lines changed: 13 additions & 16 deletions

File tree

src/TwitterOAuth.php

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -375,9 +375,10 @@ private function uploadMediaNotChunked(string $path, array $parameters)
375375
'You must supply a readable file',
376376
);
377377
}
378+
378379
$parameters['media'] = base64_encode($file);
379380
return $this->http('POST', self::API_HOST, $path, $parameters, [
380-
'jsonPayload' => false,
381+
'jsonPayload' => true,
381382
]);
382383
}
383384

@@ -395,10 +396,11 @@ private function uploadMediaChunked(string $path, array $parameters)
395396
$init = $this->http(
396397
'POST',
397398
self::API_HOST,
398-
$path,
399+
'media/upload/initialize',
399400
$this->mediaInitParameters($parameters),
400-
['jsonPayload' => false],
401+
['jsonPayload' => true],
401402
);
403+
402404
if (!property_exists($init, 'data')) {
403405
throw new TwitterOAuthException(
404406
$init->errors[0]->message ?? 'Missing "data"',
@@ -408,30 +410,26 @@ private function uploadMediaChunked(string $path, array $parameters)
408410
$segmentIndex = 0;
409411
$media = fopen($parameters['media'], 'rb');
410412
while (!feof($media)) {
411-
$this->http(
413+
$req = $this->http(
412414
'POST',
413415
self::API_HOST,
414-
'media/upload',
416+
"media/upload/{$init->data->id}/append",
415417
[
416-
'command' => 'APPEND',
417-
'media_id' => $init->data->id,
418418
'segment_index' => $segmentIndex++,
419-
'media' => new CURLFile('data://application/octet-stream;base64,' . base64_encode(fread($media, $this->chunkSize)), $parameters['media_type'], 'chunk'),
419+
'media' => base64_encode(fread($media, $this->chunkSize)),
420420
],
421-
['jsonPayload' => false],
421+
['jsonPayload' => true],
422422
);
423423
}
424424
fclose($media);
425+
425426
// Finalize
426427
$finalize = $this->http(
427428
'POST',
428429
self::API_HOST,
429-
'media/upload',
430-
[
431-
'command' => 'FINALIZE',
432-
'media_id' => $init->data->id,
433-
],
434-
['jsonPayload' => false],
430+
"media/upload/{$init->data->id}/finalize",
431+
[],
432+
['jsonPayload' => true],
435433
);
436434
return $finalize;
437435
}
@@ -453,7 +451,6 @@ private function mediaInitParameters(array $parameters): array
453451
'shared',
454452
];
455453
$base = [
456-
'command' => 'INIT',
457454
'total_bytes' => filesize($parameters['media']),
458455
];
459456
$allowed_parameters = array_intersect_key(

0 commit comments

Comments
 (0)