youtube data api v3 php 搜索分页? [英] youtube data api v3 php search pagination?

查看:27
本文介绍了youtube data api v3 php 搜索分页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 youtube api v3 php 搜索...

i am trying with youtube api v3 php search...

我第一次使用这个 api 我是初学者...

first time i'm using this api for this i am beginner...

我有3个问题;

1) 如何在搜索列表下方显示分页号?(每页 50 结果)

1) how can below search list showing pagination numbers? (per page 50 result)

2) 如何在列表中显示视频时长?(3:20 分:秒)

2) how can video duration show in list? (3:20 min:second)

3) viewCount 怎么排序

3) how can order viewCount

if ($_GET['q']) {
  require_once 'src/Google_Client.php';
  require_once 'src/contrib/Google_YoutubeService.php';
  $DEVELOPER_KEY = 'my key';

  $client = new Google_Client();
  $client->setDeveloperKey($DEVELOPER_KEY);
  $youtube = new Google_YoutubeService($client);

  try {
    $searchResponse = $youtube->search->listSearch('id,snippet', array(
      'q' => $_GET['q'],
      'maxResults' => 50,
      'type' => "video",
    ));

  foreach ($searchResponse['items'] as $searchResult) {

    $videos .= '<li style="clear:left"><img src="'.$searchResult['snippet']['thumbnails']['default']['url'].'" style="float:left; margin-right:18px" alt="" /><span style="float:left">'.$searchResult['snippet']['title'].'<br />'.$searchResult['id']['videoId'].'<br />'.$searchResult['snippet']['publishedAt'].'<br />'.$item['contentDetails']['duration'].'</span></li>';
}




$htmlBody .= <<<END
    <ul>$videos</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()));
  }
}

推荐答案

1) 如何在搜索列表下方显示分页号?(每页 50 结果)

您需要编写自己的缓存逻辑来实现此功能,因为对于每个结果,您都会获得两个令牌NextPageToken"和PreviousPageToken",后续查询必须包含该令牌编号才能获取下一页或上一页令牌,如下所示.

You need to write your own cacheing logic to implement this feature because with every result you get two tokens "NextPageToken" and "PreviousPageToken" and subsequent query must contain that token number to get next page or previous page token like below.

因此,当缓存中没有结果时,您应该发送 nextpagetoken 或上一页令牌.

So whenever results are not available at cache then you should send either nextpagetoken or previous page token.

https://www.googleapis.com/youtube/v3/search?key=API_KEY&part=snippet&q=japan&maxResults=10&order=date&pageToken=NEXT_or_PREVIOUS_PAGE_TOKEN

特别是在您需要每页 50 页并且显示 3 个分页(如 (1,2,NEXT))的情况下,您需要获取结果两次.您将保留在缓存中的两个结果,因此第 1 页和第 2 页的结果将从缓存中检索.接下来,您可以通过发送 nextPageToken 来确保再次查询 google.

In particular your case where you need 50 pages per page and you are showing 3 pagination like (1,2,NEXT) then you need to fetch results two times. Both the results you will keep in cache so for page 1 and 2 results will be retrieved from cache. For next you make it sure that you are making query google again by sending nextPageToken.

因此要显示分页 1-n 和每页 50 结果,那么您需要对 google api 进行 n-1 次查询.但是,如果您每页显示 10 个结果,那么您可以对 50 个结果进行单个查询,使用它您可以在检索到的结果的帮助下显示前 5 页 (1-5),然后您应该再次发送如上所示的下一页令牌.

Thus to show pagination 1-n and every page 50 results then you need to make n-1 queries to google api. But if you are showing 10 results per page then you cane make single query of 50 results using which you can show first 5 pages (1-5) with the help of retrieved results and at next you should again send next page token like above.

注意 - Google youtube api 最多提供 50 个结果.

NOTE- Google youtube api provide 50 results max.

2) 如何在列表中显示视频时长?(3 分 20 秒)

Youtube API v3 不会在简单的第一次搜索响应时返回视频时长.要获得视频时长,我们需要像下面这样对 youtube api 进行一次额外调用.

Youtube API v3 do not return video duration at simple first search response. To get video duration we need to make one extra call to youtube api like below.

https://www.googleapis.com/youtube/v3/videos?id=VIDEO_ID1%2CVIDEO_ID2&part=contentDetails&key=API_KEY(最多 50 个 ID)

https://www.googleapis.com/youtube/v3/videos?id=VIDEO_ID1%2CVIDEO_ID2&part=contentDetails&key=API_KEY (max 50 IDs)

此问题在http://code.google.com/p/gdata-issues/issues/detail?id=4294".我也在这里发布了我的答案.

This issue is highlighted in "http://code.google.com/p/gdata-issues/issues/detail?id=4294".I posted my answer here too.

因此,如果我们想显示视频时长,那么我们每次需要进行两次调用.

Hence if we want to display video duration then we need to make two calls every time.

3) 如何订购viewCount

在查询下方触发,它将提供按观看次数排序的结果.

Trigger below query it will provide results ordered by view count.

https://www.googleapis.com/youtube/v3/search?key=KEY&part=snippet&q=japan&maxResults=5&order=viewCount

详情请参考 - https://developers.google.com/youtube/v3/docs/search/list#order

这篇关于youtube data api v3 php 搜索分页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆