How to create SoundCloud playlist using PHP wrapper

谁说胖子不能爱 提交于 2019-12-01 13:02:31

Thanks for using my library.

playlists at soundcloud are tricky. lately I've updated the library and tested what you need in the latest version.

UPDATE 28-04-2015: you'll need the latest 2.2.1 stable version or higher to use the below example code.

Note: version 3.x.x is still under heavy development, example below is for 2.x.x

Note: take in considerations that the Request object was changed in the way the requests are made to soundcloud further tests are needed before i recommend to use that version in production. (also that's why I still didn't tag it as stable version)

With this in mind, do this:

1) Download the latest MASTER 2.2.1 tag, or higher (If you installed it via composer update you composer.json to "njasm/soundcloud": "dev-master" "njasm/soundcloud": "2.2.1" and run composer update in a development environment.) You'll need any version above 2.2.0 stable but not 3.0.x since it's not stable yet.

after that try this code:

// initialize Soundcloud
// auth via User Credential Flow
$soundcloud = new \Njasm\Soundcloud\SoundcloudFacade(
    $clientID, $clientSecret
);

$soundcloud->userCredentials("user@email.com", "user_password");

// build playlist main array
$playlistData = array(
    "playlist" => array(
        "title" => "My2 great Title Playlist!",
        "sharing" => "public", // or private
    )
);

//create playlist at soundcloud and grab the response
$response = $soundcloud->post('/playlists', $playlistData)->request();

// build tracks array
$tracks = array(
    "playlist" => array(
        "tracks" => array(
            array("id" => 29720509), // Connect the Dots
            array("id" => 26057359) // Forgotten Values
         )
    )
);

// put tracks into playlist
$response = $soundcloud->put(
    '/playlists/' . $response->bodyObject()->id, 
    $tracks
)->request();

var_dump($response->bodyArray());
die();

This Should will work. Take in consideration also that every time you "PUT" a request to a playlist you have to build the tracks array with all the track IDS that you want in that playlist/set, soundcloud will erase all tracks and insert the new ones.

I Would like to ask you, if you find any issues with the library to report them in the github issues page.

Thanks and have fun!

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!