Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Video Upload On twiter api using PHP #1228

Open
Dagemgissila opened this issue Apr 24, 2024 · 1 comment
Open

Video Upload On twiter api using PHP #1228

Dagemgissila opened this issue Apr 24, 2024 · 1 comment
Labels

Comments

@Dagemgissila
Copy link

Dagemgissila commented Apr 24, 2024

$video = $_FILES['videoFile']['tmp_name'];
$twitter->setApiVersion('1.1');

$code = $twitter->upload('media/upload',[
    'media' => $video,
    "command"=>"INIT",
    'media_type' => 'video/mp4',
    'media_category' => 'tweet_video',
    "total_bytes" => (int)filesize($video),
]);
$twitter->setApiVersion(1.1);

$total_bytes=(int)filesize($video);
$twitter->setApiVersion('2');
$video_media_id = $code->media_id_string;
$media_id = $code->media_id;
var_dump("id".$media_id);
$fp = fopen($video, 'rb'); // Open the video file for reading
$segment_id = 0;
$bytes_sent = 0;

while ($bytes_sent < $total_bytes) {
    $chunk = fread($fp, 4 * 1024 * 1024); // Read a chunk of 4MB
    
    echo 'APPEND\n'.$total_bytes;
    
    $request_data = [
        'command' => 'APPEND',
        'media_id' => $media_id,
        'segment_index' => $segment_id,
        "media_data" => base64_encode($chunk),
    ];

  
 
 $response = $twitter->upload('media/upload', ['media'=>$chunk]);

 

    $segment_id++;
    $bytes_sent = ftell($fp); // Get the current position of the file pointer
    
    // echo $bytes_sent . ' of ' . $total_bytes . ' bytes uploaded\n';
}

fclose($fp); // Close the file
echo "Upload chunks complete.\n";
$final=$twitter->upload('media/upload',
["command" => "FINALIZE",
"media_id" => $media_id]
);
var_dump($final);
echo '------------------------------------------------------<br>';
$parameters = [
    'status' => 'hear is my video twittsvbn',
    'media_id' => $media_id
    ];
print_r($parameters);
echo '------------------------------------------------------<br>';		
$tweet=  $twitter->post('statuses/update',  $parameters);

echo json_encode($tweet);

Hello Guys i try to upload video to X but the above code say me Warning: Undefined array key "media" in C:\xampp\htdocs\mework\vendor\abraham\twitteroauth\src\TwitterOAuth.php on line 358

Fatal error: Uncaught TypeError: is_readable(): Argument #1 ($filename) must be of type string, null given in C:\xampp\htdocs\mework\vendor\abraham\twitteroauth\src\TwitterOAuth.php:358 Stack trace: #0 C:\xampp\htdocs\mework\vendor\abraham\twitteroauth\src\TwitterOAuth.php(358): is_readable(NULL) #1 C:\xampp\htdocs\mework\vendor\abraham\twitteroauth\src\TwitterOAuth.php(322): Abraham\TwitterOAuth\TwitterOAuth->uploadMediaNotChunked('media/upload', Array) #2 C:\xampp\htdocs\mework\index.php(65): Abraham\TwitterOAuth\TwitterOAuth->upload('media/upload', Array) #3 {main} thrown in C:\xampp\htdocs\mework\vendor\abraham\twitteroauth\src\TwitterOAuth.php on line 358

@Aeknasd145
Copy link

#1227 check this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants