Change the subtitles and descriptions of videos on my YouTube channel XY using the API service

WPTurbo » Snippets » Change the subtitles and descriptions of videos on my YouTube channel XY using the API service
0

Created with:

Visibility: 

public

Creator: Jan

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project
					<?php

namespace WPGriffino;


class YouTubeAPI
{

    private $api_key;
    private $channel_id;

    public function __construct( string $api_key, string $channel_id )
    {
        $this->api_key = $api_key;
        $this->channel_id = $channel_id;
    }

    public function updateVideoDescription( string $video_id, string $new_description ): bool
    {
        $url = "https://www.googleapis.com/youtube/v3/videos?key={$this->api_key}&part=snippet";

        $data = [
            "id" => $video_id,
            "snippet" => [
                "description" => $new_description,
            ],
        ];

        $response = wp_remote_request( $url, [
            "method"    => "PUT",
            "body"      => json_encode( $data ),
            "headers"   => [
                "Content-Type" => "application/json",
            ],
        ] );

        if ( is_wp_error( $response ) )
        {
            return false; // Handle error appropriately
        }

        return true; // Success
    }

    public function updateVideoCaptions( string $video_id, string $caption_file_path ): bool
    {
        // Upload a new caption track
        $url = "https://www.googleapis.com/upload/youtube/v3/captions?key={$this->api_key}";

        $data = [
            "snippet" => [
                "videoId" => $video_id,
                "language" => "en", // Specify the language code
                "name" => "New Captions",
                "isDraft" => false,
            ],
        ];

        $response = wp_remote_request( $url, [
            "method"    => "POST",
            "body"      => json_encode( $data ),
            "headers"   => [
                "Content-Type" => "application/json",
                "X-Upload-Content-Type" => "text/plain", // Adjust if your file type is different
            ],
            "body" => file_get_contents( $caption_file_path ),
        ] );

        if ( is_wp_error( $response ) )
        {
            return false; // Handle error appropriately
        }

        return true; // Success
    }

}
				

In this updated code, I added two methods to the YouTubeAPI class: updateVideoDescription and updateVideoCaptions.

updateVideoDescription: This method updates the description of a video. It constructs a request to the YouTube Data API's video endpoint, where it sets the new description for the specified video ID.

updateVideoCaptions: This method is designed to upload a new caption track for a video. It constructs a request to the YouTube Captions API. The request includes a snippet specifying the video ID, language, and name for the caption track. The actual caption content is read from a file specified by $caption_file_path. The content type is set to text/plain for plain text caption files; ensure that the file path provided points to a valid caption file.

Make sure to handle the API key securely and ensure that your application has the necessary permissions to update video descriptions and captions.

Register an account to save your snippets or go Pro to get more features.