Ramya.K
Ramya.K

Reputation: 19

Twitter Video share using CodeBird API

I'm trying to share a video to Twitter Account but getting a response like below

stdClass Object ( [request] => /1.1/media/upload.json [error] => Segments do not add up to provided total file size. [httpstatus] => 400 [rate] => stdClass Object ( [limit] => 615 [remaining] => 613 [reset] => 1576651447 ) )

I don't know what to do to fix this issue. If anyone experienced this please let me know.

This is my code to share video to twitter.

\Codebird\Codebird::setConsumerKey(CONSUMER_KEY, CONSUMER_SECRET);        
$cb = \Codebird\Codebird::getInstance();        
$cb->setToken($access_token, $access_token_secret);        
$file       = $videopath;       
$size_bytes = filesize($file);       
$fp         = fopen($file, 'r');

// INIT
$reply = $cb->media_upload([
'command'     => 'INIT',
'media_type'  => 'video/mp4',
'total_bytes' => $size_bytes
]);

$media_id = $reply->media_id_string;

// APPEND

$segment_id = 0;

while (! feof($fp)) {
    $chunk = fread($fp, 1048576); // 1MB per chunk for this sample

    $reply = $cb->media_upload([
        'command'       => 'APPEND',
        'media_id'      => $media_id,
        'segment_index' => $segment_id,
        'media'         => $chunk
    ]);

    $segment_id++;
}

fclose($fp);

// FINALIZE

$reply = $cb->media_upload([
    'command'       => 'FINALIZE',
    'media_id'      => $media_id
]);

if ($reply->httpstatus < 200 || $reply->httpstatus > 299) { 
    die();
}

// Now use the media_id in a Tweet
$reply = $cb->statuses_update([
'status'    => $msg,
'media_ids' => $media_id
]);

I'm getting an error in the Finalize command result.

I tried to fix this but I can't because I don't know the exact reason for this issue and I didn't know how to handle and fix this. Can anyone help me to fix this? The above is the code I have used to share the post to the twitter account. And one thing is, the above code works fine when I tried to share the post immediately to twitter using this API but the issue is only when I tried to share the post by scheduling it with a specific time to share.

And when I trying to share a video below 1mb it gets shared. When I tried a video with 1mb or more than that I got the below error while sharing to account.

{"errors":[{"code":324,"message":"Invalid media id 1215261697298108416"}],"httpstatus":400,"rate":null}

Upvotes: 1

Views: 114

Answers (0)

Related Questions