简体   繁体   English

PHP:使用Web-api添加曲目以点播播放列表-JSON错误

[英]PHP: Adding tracks to spotify playlist with web-api - JSON ERROR

I am trying to add a track to my own playlist with a little php script, but it won't work. 我正在尝试使用一个小的php脚本将曲目添加到我自己的playlist ,但是它将无法正常工作。

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/ 这是用于添加曲目的Spotify文档: 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); 

I solved it finally: 我终于解决了:

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

$url = ' https://api.spotify.com/v1/users/ USERID /playlists/ playlistID /tracks?uris=spotify%3Atrack%3A3DXncPQOG4VBw3QHh3S817'; $ 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);

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);

// ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM