我刚刚开始使用Youtube的v3 API而且我被困在某处应该使用分页但不知道该怎么做!
我只能使用nextpagetoken
作为pagetoken
转到下一页,但我无法返回上一页!
if (!empty($_GET['page'])) {
$noPage = $_GET['page'];
$page = ($noPage-1)*20;
$pageToken = $_SESSION["nextToken"];
} else {
$noPage = 1;
$page = 1;
}
$searchResponse = $youtube->search->listSearch('id,snippet', array('q' => $q, 'maxResults' => $maxResults, 'type' => "video", 'pageToken' => $pageToken,));
if ($noPage > 1) {
//echo'<a href="?page='.($noPage-1).'"><input type="submit" value="« Previous Page"/></a> - ';
echo'<a href="'.$vhome.'search.html?q='.querydecode($q).'&page='.$prev.'"><input type="submit" value="« Previous Page"/></a> - ';
}
if ($searchResponse['nextPageToken']) {
//echo '<a href="?page='.($noPage+1).'"><input type="submit" value="Next Page »"/></a>';
echo '<a href="'.$vhome.'search.html?q='.querydecode($q).'&page='.$next.'"><input type="submit" value="Next Page »"/></a>';
}
如何获得下一页和上一页的结果?
答案 0 :(得分:1)
第1页的搜索结果将包含&#34; nextPageToken&#39; ...如果你把它设置为&#39; pageToken&#39;参数,并保持查询中的所有其他参数相同,它将加载页面2.
// loads the first page of results
$searchResponse = $youtube->search->listSearch('id,snippet', array('q' => $q, 'maxResults' => $maxResults, 'type' => "video"));
// this gets page two of results
$newSearchResponse = $youtube->search->listSearch('id,snippet', array('q' => $q, 'maxResults' => $maxResults, 'type' => "video", 'pageToken' => $searchResponse["nextPageToken"]));
第二页还将包含一个&#39; nextPageToken&#39;您可以使用它转到第3页,但第2页也会返回&#39; prevPageToken&#39 ;;如果您设置了&#39; pageToken&#39;该值的参数并重新运行您的请求,您将再次从第1页获得结果。
// loads the third page of results
$newNewSearchResponse = $youtube->search->listSearch('id,snippet', array('q' => $q, 'maxResults' => $maxResults, 'type' => "video",'pageToken' => $newSearchResponse["nextPageToken"]));
// this goes back to page 1 of results
$oldSearchResponse = $youtube->search->listSearch('id,snippet', array('q' => $q, 'maxResults' => $maxResults, 'type' => "video", 'pageToken' => $newSearchResponse["prevPageToken"]));