【问题标题】:Youtube API (PHP) - how to add (existing) video to existing playlist?Youtube API (PHP) - 如何将(现有)视频添加到现有播放列表?
【发布时间】:2013-05-17 13:02:09
【问题描述】:

我正在使用 Youtube API 上传一些视频,但我不知道如何将上传的视频添加到特定的播放列表。我在谷歌上搜索过,我根本没有找到任何帮助。

我已阅读开发人员指南并找到了这个 - https://developers.google.com/youtube/2.0/developers_guide_php#Adding_a_Playlist_Video,但我不知道如何定义我希望脚本添加到哪个现有播放列表的视频。

这是我现在用来上传视频的:

require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Gdata_YouTube');
Zend_Loader::loadClass('Zend_Gdata_ClientLogin'); 

$developerKey = 'MYDEVKEY';
$applicationId = 'SOMEID';

$authenticationURL= 'https://www.google.com/accounts/ClientLogin';
$httpClient = Zend_Gdata_ClientLogin::getHttpClient(
              $username = 'user',
              $password = 'pass',
              $service = 'youtube',
              $client = null,
              $source = 'something', 
              $loginToken = null,
              $loginCaptcha = null,
              $authenticationURL);  

    $clientId = 'something';

    $yt = new Zend_Gdata_YouTube($httpClient, $applicationId, $clientId, $developerKey);

    $videoName = "video/user_12345.mov";

    $myVideoEntry = new Zend_Gdata_YouTube_VideoEntry();
    $filesource = $yt->newMediaFileSource($videoName);
    $filesource->setContentType('video/quicktime');
    $filesource->setSlug('video/test.mov');
    $myVideoEntry->setMediaSource($filesource);
    $myVideoEntry->setVideoTitle('Video title');
    $myVideoEntry->setVideoDescription('Video description');
    $myVideoEntry->setVideoCategory('Autos');
    $myVideoEntry->SetVideoTags('car');
    $uploadUrl ='https://uploads.gdata.youtube.com/feeds/users/default/uploads';

    $newEntry = $yt->insertEntry($myVideoEntry, $uploadUrl, 'Zend_Gdata_YouTube_VideoEntry');
    $state = $newEntry->getVideoState();
    $idv = $newEntry->getVideoId();

【问题讨论】:

  • 我没有使用 YT-API 的 ZEND 框架包装器的经验,但 this 链接显示了您需要发出什么样的请求。您可以使用 PHP cURL 发出请求。

标签: php api youtube youtube-api


【解决方案1】:

doc you linked to 中的代码为您提供了一个起点:

$postUrl = $playlistToAddTo->getPlaylistVideoFeedUrl();
// video entry to be added
$videoEntryToAdd = $yt->getVideoEntry('4XpnKHJAok8');

// create a new Zend_Gdata_PlaylistListEntry, passing in the underling DOMElement of the VideoEntry
$newPlaylistListEntry = $yt->newPlaylistListEntry($videoEntryToAdd->getDOM());

// post
try {
  $yt->insertEntry($newPlaylistListEntry, $postUrl);
} catch (Zend_App_Exception $e) {
  echo $e->getMessage();
}

在该示例中,您需要传入新视频的 id,即脚本中的 $idv 值,而不是 4XpnKHJAok8

该代码假定您已经有一个 $playlistToAddTo 对象,但您可能会有一个播放列表 ID。你可以修改为阅读

$postUrl = sprintf('https://gdata.youtube.com/feeds/api/playlists/%s?v=2', $playlistId);

其中$playlistId 是您要将视频添加到的播放列表的 ID。

【讨论】:

    猜你喜欢
    • 2015-12-31
    • 1970-01-01
    • 1970-01-01
    • 2018-03-10
    • 2018-02-14
    • 1970-01-01
    • 2011-02-27
    • 1970-01-01
    • 2017-05-10
    相关资源
    最近更新 更多