yousef2002307's avatar

i have a problem uploading video to twitter using laravel 11

that is teh code:

public function uploadVideo(Request $request) { // Increase the maximum execution time to 300 seconds (5 minutes) set_time_limit(300);

// Set your access token details
$userFirst = User::find(auth()->user()->id);
if ($userFirst->social_type != "twitter") {
    $account_id = $userFirst->account_id;
    $user = User::where('account_id', $account_id)
        ->where('social_type', 'twitter')
        ->where('role', 0)
        ->first();
    if (!$user) {
        return response()->json(["message" => "No Twitter account found"], 404);
    }
} else {
    $user = User::find(auth()->user()->id);
}

$accessToken = $user->access_token;
$accessTokenSecret = $user->refresh_token;

// Initialize TwitterOAuth with your app credentials and the user's access token
$twitter = new TwitterOAuth(
    config('services.twitter.client_id'),
    config('services.twitter.client_secret'),
    $accessToken,
    $accessTokenSecret
);

// Increase the timeout settings
$twitter->setTimeouts(30, 300); // Set the connect timeout to 30 seconds and the request timeout to 300 seconds

// Tell the library we want to use the v1.1 API for media upload
$twitter->setApiVersion('1.1');

if ($request->hasFile('video')) {
    $videoFile = $request->file('video');
    $videoPath = $videoFile->getPathname();
    $videoMimeType = $videoFile->getMimeType();
    $videoExtension = $videoFile->getClientOriginalExtension();
    $newVideoPath = $videoPath . '.' . $videoExtension;

    // Rename the file with the correct extension
    rename($videoPath, $newVideoPath);

    $videoSize = filesize($newVideoPath);

    // Initialize the upload
    $initResponse = $twitter->upload('media/upload', [
        'command' => 'INIT',
        'media_type' => $videoMimeType,
        'total_bytes' => $videoSize,
        'media_category' => 'tweet_video'
    ]);

    if (!isset($initResponse->media_id_string)) {
        return response()->json([
            "message" => "Failed to initialize video upload",
            "error" => $initResponse
        ], 500);
    }

    $mediaId = $initResponse->media_id_string;

    // Append the media chunks
    $segmentIndex = 0;
    $chunkSize = 5 * 1024 * 1024; // 5MB per chunk
    $handle = fopen($newVideoPath, 'rb');
    while (!feof($handle)) {
        $chunk = fread($handle, $chunkSize);
        $appendResponse = $twitter->upload('media/upload', [
            'command' => 'APPEND',
            'media_id' => $mediaId,
            'segment_index' => $segmentIndex,
            'media' => new \CURLFile($newVideoPath, $videoMimeType, $videoFile->getClientOriginalName())
        ], true);

        if ($appendResponse !== true) {
            return response()->json([
                "message" => "Failed to append video chunk",
                "error" => $appendResponse
            ], 500);
        }

        $segmentIndex++;
    }
    fclose($handle);

    // Finalize the upload
    $finalizeResponse = $twitter->upload('media/upload', [
        'command' => 'FINALIZE',
        'media_id' => $mediaId
    ]);

    if (isset($finalizeResponse->media_id_string)) {
        return response()->json([
            "message" => "Video uploaded successfully",
            "media_id" => $finalizeResponse->media_id_string
        ], 200);
    } else {
        return response()->json([
            "message" => "Failed to finalize video upload",
            "error" => $finalizeResponse
        ], 500);
    }
} else {
    return response()->json(["message" => "No video file provided"], 400);
}

} taht is the error that shows to me : { "message": "Undefined array key "media"", "exception": "ErrorException", "file": "C:\xampp\htdocs\ThePostFlex\vendor\abraham\twitteroauth\src\TwitterOAuth.php", "line": 358, "trace": [ { "file": "C:\xampp\htdocs\ThePostFlex\vendor\laravel\framework\src\Illuminate\Foundation\Bootstrap\HandleExceptions.php", "line": 256, "function": "handleError", "class": "Illuminate\Foundation\Bootstrap\HandleExceptions", "type": "->" }, { "file": "C:\xampp\htdocs\ThePostFlex\vendor\abraham\twitteroauth\src\TwitterOAuth.php", "line": 358, "function": "Illuminate\Foundation\Bootstrap\{closure}", "class": "Illuminate\Foundation\Bootstrap\HandleExceptions", "type": "->" }, { "file": "C:\xampp\htdocs\ThePostFlex\vendor\abraham\twitteroauth\src\TwitterOAuth.php", "line": 322, "function": "uploadMediaNotChunked", "class": "Abraham\TwitterOAuth\TwitterOAuth", "type": "->" }, { "file": "C:\xampp\htdocs\ThePostFlex\app\Http\Controllers\TwitterController.php", "line": 69, "function": "upload", "class": "Abraham\TwitterOAuth\TwitterOAuth", "type": "->" },

0 likes
0 replies

Please or to participate in this conversation.