2014-01-22 28 views
7

我正在使用youtubedata api 3.0而不是來自gdata(feed)的要求列出來自頻道的所有視頻的一個項目,Api僅返回來自頻道的50個視頻,並且沒有提及在開發者處獲得更多視頻。谷歌。 幫助。 這是我的代碼youtube data api 3 php,如何從頻道獲得50多個視頻?

<?php 

require_once 'contrib/Google_YoutubeService.php';*/ 
require_once '../upload/google-api-php-client/src/Google_Client.php'; 
    require_once '../upload/google-api-php-client/src/contrib/Google_YouTubeService.php'; 
session_start(); 

/* You can acquire an OAuth 2 ID/secret pair from the API Access tab on the Google APIs Console 
    <http://code.google.com/apis/console#access> 
For more information about using OAuth2 to access Google APIs, please visit: 
    <https://developers.google.com/accounts/docs/OAuth2> 
Please ensure that you have enabled the YouTube Data API for your project. */ 
$OAUTH2_CLIENT_ID = 'sadsadsadasdsad'; 
$OAUTH2_CLIENT_SECRET = 'sadsadsadasdasdasdasd'; 

$client = new Google_Client(); 
$client->setClientId($OAUTH2_CLIENT_ID); 
$client->setClientSecret($OAUTH2_CLIENT_SECRET); 
$redirect = filter_var('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'], 
    FILTER_SANITIZE_URL); 
$client->setRedirectUri($redirect); 

$youtube = new Google_YoutubeService($client); 

if (isset($_GET['code'])) { 
    if (strval($_SESSION['state']) !== strval($_GET['state'])) { 
    die('The session state did not match.'); 
    } 

    $client->authenticate(); 
    $_SESSION['token'] = $client->getAccessToken(); 
    header('Location: ' . $redirect); 
} 

if (isset($_SESSION['token'])) { 
    $client->setAccessToken($_SESSION['token']); 
} 

if ($client->getAccessToken()) { 
    try { 
    $channelsResponse = $youtube->channels->listChannels('contentDetails', array(
     'mine' => 'true', 
    )); 

    $htmlBody = ''; 
    foreach ($channelsResponse['items'] as $channel) { 
     $uploadsListId = $channel['contentDetails']['relatedPlaylists']['uploads']; 
$pageToken=1; 
    while($pageToken<=25){ 
     $playlistItemsResponse = $youtube->playlistItems->listPlaylistItems('snippet', array(
     'playlistId' => $uploadsListId, 
     'maxResults' => 50, 

     )); 


     $htmlBody .= "<h3>Videos in list $uploadsListId</h3><ul>"; 
     foreach ($playlistItemsResponse['items'] as $playlistItem) { 
     $htmlBody .= sprintf('<li>%s (%s)</li>', $playlistItem['snippet']['title'], 
      $playlistItem['snippet']['resourceId']['videoId']); 
     } 
     $htmlBody .= '</ul>'; 
    } 
    } 
    } catch (Google_ServiceException $e) { 
    $htmlBody .= sprintf('<p>A service error occurred: <code>%s</code></p>', 
     htmlspecialchars($e->getMessage())); 
    } catch (Google_Exception $e) { 
    $htmlBody .= sprintf('<p>An client error occurred: <code>%s</code></p>', 
     htmlspecialchars($e->getMessage())); 
    } 

    $_SESSION['token'] = $client->getAccessToken(); 
} else { 
    $state = mt_rand(); 
    $client->setState($state); 
    $_SESSION['state'] = $state; 

    $authUrl = $client->createAuthUrl(); 
    $htmlBody = <<<END 
    <h3>Authorization Required</h3> 
    <p>You need to <a href="$authUrl">authorize access</a> before proceeding.<p> 
END; 
} 
?> 

<!doctype html> 
<html> 
    <head> 
    <title>YouTube Search</title> 
    </head> 
    <body> 
    <?=$htmlBody?> 
    </body> 
</html> 

回答

6

YEPP它真的晚,但因爲你沒有接受的答案,讓我給你這裏的解決方案。

說你打一個網址BaseURL

所以,當你打的API來獲取前50級的影片,它給你一個nextPageToken

爲了得到未來五點的視頻,你只需要此令牌追加到您BaseURL,這樣的事情:

BaseURL + "&pageToken=" +nextPageToken

同樣,如果你已經做了一些分頁,你還可以得到一個​​,現在你只需像上面解釋的那樣添加這個標記來到前一頁。

相關問題