2013-05-22 110 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。

相關問題