doujinge9648 2014-04-03 08:15
浏览 41
已采纳

youtube api maxResults

I tried to search for something similar but haven't found anything related. My problem is that I try to make is so when you search for a video it displays the first time only 8 videos, then each time you click "More Videos" it adds 8 more videos. Guessing I have done something wrong with "maxResults" or/and "pageToken". If I understand the API correctly in version 3 you can only display 50 videos and then you need to go to another page, is there some way to make it continue downwards instead? Any help is appreciated.

<?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>
  • 写回答

1条回答 默认 最新

  • dongyu9263 2014-04-03 08:58
    关注

    Yes, CAqQAA is a string returned from Youtube. You are putting it in the $_GET array, where it won't find it. You have to use $searchResponse['nextPageToken'] for the pageToken.

    You could put it in the form as a hidden value <input type="hidden" name="token" value="<?php echo $searchResponse['nextPageToken']; ?>"/>

    Then in your second if you can do 'pageToken' => $_GET['token']

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等