2013-05-22 76 views
3

我使用Youtube API上传一些视频,但我无法弄清楚如何将上传的视频添加到特定的播放列表。我已经搜遍了谷歌,我根本没有找到任何帮助。Youtube API(PHP) - 如何将(现有)视频添加到现有播放列表?

我已阅读开发人员指南,我发现这个 - 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(); 
+0

我没有使用YT-API的ZEND Framework包装器的经验,但[this](https://developers.google.com/youtube/2.0/developers_guide_protocol_playlists#Adding_a_video_to_a_playlist)链接显示您需要什么类型的请求。您可以使用PHP cURL来发出请求。 – user1190992

回答

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(); 
} 

而不是在这个例子4XpnKHJAok8,你会想传入新视频的ID,即脚本中的$idv值。

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

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

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

相关问题