retrieve all playlist entry Youtube API V3 using p

2019-03-31 11:51发布

So I have the function that will retrieve all playlist entries from the ZEND Gdata API. Now, I just try to add 'getNextFeed()' but the V3 uses 'pageToken' to display the next entries. The problem I am having is how to retrieve the 'nextPage' on my code and implement it. I know the logic is to get 'nextPageToken' and put it into the loop, but I do not know how. Sorry, I am new to 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条回答
趁早两清
2楼-- · 2019-03-31 12:38

okey last night i try to solving my problem, and got the answer.

here is my code

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

and call the function

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

hope this help someone that have similar problem.

thanks!

查看更多
登录 后发表回答