如何在YouTube搜索列表中应用分页



我正在开发YouTube API,我想应用分页但我不知道该怎么做我在谷歌上搜索,只得到我必须获得页面令牌的信息但我不知道如何获取页面令牌请帮我解决这个问题谢谢。

    <?php
$htmlBody = <<<END
<form method="GET">
  <div>
    Search Term: <input type="search" id="q" name="q" placeholder="Enter Search Term">
  </div>
  <div>
    Max Results: <input type="number" id="maxResults" name="maxResults" min="1" max="50" step="1" value="25">
  </div>
  <input type="submit" value="Search">
</form>
END;
// This code will execute if the user entered a search query in the form
// and submitted the form. Otherwise, the page displays the form above.
if ($_GET['q'] && $_GET['maxResults']) {
  // Call set_include_path() as needed to point to your client library.
  require_once ($_SERVER["DOCUMENT_ROOT"].'/youtube/google-api-php-client/src/Google_Client.php');
  require_once ($_SERVER["DOCUMENT_ROOT"].'/youtube/google-api-php-client/src/contrib/Google_YouTubeService.php');
  /*
   * Set $DEVELOPER_KEY to the "API key" value from the "Access" tab of the
   * Google Developers Console <https://console.developers.google.com/>
   * Please ensure that you have enabled the YouTube Data API for your project.
   */
  $DEVELOPER_KEY = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
  $client = new Google_Client();
  $client->setDeveloperKey($DEVELOPER_KEY);
  // Define an object that will be used to make all API requests.
  $youtube = new Google_YoutubeService($client);
  try {
    // Call the search.list method to retrieve results matching the specified
    // query term.
    $searchResponse = $youtube->search->listSearch('id,snippet', array(
      'q' => $_GET['q'],
      'maxResults' => '10',
    ));
    $videos = '';
    $channels = '';
    $playlists = '';
    // Add each result to the appropriate list, and then display the lists of
    // matching videos, channels, and playlists.
    foreach ($searchResponse['items'] as $searchResult) {
      switch ($searchResult['id']['kind']) {
        case 'youtube#video':
          $videos .= sprintf('<a href=new/yt.php?videoid='.$searchResult['id']['videoId'].' target=_blank><li>%s (%s)</li>', $searchResult['snippet']['title'],
            $searchResult['id']['videoId']."   Watch This Video<br><img  src=' http://img.ytapi.com/vi/".$searchResult['id']['videoId']."/mqdefault.jpg' /></a>");
          break;
        case 'youtube#channel':
          $channels .= sprintf('<li>%s (%s)</li>',
              $searchResult['snippet']['title'], $searchResult['id']['channelId']);
          break;
        case 'youtube#playlist':
          $playlists .= sprintf('<li>%s (%s)</li>',
              $searchResult['snippet']['title'], $searchResult['id']['playlistId']);
          break;
      }
    }
    $htmlBody .= <<<END
    <h3>Videos</h3>
    <ul>$videos</ul>
    <h3>Channels</h3>
    <ul>$channels</ul>
    <h3>Playlists</h3>
    <ul>$playlists</ul>
END;
  } 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()));
  }
  ?>
      <h1><a href="<?php echo $_SERVER['REQUEST_URI'] ?>/<?php echo $searchResponse['nextPageToken'] ?>">next</a></h1>
<?php
}
?>
<!doctype html>
<html>
  <head>
    <title>YouTube Search</title>
  </head>
  <body>
    <?=$htmlBody?>
  </body>
</html>

搜索响应将返回一个"nextPageToken",可能还有一个"prevPageToken",可以与您的下一个请求一起传递(除了更改页面令牌之外,它应该与当前请求相同)以获取不同的数据集。您可以使用"pageToken"参数传递它(而不是像此处的代码那样作为URL路径的一部分)。因此,例如,您可以将搜索请求修改为如下所示:

$searchResponse = $youtube->search->listSearch('id,snippet', array(
  'q' => $_GET['q'],
  'maxResults' => '10',
  'pageToken' => $_GET['pageToken']
));

然后,您显示搜索结果,并在下面修改您的"下一个"链接,如下所示:

<h1><a href="<?php echo $_SERVER['REQUEST_URI'] ?>?q=<?php echo $_GET['q']>&pageToken=<?php echo $searchResponse['nextPageToken'] ?>">next</a></h1>

相关内容

  • 没有找到相关文章

最新更新