Reputation: 53
I am trying to upload a video with text to Twitter using API. To accomplish that, I am using this PHP library - https://github.com/jublo/codebird-php and I followed this tutorial - https://joelennon.com/posting-large-videos-to-twitter-in-php-with-codebird.
My final code is similar to the tutorial:-
$file = fopen('svid.mp4', 'rb');
$size = fstat($file)['size'];
$media = $twitter->media_upload([
'command' => 'INIT',
'media_type' => 'video/mp4',
'media_category' => 'tweet_video',
'total_bytes' => $size,
]);
$mediaId = $media->media_id_string;
$segmentId = 0;
while (!feof($file)) {
$chunk = fread($file, 4 * 1024 * 1024);
$media = $twitter->media_upload([
'command' => 'APPEND',
'media_id' => $mediaId,
'segment_index' => $segmentId,
'media' => $chunk,
]);
$segmentId++;
}
fclose($file);
$media = $twitter->media_upload([
'command' => 'FINALIZE',
'media_id' => $mediaId,
]);
var_dump($media);
if(isset($media->processing_info)) {
$info = $media->processing_info;
if($info->state != 'succeeded') {
$attempts = 0;
$checkAfterSecs = $info->check_after_secs;
$success = false;
do {
$attempts++;
sleep($checkAfterSecs);
$media = $twitter->media_upload([
'command' => 'STATUS',
'media_id' => $mediaId,
]);
$procInfo = $media->processing_info;
if($procInfo->state == 'succeeded' || $procInfo->state == 'failed') {
break;
}
$checkAfterSecs = $procInfo->check_after_secs;
} while($attempts <= 10);
}
}
$params = [
'status' => 'This is the text of your tweet!',
'media_ids' => $mediaId,
];
$tweet = $twitter->statuses_update($params);
Now, once the upload is complete, the var_dump
looks like this:-
object(stdClass)#7 (8) { ["media_id"]=> int(1879869571785728000) ["media_id_string"]=> string(19) "1879869571785728000" ["media_key"]=> string(21) "7_1879869571785728000" ["size"]=> int(2848208) ["expires_after_secs"]=> int(86400) ["processing_info"]=> object(stdClass)#5 (2) { ["state"]=> string(7) "pending" ["check_after_secs"]=> int(1) } ["httpstatus"]=> int(200) ["rate"]=> object(stdClass)#10 (3) { ["limit"]=> string(3) "615" ["remaining"]=> string(3) "605" ["reset"]=> string(10) "1737032398" } }
As you can see, the state
is pending
. So, it won't be possible to post with the following code - $tweet = $twitter->statuses_update($params);
. So, I tried running this to check out the issue:-
$media = $twitter->media_upload([
'command' => 'STATUS',
'media_id' => '1879869571785728000',
]);
This throws the following:-
object(stdClass)#6 (3) { ["errors"]=> array(1) { [0]=> object(stdClass)#9 (2) { ["code"]=> int(38) ["message"]=> string(27) "media parameter is missing." } } ["httpstatus"]=> int(400) ["rate"]=> object(stdClass)#11 (3) { ["limit"]=> string(3) "415" ["remaining"]=> string(3) "391" ["reset"]=> string(10) "1737032321" } }
I have tried researching this, but, haven't found a solution. Now, here are the issues I am facing, summarised:-
media parameter is missing
when checking status of upload. So, I have no way of knowing if the upload was successful and processed successfully.$tweet = $twitter->statuses_update($params);
doesn't work, even with the media_ids
removed (i.e., only text). I tried adding a tweet with two other libraries (https://github.com/abraham/twitteroauth and https://github.com/noweh/twitter-api-v2-php). In both these cases, simple text works, but, with media it doesn't work. But, I have been trying to migrate to Codebird because of the ease of doing chunked uploads.Can someone point me in the right direction and tell me what I am doing wrong?
UPDATE 1: Fixed media parameter is missing
Source - https://github.com/jublo/codebird-php/issues/260
$media = $twitter->media_upload([
'command' => 'STATUS',
'media_id' => $mediaId,
'httpmethod' => 'GET'
]);
Upvotes: -1
Views: 40