如何在youtube搜索列表中应用分页..?

时间:2015-04-25 09:00:17

标签: youtube pagination youtube-api

我正在使用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>

1 个答案:

答案 0 :(得分:0)

搜索响应将返回&#34; nextPageToken&#34;并且可能是&#34; prevPageToken&#34;可以与您的下一个请求一起传递(除了更改页面令牌之外,应该与当前请求相同)以获取不同的数据集。你用&#34; pageToken&#34;传递它。参数(不是代码在此处执行的URL路径的一部分)。因此,例如,您可以将搜索请求修改为如下所示:

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

然后,您显示搜索结果,然后显示您的&#34; next&#34;链接应该像这样修改:

 #include<stdio.h>
#include<conio.h>
#include<stdlib.h>

struct Node{
 int data;
 struct Node* next;
};
struct Node* head;  //global variable
int main(){
    head = NULL;  //empty list
    int n, i, x;
    printf("How many numbers would you like to enter");
    scanf("%d", &n);
    for(i=0; i<n; i++){
        printf("Enter the number you want to add to list:");
        scanf("%d", &x);
        Insert(x);
        Print();
    }
}


void Insert(int x){
Node* temp= (Node*)malloc(sizeof(struct Node)); //I get error here
temp->data = x;
//temp->next = NULL; //redundant
//if(head!= NULL){
temp->next = head;  //temp.next will point to null when head is null nd otherwise what head was pointing to
//}
head = temp;
}

void Print(){
    struct Node* temp1 = head;  //we dont want tomodify head so store it in atemp. bariable and then traverse
    while(temp1 != NULL){
         printf(" %d", temp1->data);
         temp1= temp1->next;
    }
    printf("\n");
}