gok*_*han 6 youtube youtube-api youtube-data-api
我正在尝试使用youtube api v3 php搜索...
我第一次使用这个API我是初学者...
我有3个问题;
1)如何在搜索列表下面显示分页编号?(每页50条结果)
2)视频持续时间如何显示在列表中?(3分20秒:秒)
3)如何订购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()));
}
}
Run Code Online (Sandbox Code Playgroud)
vir*_*rus 18
1)如何在搜索列表下面显示分页编号?(每页50条结果)
您需要编写自己的缓存逻辑来实现此功能,因为每个结果都会获得两个标记"NextPageToken"和"PreviousPageToken",后续查询必须包含该标记编号才能获得下一页或上一页标记,如下所示.
因此,只要缓存中没有结果,您就应该发送nextpagetoken或previous page token.
特别是在你每页需要50页并且你显示3分页(例如1,2,NEXT)的情况下,你需要两次获取结果.您将保留在缓存中的结果,以便从缓存中检索第1页和第2页结果.对于下一步,您确保通过发送nextPageToken再次查询谷歌.
因此,要显示分页1-n和每页50结果,那么您需要对谷歌api进行n-1次查询.但是,如果您每页显示10个结果,那么您可以单独查询50个结果,使用这些结果可以在检索到的结果的帮助下显示前5页(1-5),然后您应该再次发送下一页令牌,如上所述.
注意 - Google youtube api最多提供50个结果.
2)视频持续时间如何显示在列表中?(3分20秒:秒)
Youtube API v3不会在简单的第一次搜索响应时返回视频持续时间.要获得视频时长,我们需要再向youtube api拨打一次,如下所示.
此问题在" http://code.google.com/p/gdata-issues/issues/detail?id=4294 "中突出显示.我也在此处发布了我的回答.
因此,如果我们想要显示视频持续时间,那么我们每次都需要进行两次调用.
3)如何订购viewCount
触发下面的查询它将提供按视图计数排序的结果.
有关详细信息,请参阅此处 - https://developers.google.com/youtube/v3/docs/search/list#order
| 归档时间: |
|
| 查看次数: |
11070 次 |
| 最近记录: |