0

I am trying to add a track to my own playlist with a little php script, but it won't work.

I always get the errror:

{ "error" : { "status" : 400, "message" : "Error parsing JSON." } }

This is the spotify document for adding tracks: https://developer.spotify.com/web-api/add-tracks-to-playlist/

Has anybody an idea what the problem could be?

$token='my Access token';

$headers = array(
    "Accept: application/json",
    "Authorization: Bearer " . $token
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.spotify.com/v1/users/*myusername*/playlists/*myplaylistID*/tracks' );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, CURLOPT_POST, 1 );
curl_setopt($ch, CURLOPT_POSTFIELDS, 'uris=spotify:track:3DXncPQOG4VBw3QHh3S817' );
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result=curl_exec($ch);

print "$result";

$result = json_decode($result, true);
curl_close($ch); 
3
  • 1
    From a 2 minute read of the manual curl_setopt($ch, CURLOPT_POSTFIELDS, '{"uris":"spotify:track:3DXncPQOG4VBw3QHh3S817"}' ); Commented Dec 25, 2017 at 20:40
  • Yeah, I saw this, but I a am new to json, what must I change in my code? Commented Dec 25, 2017 at 20:41
  • I changed it like you posted, it gave the same error :( Commented Dec 25, 2017 at 20:43

2 Answers 2

1

I solved it finally:

$key='***AccessKey***';

$url = 'https://api.spotify.com/v1/users/USERID/playlists/playlistID/tracks?uris=spotify%3Atrack%3A3DXncPQOG4VBw3QHh3S817';

$headers = array(
        "Content-Length: 0",
        "Accept-Encoding: gzip, deflate, compress",
        "User-Agent: runscope/0.1",
        "Content-Type: application/json",
        "Authorization: Bearer ".$key);


    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = json_decode(curl_exec($ch), true);
    curl_close($ch);
    print_r($response);
Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

$data = array('uris' => 'spotify:track:3DXncPQOG4VBw3QHh3S817');
$data_json = json_encode($data);

// ...

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.spotify.com/...');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_json);
curl_setopt($ch, CURLOPT_HTTPHEADER, array
(                                                                          
    'Content-Type: application/json',                                                                                
    'Content-Length: '.strlen($data_json),
    'Authorization: Bearer '.$token
));  

$result = curl_exec($ch);

// ...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.