如何通过YouTube官方PHP SDK将YouTube视频添加到播放列表

时间:2017-09-06 07:55:54

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

我正在使用此页面中的代码 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上传视频,因此身份验证和连接以及所有基本要求都可以

1 个答案:

答案 0 :(得分:0)

以下是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帖子,了解他们如何在播放列表中上传视频。