【问题标题】:How to apply pagination in youtube search list..?如何在 youtube 搜索列表中应用分页..?
【发布时间】:2015-04-26 14:18:29
【问题描述】:

我正在开发 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>

【问题讨论】:

    标签: youtube pagination youtube-api


    【解决方案1】:

    搜索响应将返回一个“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>
    

    【讨论】:

      猜你喜欢
      • 2014-11-15
      • 2021-06-25
      • 1970-01-01
      • 2011-09-14
      • 2014-04-10
      • 1970-01-01
      • 2016-09-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多