2013-12-22 161 views
3

因此,我有一個函數可以從ZEND Gdata API檢索所有播放列表條目。 現在,我只是嘗試添加'getNextFeed()',但V3使用'pageToken'來顯示下一個條目。 我遇到的問題是如何檢索我的代碼上的'nextPage'並實現它。 我知道邏輯是得到'nextPageToken'並將其放入循環中,但我不知道如何。 對不起,我是JSON新手。使用pageToken檢索所有播放列表條目Youtube API V3

<?php 
$client = new Google_Client(); 
    $client->setDeveloperKey($DEVELOPER_KEY); 

    $youtube = new Google_YoutubeService($client); 

    try { 
    $searchResponse = $youtube->playlistItems->listPlaylistItems('id,snippet', array(
     'playlistId' => $_GET['q'], 
     'maxResults' => $_GET['maxResults'] 
    )); 

    foreach ($searchResponse['items'] as $searchResult) { 
      $videoId = $searchResult['snippet']['resourceId']['videoId']; 
      $videoTitle = $searchResult['snippet']['title']; 
      $videoThumb = $searchResult['snippet']['thumbnails']['high']['url']; 
      $videoDesc = $searchResult['snippet']['description']; 
      print '<div>'. 
         $videoTitle.'<br/><br/><img src="'. 
         $videoThumb.'" /><br/>'. 
         $videoId.'<br/>'. 
         $videoDesc.'<br/>'. 
         '</div><br/><br/>'; 
    } 
    } catch (Google_ServiceException $e) { 
    return; 
    } catch (Google_Exception $e) { 
    return; 
    } 
} 

?> 

回答

3

okey昨晚我試着解決我的問題,並得到了答案。

這裏是我的代碼

<?php 
function youtube_search($query, $max_results, $next_page_token=''){ 

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

     $params = array(
      'playlistId'=>$query, 
      'maxResults'=>$max_results, 
     ); 

      // if next_page_token exist add 'pageToken' to $params 
     if(!empty($next_page_token)){ 
      $params['pageToken'] = $next_page_token; 
     } 

      // than first loop 
     $searchResponse = $youtube->playlistItems->listPlaylistItems('id,snippet,contentDetails', $params); 
     foreach ($searchResponse['items'] as $searchResult) { 
     $videoId = $searchResult['snippet']['resourceId']['videoId']; 
     $videoTitle = $searchResult['snippet']['title']; 
     $videoThumb = $searchResult['snippet']['thumbnails']['high']['url']; 
     $videoDesc = $searchResult['snippet']['description']; 
     print '<div>'. 
        $videoTitle.'<br/><br/><img src="'. 
        $videoThumb.'" /><br/>'. 
        $videoId.'<br/>'. 
        $videoDesc.'<br/>'. 
        '</div><br/><br/>'; 
     } 

      // checking if nextPageToken exist than return our function and 
      // insert $next_page_token with value inside nextPageToken 
     if(isset($searchResponse['nextPageToken'])){ 
       // return to our function and loop again 
      return youtube_search($query, $max_results, $searchResponse['nextPageToken']); 
     } 
    } 
?> 

並調用該函數

youtube_search($_GET['q'],$_GET['maxResults']); 

希望這有助於有人有類似的問題。

謝謝!

相關問題