i have retrieve video media url from direct messages, and how do i upload the video as a tweet?
here’s my code

$user = $connection->get("direct_messages/events/list", ['count' => '5']);
    $someObject = json_decode(json_encode($user));
    foreach ($someObject->events as $item) {
       $media = $item->message_create->message_data->attachment->media->video_info->variants;
       $url = $media[0]->url;
     // the output from $url is https://video.twimg.com/dm_video/123/vid/123x123/vids.mp4?tag=1
    }

and now how can i upload the video?
ive tried using this but it doesnt work i guess

$postfields = array('media' => $url, 'media_category' => 'amplify_video');
$exc = $connection->post('https://upload.twitter.com/1.1/media/upload.json', $postfields);
$vids = json_decode(json_encode($exc));

$postfields = array('media_ids' => $vids->media_id_string, 'status' => 'This should be vids');
$exc = $connection->post('statuses/update', $postfields);
$ids = $exc->id_str;

but unfortunately the result was

[message] => Sorry, that page does not exist

[code] => 34

so it only tweet without the video, can anyone help me fix the problem? i still dont get it and it confused me.

Firstly, the correct media_category for video is tweet_video, not amplify_video.

Secondly, you should be using the chunked upload media method for video.

I can’t speak to how you would do this in PHP so you would need to consult the documentation for the library you are using.

Moving to the Media APIs category.

1 Like

Thank you, Andy.
i added this code

$media = $connection->upload('media/upload', 
        ['command' => 'INIT', 
         'media_type' => 'video/mp4', 
         'media' => $url, 
         'media_category' => 'tweet_video']);
$idstr = $media->media_id;  
// [media_id] => 1226619851847766016   [media_id_string] => 1226619851847766016 
// [expires_after_secs] => 86399   [media_key] => 7_1226619851847766016 

$result = $connection->post('statuses/update', 
         array('media_ids'=> $idstr, 
               'status'=> 'meow'
          ));

but when i execute the $result it shows

[code] => 324 
[message] => Invalid media id 1226619851847766016 

i still dont get it, its uploaded but the media id is invalid.

Thank you.
iam using @abraham api, but i cant find documentation for video upload/chunked upload, iam really confused uhh

You are missing the APPEND, FINALIZE, and STATUS steps of the chunked media upload. https://developer.twitter.com/en/docs/media/upload-media/uploading-media/chunked-media-upload

First split your file into chunks, you begin the upload with INIT, get a response from twitter with the media ID, then call APPEND several times with chunks of the file upload, then when all the file parts have successfully been uploaded, call FINALIZE, after that you can wait a few seconds or call STATUS https://developer.twitter.com/en/docs/media/upload-media/api-reference/get-media-upload-status and make sure that the message you get back has "state":"succeeded" and there are no errors. Then you may proceed and attach the media ID to a new status and tweet.

1 Like

i tried using this Posting large videos to Twitter in PHP

   $file = fopen('video.mp4', 'rb'); //file video.mp4
   $size = fstat($file)['size']; // size 1,5mb
    
// INIT
    $media1 = $connection->upload('media/upload', 
        [
        'command' => 'INIT',
        'media_type' => 'video/mp4',
        'media_category' => 'tweet_video',
        'total_bytes' => $size,
        ]);
    $mediaId = $media1->media_id_string;
    $segmentId = 0;
    
    // APPEND
    while (!feof($file)) {
        $chunk = fread($file, 2 * 1024 * 1024);
        $media2 = $connection->upload('media/upload', 
            [
            'command' => 'APPEND',
            'media_id' => $mediaId,
            'media' => $chunk,
            'segment_index' => $segmentId,
            ]);
        $segmentId++;
    }
    
    // FINALIZE
    fclose($file);
    $media3 = $connection->upload('media/upload', 
        [
        'command' => 'FINALIZE',
        'media_id' => $mediaId,
        ]); 
// But the output is

the output is always
Segments do not add up to provided total file size.
and when i check the status

    $media4 = $connection->upload('media/upload', 
        [
        'command' => 'STATUS',
        'media_id' => $mediaId,
        ]);

it says
media type unrecognized


duhh, i need some other documentation about this but cant find any. i think iam just gonna give up with this one.

I can’t try this out myself, but i’d look at the chunk size and maybe reduce that down - to like 1MB or something.

Also I would look at the actual chunk sizes and see if they add up - sometimes the last chunk doesn’t get updated before the file stops reading or something like that.

1 Like

The CodeBird example you linked to does work successfully, but it looks like your code is using a combination of the Codebird example with TwitterOAuth. I believe your issues may lie somewhere in here.

From what I can tell, TwitterOAuth manages the media upload chunked process through a boolean param called “chunked” in the upload function. As found here.

This allows for the TwitterOAuth library to use only one method call and eliminate the need for a separate INIT, APPEND, and FINALIZE call.

//Example of successful media chunked upload

  $media = $connection->upload('media/upload', 
        [
        'media_type' => 'video/mp4',
        'media_category' => 'tweet_video',
        'total_bytes' => $size,
        'media' => '[FILE_URL]',
        ],true);

However, you will need to check for the STATUS of the file to ensure the file has completed uploading before being tweeted. If the media is still processing, the tweet will post without an attached video.

//Example of Media Status call

 $media = $connection->mediaStatus($mediaId);
//Example of Media Status call using the check_after_secs field

if(isset($media->processing_info)) {
    $info = $media->processing_info;
    print_r($info->state);
    if($info->state != 'succeeded') {
        $attempts = 0;
        $checkAfterSecs = $info->check_after_secs;
        $success = false;
        do {
            $attempts++;
            sleep($checkAfterSecs);

            $media = $connection->mediaStatus($mediaId);

            $procInfo = $media->processing_info;

            if($procInfo->state == 'succeeded' || $procInfo->state == 'failed') {
                break;
            }

            $checkAfterSecs = $procInfo->check_after_secs;
        } while($attempts <= 10);
    }
}

Once the STATUS shows that the upload has succeeded, you can post a tweet with the media attached.

$result = $connection->post('statuses/update', 
        [
            'media_ids'=> $mediaId, 
            'status'=> 'This is the text of your tweet!'
        ]);

Hopefully this info helps!

2 Likes

Thank you @whereskarms finally it’s working.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.