[GH-ISSUE #116] removing tracks at a certain position without passing a track id #67

Closed
opened 2026-02-27 19:25:53 +03:00 by kerem · 5 comments
Owner

Originally created by @kasperkamperman on GitHub (Feb 27, 2018).
Original GitHub issue: https://github.com/jwilsson/spotify-web-api-php/issues/116

I'd like to remove tracks if my playlist is too long. According to the Spotify API this should be doable to only pass a positions array. However I get the idea that this isn't implemented. Although it also could be that my syntax is wrong.

https://developer.spotify.com/web-api/remove-tracks-playlist/
Paragraph: Removing the track at a given position in a specific playlist snapshot

First try:

$tracks = [['positions' => 5]];
echo $api->deleteUserPlaylistTracks($me->id, $playlistId, $tracks);

This try will give the error:
Notice: Undefined index: id in /Applications/MAMP/htdocs/spotifyfm/src/SpotifyWebAPI.php on line 339 JSON body doesn't conform to specification

Second try:

$tracks = [
  ['id' => ''],
  ['positions' => 5]
];

This try will give the error:
Notice: Undefined index: id in /Applications/MAMP/htdocs/spotifyfm/src/SpotifyWebAPI.php on line 339
JSON body contains an invalid track uri: spotify:track:

Originally created by @kasperkamperman on GitHub (Feb 27, 2018). Original GitHub issue: https://github.com/jwilsson/spotify-web-api-php/issues/116 I'd like to remove tracks if my playlist is too long. According to the Spotify API this should be doable to only pass a positions array. However I get the idea that this isn't implemented. Although it also could be that my syntax is wrong. https://developer.spotify.com/web-api/remove-tracks-playlist/ Paragraph: Removing the track at a given position in a specific playlist snapshot First try: ``` $tracks = [['positions' => 5]]; echo $api->deleteUserPlaylistTracks($me->id, $playlistId, $tracks); ``` This try will give the error: Notice: Undefined index: id in /Applications/MAMP/htdocs/spotifyfm/src/SpotifyWebAPI.php on line 339 JSON body doesn't conform to specification Second try: ``` $tracks = [ ['id' => ''], ['positions' => 5] ]; ``` This try will give the error: Notice: Undefined index: id in /Applications/MAMP/htdocs/spotifyfm/src/SpotifyWebAPI.php on line 339 JSON body contains an invalid track uri: spotify:track:
kerem 2026-02-27 19:25:53 +03:00
  • closed this issue
  • added the
    bug
    label
Author
Owner

@jwilsson commented on GitHub (Feb 27, 2018):

Hi!
It looks like we missed that case in deleteUserPlaylistTracks(). Right now, the ID is always required but it should be made optional.

Would you be interested in submitting a PR with a fix?

<!-- gh-comment-id:368846219 --> @jwilsson commented on GitHub (Feb 27, 2018): Hi! It looks like we missed that case in `deleteUserPlaylistTracks()`. Right now, the ID is always required but it should be made optional. Would you be interested in submitting a PR with a fix?
Author
Owner

@kasperkamperman commented on GitHub (Feb 27, 2018):

I'm afraid I don't have enough knowledge of the inner workings to fix it.

I tried testing to comment the "id" lines in the deleteUserPlaylistTracks function, just to test. However I get the error "JSON body contains a track without URI". I also pass the snapshot_id which seems obligatory in this case.

I think this error is thrown by Spotify, so I don't know what is wrong now.

What I notice is that this specific implementation is not mentioned on the beta version of the developer site. So maybe this feature is removed?
https://beta.developer.spotify.com/documentation/web-api/reference/playlists/remove-tracks-playlist/

public function deleteUserPlaylistTracks($userId, $playlistId, $tracks, $snapshotId = '') {
        $options = [];

        if ($snapshotId) {
            $options['snapshot_id'] = $snapshotId;
        }

        $options['tracks'] = array_map(function ($track) {
            $track = (array) $track;

            if (isset($track['positions'])) {
                $track['positions'] = (array) $track['positions'];
            }

            //$track['uri'] = $this->idToUri($track['id'], 'track');
            //unset($track['id']);

            return $track;
        }, $tracks);

<!-- gh-comment-id:368919961 --> @kasperkamperman commented on GitHub (Feb 27, 2018): I'm afraid I don't have enough knowledge of the inner workings to fix it. I tried testing to comment the "id" lines in the deleteUserPlaylistTracks function, just to test. However I get the error "JSON body contains a track without URI". I also pass the snapshot_id which seems obligatory in this case. I think this error is thrown by Spotify, so I don't know what is wrong now. What I notice is that this specific implementation is not mentioned on the beta version of the developer site. So maybe this feature is removed? https://beta.developer.spotify.com/documentation/web-api/reference/playlists/remove-tracks-playlist/ ``` public function deleteUserPlaylistTracks($userId, $playlistId, $tracks, $snapshotId = '') { $options = []; if ($snapshotId) { $options['snapshot_id'] = $snapshotId; } $options['tracks'] = array_map(function ($track) { $track = (array) $track; if (isset($track['positions'])) { $track['positions'] = (array) $track['positions']; } //$track['uri'] = $this->idToUri($track['id'], 'track'); //unset($track['id']); return $track; }, $tracks); ```
Author
Owner

@jwilsson commented on GitHub (Feb 28, 2018):

@kasperkamperman No worries! I'll take a look at it, unless someone else beats me to it.

<!-- gh-comment-id:369140783 --> @jwilsson commented on GitHub (Feb 28, 2018): @kasperkamperman No worries! I'll take a look at it, unless someone else beats me to it.
Author
Owner

@kasperkamperman commented on GitHub (Mar 1, 2018):

Thanks for the fix!

<!-- gh-comment-id:369707926 --> @kasperkamperman commented on GitHub (Mar 1, 2018): Thanks for the fix!
Author
Owner

@jwilsson commented on GitHub (Mar 1, 2018):

@kasperkamperman A fix for this has been released in 2.2.0. Something like this should work:

$tracks = [
  'positions' => [1, 2, 3],
];

$api->deleteUserPlaylistTracks($me->id, $playlistId, $tracks, $snapshotId);

Due note that the $snapshotId is required, but you can get it from a call to getUserPlaylist().

<!-- gh-comment-id:369708214 --> @jwilsson commented on GitHub (Mar 1, 2018): @kasperkamperman A fix for this has been released in `2.2.0`. Something like this should work: ```php $tracks = [ 'positions' => [1, 2, 3], ]; $api->deleteUserPlaylistTracks($me->id, $playlistId, $tracks, $snapshotId); ``` Due note that the `$snapshotId` is required, but you can get it from a call to `getUserPlaylist()`.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/spotify-web-api-php#67
No description provided.