2015-05-15 98 views
1

我剛開始使用Youtube的v3 API,我被困在某個地方應該使用分頁,但不知道該怎麼做!如何使用YouTube的API V3分頁功能?

我只能使用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="&laquo; Previous Page"/></a> - '; 
    echo'<a href="'.$vhome.'search.html?q='.querydecode($q).'&amp;page='.$prev.'"><input type="submit" value="&laquo; Previous Page"/></a> - '; 
} 
if ($searchResponse['nextPageToken']) { 
    //echo '<a href="?page='.($noPage+1).'"><input type="submit" value="Next Page &raquo;"/></a>'; 
    echo '<a href="'.$vhome.'search.html?q='.querydecode($q).'&amp;page='.$next.'"><input type="submit" value="Next Page &raquo;"/></a>'; 
} 

如何獲得next和prev頁面的結果?

回答

1

頁面1的搜索結果將包含一個「nextPageToken」...如果您將其設置爲'pageToken'參數,並且使查詢中的所有其他參數保持不變,則將加載第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"])); 

第二頁也將包含「nextPageToken這個」你可以用它去3頁,但第2頁也返回了「prevPageToken」;如果你設置了「pageToken」參數該值並重新運行請求,您將再次從第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"])); 
相關問題