2013-12-15 154 views
0

我想了解遞歸:)那麼,具體是使用Google的PHP客戶端庫獲取完整的YouTube播放列表。YouTube API:獲取完整播放列表

這是我的課程中名爲YTFunctions的函數。它首先被一個有效的,經過驗證的YouTube_Service對象和一個playlistId調用。然後,只要PlaylistItems響應具有nextPageToken,然後將其附加到應包含播放列表中包含的所有對象(視頻)的輸出,理論上應該一遍又一遍地調用自己。出於某種原因,它只是返回一個空數組。

public static function getFullPlaylistByID($youtube, $playlistId, $pageToken) { 
    $params = array(
     'maxResults' => 50, 
     'playlistId' => $playlistId, 
    ); 
    if ($pageToken !== false) { 
     $params['pageToken'] = $pageToken; 
    } 
    $playlistResponse = $youtube->playlistItems->listPlaylistItems('snippet,contentDetails', $params); 
    error_log(count($playlistResponse['items'])); 
    $playlistItems = $playlistResponse['items']; 
    if (isset($playlistResponse['nextPageToken'])) { 
     $playlistItems = array_merge($playlistItems, YTFunctions::getFullPlaylistByID($youtube,$playlistId, $playlistResponse['nextPageToken'])); 
    } else { 
    } 
    return $playlistItems; 
} 

我很明顯缺少這裏的東西,任何幫助將不勝感激。

托比亞斯Timpe

回答

1

這裏我的類,將得到所有playlistItems

<?php 
class yt{ 
    public $DEVELOPER_KEY = '{DEVELOPER_KEY}'; 
    public $client; 
    public $youtube; 

    public function __construct(){ 
     $this->client = new Google_Client(); 
     $this->client->setDeveloperKey($this->DEVELOPER_KEY); 
     $this->youtube = new Google_YoutubeService($this->client);  
    } 

    public function getPlaylistItems($playlist, $maxResults, $nextPageToken = ''){ 
     $params = array(
      'playlistId' => $playlist, 
      'maxResults' => $maxResults, 
     ); 

     // if $nextPageToken exist 
     if(!empty($nextPageToken)){ 
      //insert $nextPageToken value into $params['pageToken'] 
      $params['pageToken'] = $nextPageToken; 
     } 

     $youtubeFeed = $this->youtube->playlistItems->listPlaylistItems('id,snippet,contentDetails', $params); 
     // function for looping our feed and entry 
     $this->setFeed($youtubeFeed); 

     //check if nextPageToken are exist 
     if(!empty($youtubeFeed['nextPageToken'])){ 
      $insert = $this->getPlaylistItems($playlist, $maxResults, $youtubeFeed['nextPageToken']); 
      // return to function if nextPageToken are exist 
      return $insert; 
     } 

    } 
} 

$yt = new yt(); 

?> 

,比使用我們的類

$yt->getPlaylistItems('PL0823049820348','25'); 
相關問題