使用pageToken检索所有播放列表条目Youtube API V3

时间:2013-12-22 08:21:39

标签: php youtube-api

所以我有一个函数可以从ZEND Gdata API中检索所有播放列表条目。 现在,我只是尝试添加'getNextFeed()',但V3使用'pageToken'来显示下一个条目。 我遇到的问题是如何检索我的代码上的'nextPage'并实现它。 我知道逻辑是获取'nextPageToken'并将其放入循环中,但我不知道如何。 对不起,我是JSON的新手。

<?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;
  }
}

?>

1 个答案:

答案 0 :(得分:3)

昨晚我试图解决我的问题,得到答案。

这是我的代码

<?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']);

希望这有助于有类似问题的人。

谢谢!