2012-02-20 48 views
3

我使用發現@http://code.google.com/apis/youtube/2.0/developers_guide_php.html測試代碼來創建播放列表:的Youtube API列表ID

$newPlaylist = $yt->newPlaylistListEntry(); 
$newPlaylist->summary = $yt->newDescription()->setText($desc); 
$newPlaylist->title = $yt->newTitle()->setText($title); 
// post the new playlist 
$postLocation = 'http://gdata.youtube.com/feeds/api/users/default/playlists'; 
try { 
    $playlist = $yt->insertEntry($newPlaylist, $postLocation); 
    } 
catch (Zend_Gdata_App_Exception $e) { 
    echo $e->getMessage(); 
} 

播放列表創建的,但我怎樣讓這個剛創建的播放列表的ID或網址?

回答

2

我有同樣的問題。我設法得到了一點進一步,但我仍然無法獲得播放列表ID。下面是我做的:

代替:

$playlist = $yt->insertEntry($newPlaylist, $postLocation); 

我用:

$playlist = $yt->insertEntry($newPlaylist, $postLocation, 'Zend_Gdata_YouTube_PlaylistListEntry'); 

但是當我試圖通過$playlist->getPlaylistID()$playlist->playlistId->text我得到它說同樣的異常拿到ID:

yt:playlistId元素在2之前的版本中不受支持。

即使我有$yt->setMajorProtocolVersion(2);

0

這是一個徹頭徹尾的黑客先前設置它,我有完全相同的問題,所以我走進Zend的在/ GDATA/YouTube的/ PlaylistListEntry.php類在線229我註釋了if else語句。

/** 
* Returns the Id relating to the playlist. 
* 
* @throws Zend_Gdata_App_VersionException 
* @return Zend_Gdata_YouTube_Extension_PlaylistId The id of this playlist. 
*/ 
public function getPlaylistId() 
{ 
    /*if (($this->getMajorProtocolVersion() == null) || 
     ($this->getMajorProtocolVersion() == 1)) { 
     require_once 'Zend/Gdata/App/VersionException.php'; 
     throw new Zend_Gdata_App_VersionException('The yt:playlistId ' . 
      'element is not supported in versions earlier than 2.'); 
    } else {*/ 
     return $this->_playlistId; 
    //} 
} 

我希望有人來告訴我們如何解決這個問題的正確方法,但是這使得它如此

function printPlaylistListEntry($playlistListEntry, $showPlaylistContents = false) 
    { 

     $this->yt->setMajorProtocolVersion(2); 
     echo '<br>Title: ' . $playlistListEntry->title->text . "\n"; 
     echo '<br>Description: ' . $playlistListEntry->description->text . "\n"; 
     echo '<br>playlistId: ' . $playlistListEntry->playlistId->text . "\n"; 

...(在YouTube V2 PHP API)。

將返回playlistid。

標題:therighttitle

說明:therightdescription

playlistId:therightplaylistId

編輯:我覺得這可能是一個更好的解決方案:

if ($this->getMajorProtocolVersion() < 2) { 
     require_once 'Zend/Gdata/App/VersionException.php'; 
     throw new Zend_Gdata_App_VersionException('The yt:playlistId ' . 
      'element is not supported in versions earlier than 2.'); 
    } else { 

     return $this->_playlistId; 
    } 

用此代替getPlaylistId()函數,遵循前面的getDescription函數的邏輯,以及它的較少哈希。再次完全公開批評爲什麼這是或不是來自zend人的好主意。

0

你不需要任何特殊的黑客來完成這項工作。您只需顯式設置$ playlist變量的協議版本以及$ yt變量。正如你所說,設置較早$ YT主要協議版本:

$yt->setMajorProtocolVersion(2); 

然後初始化$播放列表後,設置在該協議還有:

$playlist = $yt->insertEntry($newPlaylist, $postLocation, 'Zend_Gdata_YouTube_PlaylistListEntry'); 
$playlist->setMajorProtocolVersion(2); 

一旦你這樣做,你應該能夠讓您的播放列表ID沒有問題:)

$playlist_id = $playlist->getPlaylistID();