Skip to content

Video Upload On twiter api using PHP #1228

Open
@Dagemgissila

Description

$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

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions