jwilsson / spotify-web-api-php

A PHP wrapper for Spotify's Web API.
MIT License
867 stars 156 forks source link

Get next page of playlists response #272

Closed upgrader-dev closed 8 months ago

upgrader-dev commented 8 months ago

Hello,

I'm trying to get all the playlists of a user with the method getMyPlaylists() of SpotifyWebAPI class with a limit of 20 for example. My aim is not to load the entire playlist collection to save Spotify server's usage.

So I plan tu use the 'next' property of the response (which is the uri to request to get the next page results).

I'm a beginner here, but as far as I understand, there is no such method to get the next page of playlists, and I can't use the method sendRequest() of SpotifyWebAPI class because it's protected.

If my understanding is correct maybe i could suggest a new method getNextPlaylistsPage($nextUri).

What do you think about that?

Benjamin

jwilsson commented 8 months ago

Hey Benjamin! In addition to setting the limit you can also set the offset on subsequent requests to skip the first X number of items.

For example:

$limit = 20;
$offset = 0;
$allPlaylists = [];
$next = '';

do {
  $myPlaylists = $api->getMyPlaylists([
    'limit' => $limit,
    'offset' => $offset,
  ]);

  $allPlaylists[] = $myPlaylists;
  $next = $myPlaylists->next;
  $offset += $limit;
} while ($next != null);

print_r($allPlaylists);

Cheers, Jonathan

upgrader-dev commented 8 months ago

Ok thank you