php - youtube data api 3 php,如何从一个 channel 获取超过 50 个视频?

标签 php api youtube-api

我正在开展一个项目,该项目需要使用 youtubedata api 3.0 列出 channel 中的所有视频,而不是来自 gdata(feed) 的视频,Api 仅返回来自 channel 的 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> 

最佳答案

是的,真的很晚了,但由于您没有接受答案,所以让我在这里给您解决方案。

假设你点击了一个 url BaseURL

因此,当您点击 api 获取前 50 个视频时,它会为您提供一个 nextPageToken

要获取接下来的五十个视频,您只需将此标记附加到您的 BaseURL,如下所示:

BaseURL + "&pageToken="+nextPageToken

同样,如果您已经完成了一些分页,您也可能会得到一个 prevPageToken,现在您只需像上面解释的那样附加此标记即可转到上一页。

关于php - youtube data api 3 php,如何从一个 channel 获取超过 50 个视频?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21289493/

相关文章:

python - 尝试使用YouTube API每小时检查一次视频观看次数。可以使用Python存储数据还是需要PHP?

php - 当我使用 (if,BEGIN,END) 时,sql 查询不起作用

c++ - 如何覆盖 Py_GetPrefix()、Py_GetPath()?

python - 使用python从ustream api解码json

youtube-api - android中youtube电视配对的API

java - 使用 YouTube API 获取 channel

java - 使用 Java 和 JSP 连接到 Web 数据库

php - Symfony 中的 service.yml 和 config.yml 有什么区别

php - 检查字符串是否包含 float

ios - 如何在 swift 中使用 Gmail api 发送电子邮件