【问题标题】:youtube api maxResultsyoutube api maxResults
【发布时间】:2014-05-14 22:15:21
【问题描述】:

我试图搜索类似的东西,但没有找到任何相关的东西。 我的问题是我尝试制作的是,当您搜索视频时,它第一次仅显示 8 个视频,然后每次单击“更多视频”时,它都会添加 8 个视频。 猜我在“maxResults”或/和“pageToken”上做错了什么。 如果我在版本 3 中正确理解 API,您只能显示 50 个视频,然后您需要转到另一个页面,有没有办法让它继续向下? 任何帮助表示赞赏。

<?php
$htmlBody = "";

// 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.
$index_playlist = '<iframe width="300" height="180" src="//www.youtube.com/embed/videoseries?list=PLn1aPDOSlmD8vLeP8S2KDogDcmCgtTmHg" frameborder="0" allowfullscreen></iframe>';
if ($_GET['q']) {
$index_playlist = '';

// Call set_include_path() as needed to point to your client library.

require_once 'google_analytics/src/Google_Client.php';
require_once 'google_analytics/src/contrib/Google_YouTubeService.php';

/*
* Set $DEVELOPER_KEY to the "API key" value from the "Access" tab of the
* Google Cloud Console <https://cloud.google.com/console>
* Please ensure that you have enabled the YouTube Data API for your project.
*/
$DEVELOPER_KEY = 'HIDDEN_FOR_THIS_POST';
$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);
    $result = 8;
    $results = $results + $result;
    try {
    // Call the search.list method to retrieve results matching the specified
    // query term.
    if ($_GET['more']) {
        $searchResponse = $youtube->search->listSearch('id,snippet', array(
          'q' => $_GET['q'],
          'pageToken' => $_GET['CAgQAA'],
          'maxResults' => $result,
          'type' => 'video',
          ));
    } else {
        $searchResponse = $youtube->search->listSearch('id,snippet', array(
          'q' => $_GET['q'],
          'maxResults' => $results,
          'type' => 'video',
        ));
    };

//echo $searchResponse['nextPageToken'];
//exit;

    $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':
            $htmlBody .= '<iframe width="300" height="180" src="//www.youtube.com/embed/' . $searchResult['id']['videoId'] . '" frameborder="0" allowfullscreen style="margin-right:30px; margin-bottom:30px"></iframe>';
//          $videos .= sprintf('<li>%s</li>',
//          $videos .= sprintf('<li><a href="%s"</a></li>',
//              $searchResult['snippet']['title'], $searchResult['id']['videoId']);
          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;
*/
        };
    };
//  <ul>$videos</ul>
//  <a href="<ul>$videos</ul>"<a/>
/*    $htmlBody .= <<<END
    <h3>Videos</h3>
    <a href="<ul>$videos</ul>"</a>
    <a href="<ul>$playlists</ul>"</a>

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()));
    };
};
?>

<!doctype html>
<html>
  <head>
    <title>TubeSearch</title>
  </head>
  <body>

  <form method="GET">
      <div>
        TubeSearch:
        <br>
            <input type="search" id="q" name="q" placeholder="Enter Search Term">
        </br>
      </div>
      <input type="submit" value="Search">
    </form>
    <?php if (isset($_GET['q'])) : ?>
    <form method="GET">
    <input type="hidden" name="q" value="<?php echo $_GET['q']; ?>">
    <input type="hidden" name="more" value="<?php echo $_GET['q']; ?>">
    <input type="submit" name="more" value="More Videos">
    </form>
    <?php endif; ?>

    <?=$htmlBody?>
    <?=$index_playlist?>

  </body>
</html>

【问题讨论】:

  • 那么你得到了多少结果?你对maxResultpageToken 的想法应该可行。
  • 嗯,我得到了 8 个结果,但只有 8 个,不管我按“搜索”还是“更多视频”
  • $_GET['CAqQAA'] 究竟来自哪里?
  • 如果您查看带有 $searchResponse 的 if 语句下方,您将看到 echo $searchResponse['nextPageToken']; 它将输出 CAqQAA
  • 好吧,我添加了我的下一条评论作为答案..

标签: php html youtube youtube-api


【解决方案1】:

是的,CAqQAA 是从 Youtube 返回的字符串。您将它放在$_GET 数组中,它不会在其中找到它。对于pageToken,您必须使用$searchResponse['nextPageToken']

你可以把它写成hidden&lt;input type="hidden" name="token" value="&lt;?php echo $searchResponse['nextPageToken']; ?&gt;"/&gt;

如果你能做到的话,那么在你的第二次'pageToken' =&gt; $_GET['token']

【讨论】:

  • 谢谢,但现在它只显示前 8 个视频,然后如果你按“更多视频”,它会显示我不知道是什么,打乱我认为的前 10 个视频
  • 是的,您仍然需要添加maxResults
猜你喜欢
  • 2014-02-25
  • 2020-01-02
  • 2018-11-14
  • 2017-07-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多