PHP:使用web-api添加曲目以显示播放列表 - JSON ERROR

时间:2017-12-25 20:36:47

标签: php json curl spotify

我正在尝试使用一个小小的PHP脚本向我自己的playlist添加一个轨道,但它不起作用。

我总是得到错误的错误:

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

这是添加曲目的spotify文档: https://developer.spotify.com/web-api/add-tracks-to-playlist/

有人知道问题可能是什么吗?

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

2 个答案:

答案 0 :(得分:1)

我终于解决了它:

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

$ url =' https://api.spotify.com/v1/users/ USERID /播放列表/ playlistID / track ?的URI = Spotify的%3Atrack%3A3DXncPQOG4VBw3QHh3S817&#39 ;;

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

答案 1 :(得分:0)

试试这个:

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

// ...