Reordering a playlist

I'm actually working with the PHP Spotify API. I'm creating a playlist and I'm adding/deleting tracks depending a list of albums that change every weeks (that's the top album of the week).

The problem comes when I want to reorder the tracks. In fact, I want to keep inside the playlist the tracks that stay in the top album of the week, so I can later know which tracks was the oldest in the top.

But, reordering the playlist with the actual API is pretty much weird. It works really fine when I have to reorder one track, or maybe two tracks. But if there are more tracks to reorder, it just doesn't work.

What I'm technically doing is that I have two arrays, one that represents the top album of the week, and the others that represents the playlist. There are constructed in the same way. The key represents the name of the track and the artist, and the value represents the Spotify URI. I'm just comparing the two array in a loop, and I'm sending a cURL to reorder the playlist depending the equality of the two arrays. But that doesn't work, that will work maybe for the few firsts tracks, but then it all goes wild.

Do you have any idea on how to reorder multiple tracks in a playlist using their API ? How would you do that ? cURLing inside a loop is not the best idea I had but that was the only one

The part of the code where I'm stuck :

while($newTracks !== $oldTracks){

        $oldPosition = array_search($newTracks[$i], $actualTracks);

        $chPut = curl_init();
        $put = array(
            'range_start' => $oldPosition,
            'range_length' => 1,
            'insert_before' => $newTracks[$i++] + 1
        );

        $put = json_encode($put);
        curl_setopt($chPut, CURLOPT_URL, "https://api.spotify.com/v1/users/" . $idUser . "/playlists/" . $idPlaylist . "/tracks");
        curl_setopt($chPut, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($chPut, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($chPut, CURLOPT_CUSTOMREQUEST, "PUT");
        curl_setopt($chPut, CURLOPT_POSTFIELDS, $put);
        $modif = curl_exec($chPut);
        d($modif);
        curl_close($chPut);

        $chGetFinal = curl_init();
        curl_setopt($chGetFinal, CURLOPT_URL, "https://api.spotify.com/v1/users/" . $idUser . "/playlists/" . $idPlaylist . "/tracks");
        curl_setopt($chGetFinal, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($chGetFinal, CURLOPT_HTTPHEADER, $headers);
        $outputFinal = curl_exec($chGetFinal);
        curl_close($chGetFinal);
        $getActualTracks = json_decode($outputFinal);

        $j = 0;
        $oldTracks = array();
        foreach($getActualTracks->items as $track){
            $oldTracks[$j++] = 'spotify:track:' . $track->track->id;
        }
        if ($i > count($newTracks)){
            $i = 0;
        }

    }

The two arrays (oldTracks and newTracks) are like

[0] => spotify:track:4w2iAP3X4FkpPb5kjRdIDx

(The key corresponds to the position, and the value to the Spotify URI Track)

OldTracks is linked to what is actually in the Spotify Playlist, and newTracks is linked to what should be in the Spotify Playlist

After the first cURL (where I reorder a track), the array 'oldTracks' changes, so I'm doing another cURL to get the new version of the Spotify Playlist.

I don't know if I'm being clear, but I know that two cURLs inside a while loop that never ends is not a good idea !

The playlist I'm working on : https://open.spotify.com/user/118698176/playlist/6StRb5g20JPhlsBodz0ahH

链接地址: http://www.djcxy.com/p/55592.html

上一篇: 搜索spotify用户的音乐库的特定流派

下一篇: 重新排序播放列表