How to create SoundCloud playlist using PHP wrapper

南楼画角 提交于 2019-12-01 10:57:07

问题


I am trying to create a playlist using the njasm PHP wrapper, which can be viewed here. The reason I am using this wrapper is because it allows for direct user login. And for my purposes, this is a requirement.

Let me start by saying that I am able to connect to the API; before attempting to create a playlist, I do a few get requests for certain info.

Right now I am trying this

$response = $facade->post('/playlists', $prepaired_post_array)->request();

$prepaired_post_array contains

[0] = playlist[title]=LA Sun
[1] = &playlist[tracks][][id]=102981743
[2] = &playlist[tracks][][id]=93248225
[3] = &playlist[tracks][][id]=25780933

This statement returns code 422. Which, according to SoundClouds API, is close to something right, but something is messed up. I have tried a few different things, but everything else just returns code 400, which is just a bad request.


回答1:


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!



来源:https://stackoverflow.com/questions/29156861/how-to-create-soundcloud-playlist-using-php-wrapper

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