使用php,如何使用youtube v3 api在我的网站上传YouTube视频?

时间:2017-10-27 10:50:17

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

我正在使用此网站,用户使用youtube链接注册和上传视频。 我不想使用Youtube频道。 我是新手编程的PHP,我不知道如何去做这个。 用户上传视频后,该页面还必须包含网站上视频的类别列表。

任何帮助将不胜感激

编辑:这是我到目前为止所得到的,我不知道我是怎么来到这里的,我所知道的是它不起作用。 它有一个模态,允许用户输入视频链接并将其导入到将显示所有视频的页面。

<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>test 1</title>
    <link rel="stylesheet" href="assets/css/bootstrap.min.css">
    <link rel="stylesheet" href="assets/css/bootstrap-theme.min.css">
    <link rel="stylesheet" href="assets/css/font-awesome.min.css">
    <link rel="stylesheet" href="assets/css/style.css">
    <script src="assets/js/jquery.min.js"></script>
    <script src="assets/js/bootstrap.min.js"></script>
    </head>
<body>
<div class="col-md-8">
<div class="video-embed-iframe">
<div class="embed-responsive embed-responsive-16by9">
<iframe src="http://www.youtube.com/embed/{$videos_youtube_id}?autoplay={$player_autoplay}&showinfo={$player_showinfo}&rel={$player_related}&controls={$player_controls}&loop={$player_loop}&color={$player_color}" allowfullscreen></iframe>
</div>
</div>



<div class="modal fade" id="upload-modal">
<div class="modal-dialog modal-lg">
    <div class="modal-content">
        <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h4 class="modal-title"><span class="fa fa-upload"></span> {$lang_upload_new_videos}</h4>
    </div>
    <div class="modal-body">
        <div id="upload-message"></div>
        <form id="upload-form" method="POST" action="">
        <div class="row">
          <div class="col-md-12">
          <div class="form-group">              
            <label for="category">{$lang_category} <span>*</span> </label>
            <select class="form-control" name="category" id="category">
                {section name=x loop=$categories}
                    <option value="{$categories[x].id}">{$categories[x].category}</option>
                {/section}
            </select>
          </div>
          <div class="form-group">
            <label for="link">{$lang_videos_link} <span>*</span> </label>
            <input type="text" class="form-control" name="link" dir="ltr" id="link" placeholder="https://www.youtube.com/watch?v=uJcB3ZaaLlA" />
            <p class="help-block" dir="ltr">youtube video link</p>
          </div>

          </div>
        </div>
        <div class="modal-footer">
            <button type="button" id="import_btn" class="btn btn-inverse pull-left">{$lang_import}</button>
        </div>
        </form>
</div>
</div>
</div>

</body>
</html>

1 个答案:

答案 0 :(得分:3)

既然你说你是PHP的新手,我建议你在这个问题上多挖一点。

但这是一个有效的代码,希望对你有所帮助。

if($client->getAccessToken()) {
$snippet = new Google_VideoSnippet();
$snippet->setTitle("Test title");
$snippet->setDescription("Test descrition");
$snippet->setTags(array("tag1","tag2"));
$snippet->setCategoryId("22");

$status = new Google_VideoStatus();
$status->privacyStatus = "private";

$video = new Google_Video();
$video->setSnippet($snippet);
$video->setStatus($status);

$error = true;
$i = 0;

try {
    $obj = $youTubeService->videos->insert("status,snippet", $video,
                                     array("data"=>file_get_contents("video.mp4"), 
                                    "mimeType" => "video/mp4"));
} catch(Google_ServiceException $e) {
    print "Caught Google service Exception ".$e->getCode(). " message is ".$e->getMessage(). " <br>";
    print "Stack trace is ".$e->getTraceAsString();
}

}