【问题标题】:How to Add a YouTube Video to a Playlist via YouTube official PHP SDK如何通过 YouTube 官方 PHP SDK 将 YouTube 视频添加到播放列表
【发布时间】:2018-02-14 14:52:14
【问题描述】:

我正在使用此页面中的代码 https://developers.google.com/youtube/v3/code_samples/php#upload_a_video 将视频上传到 youtube。但我无法将上传的视频放到一个已经存在的播放列表中。

我发现这个类 Google_Service_YouTube_Resource_PlaylistItemsinsert 方法,但我不知道如何使用它

这是我想出来的

                $resourceId = new \Google_Service_YouTube_ResourceId();
                $resourceId->setVideoId( $status["id"] );
                $resourceId->setKind('youtube#video');



                $playlistItemSnippet = new \Google_Service_YouTube_PlaylistItemSnippet();
                $playlistItemSnippet->setTitle('First video in the test playlist');
                $playlistItemSnippet->setPlaylistId($playlistId);
                $playlistItemSnippet->setResourceId($resourceId);


                $playlistItem = new \Google_Service_YouTube_PlaylistItem();
                $playlistItem->setSnippet($playlistItemSnippet);
                $playlistItemResponse = $youtube->playlistItems->insert(
                    'snippet,contentDetails', $playlistItem, array());

这是回复

object(GuzzleHttp\Psr7\Request)#698 (7) {
  ["method":"GuzzleHttp\Psr7\Request":private]=>
  string(4) "POST"
  ["requestTarget":"GuzzleHttp\Psr7\Request":private]=>
  NULL
  ["uri":"GuzzleHttp\Psr7\Request":private]=>
  object(GuzzleHttp\Psr7\Uri)#749 (7) {
    ["scheme":"GuzzleHttp\Psr7\Uri":private]=>
    string(5) "https"
    ["userInfo":"GuzzleHttp\Psr7\Uri":private]=>
    string(0) ""
    ["host":"GuzzleHttp\Psr7\Uri":private]=>
    string(18) "www.googleapis.com"
    ["port":"GuzzleHttp\Psr7\Uri":private]=>
    NULL
    ["path":"GuzzleHttp\Psr7\Uri":private]=>
    string(25) "/youtube/v3/playlistItems"
    ["query":"GuzzleHttp\Psr7\Uri":private]=>
    string(29) "part=snippet%2CcontentDetails"
    ["fragment":"GuzzleHttp\Psr7\Uri":private]=>
    string(0) ""
  }
  ["headers":"GuzzleHttp\Psr7\Request":private]=>
  array(3) {
    ["Host"]=>
    array(1) {
      [0]=>
      string(18) "www.googleapis.com"
    }
    ["content-type"]=>
    array(1) {
      [0]=>
      string(16) "application/json"
    }
    ["X-Php-Expected-Class"]=>
    array(1) {
      [0]=>
      string(35) "Google_Service_YouTube_PlaylistItem"
    }
  }
  ["headerNames":"GuzzleHttp\Psr7\Request":private]=>
 array(3) {
    ["content-type"]=>
    string(12) "content-type"
    ["host"]=>
    string(4) "Host"
    ["x-php-expected-class"]=>
    string(20) "X-Php-Expected-Class"
  }
  ["protocol":"GuzzleHttp\Psr7\Request":private]=>
  string(3) "1.1"
  ["stream":"GuzzleHttp\Psr7\Request":private]=>
  object(GuzzleHttp\Psr7\Stream)#730 (7) {
    ["stream":"GuzzleHttp\Psr7\Stream":private]=>
    resource(589) of type (stream)
    ["size":"GuzzleHttp\Psr7\Stream":private]=>
    NULL
    ["seekable":"GuzzleHttp\Psr7\Stream":private]=>
    bool(true)
    ["readable":"GuzzleHttp\Psr7\Stream":private]=>
    bool(true)
    ["writable":"GuzzleHttp\Psr7\Stream":private]=>
    bool(true)
    ["uri":"GuzzleHttp\Psr7\Stream":private]=>
    string(10) "php://temp"
    ["customMetadata":"GuzzleHttp\Psr7\Stream":private]=>
    array(0) {
    }
  }
}

注意: 我可以通过 API 上传视频,因此身份验证和连接以及所有基本要求都可以

【问题讨论】:

    标签: youtube google-api youtube-api youtube-data-api google-api-php-client


    【解决方案1】:

    这里是SO post的起点。

    这是解决用户问题的代码。

    $(function() {
        $('input.Chilltrap').on('click', function(e) {
            var chilltrap = this.id;     
            if( $("input[name=" + chilltrap + "]").prop('checked') ) {
                checkboxstatusCt = "Yes";
            }
            else {
                checkboxstatusCt = "No";
            }
            var url = "url.php";
            var params = "c="+checkboxstatusCt+"&s="+chilltrap;
            if (window.XMLHttpRequest) {
                xmlhttp = new XMLHttpRequest();
            } else {
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange = function() {
                if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {                         
                    $("#error").html(xmlhttp.responseText);
                    $("#error"). fadeIn('fast');
                    setTimeout(function(){
                        $("#error"). fadeOut('slow');
                    },2000);
                }
            };
            xmlhttp.open("GET", url+"?"+params,true);
            xmlhttp.send();
        });
    });
    

    您还可以查看下面列出的一些相关 SO 帖子,以了解他们如何在播放列表中上传视频。

    【讨论】:

    • 我在发布问题之前已经阅读了链接,它们与我遇到的问题无关。而且我不知道你发布的代码是做什么的!!
    • @DiaaSaada 你的问题解决了吗?如果是,请发布解决方案,因为我面临同样的问题。
    • 是的。我做到了,通过在设置播放列表之前重新初始化 $client 和 `$service
    猜你喜欢
    • 2018-03-10
    • 1970-01-01
    • 2014-02-09
    • 2015-12-31
    • 1970-01-01
    • 2013-05-17
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    相关资源
    最近更新 更多