Dariusz
Dariusz

Reputation: 303

Youtube video list pagination

I'm stuck with this problem of creating a pagination movie list.

Below is my code:

          $API_key    = 'my_api_key';
          $channelID  = 'channel_id';
          $maxResults =  15;

          $videoList = json_decode(file_get_contents('https://www.googleapis.com/youtube/v3/search?order=date&part=snippet&channelId='.$channelID.'&maxResults='.$maxResults.'&key='.$API_key.''));

          foreach($videoList->items as $item){
              if(isset($item->id->videoId)){
                  echo '<div class="youtube-video col-xl-4">
                          <iframe width="100%" height="100%" src="https://www.youtube.com/embed/'.$item->id->videoId.'" frameborder="0" allowfullscreen></iframe>
                          <h3>'. $item->snippet->title .'</h3>
                      </div>';
              }
          }

What am I missing, and what should I do next? I was reading about pageToken but I don't know how to get the value of it to make it working.

Upvotes: 1

Views: 172

Answers (1)

Benjamin Loison
Benjamin Loison

Reputation: 5642

When you get the first result returned when accessing the YouTube Data API v3 endpoint, $videoList may have an entry nextPageToken. If so the nextPageToken value is accessible with $videoList["nextPageToken"]. Then to retrieve the next page of data associated with your request, add to your URL &pageToken={NEXT_PAGE_TOKEN} where {NEXT_PAGE_TOKEN} is the value associated to the entry nextPageToken that you just got on your last request.

Upvotes: 1

Related Questions