PHP:使用web api向spotify播放列表添加曲目-JSON错误

PHP:使用web api向spotify播放列表添加曲目-JSON错误,php,json,curl,spotify,Php,Json,Curl,Spotify,我正试图用一个小php脚本将一首曲目添加到我自己的播放列表中,但它不起作用 我总是犯错误: { "error" : { "status" : 400, "message" : "Error parsing JSON." } } 这是用于添加曲目的spotify文档: 有人知道问题出在哪里吗 $token='my Access token'; $headers = array( "Accept: application/json", "Authorization: Beare

我正试图用一个小php脚本将一首曲目添加到我自己的播放列表中,但它不起作用

我总是犯错误:

{ "error" : { "status" : 400, "message" : "Error parsing JSON." } }
这是用于添加曲目的spotify文档:

有人知道问题出在哪里吗

$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); 
试试这个:

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

// ...
我终于解决了:

$key='***AccessKey***';
$url='USERID/playlists/playlid/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);

通过2分钟阅读手册curl_setopt$ch,CURLOPT_POSTFIELDS,{uris:spotify:track:3DXncPQOG4VBw3QHh3S817};是的,我看到了这个,但我是json新手,我必须在我的代码中更改什么?我像你发布的那样更改了它,它给出了相同的错误: