Spotify格式错误的Json

时间:2017-09-01 06:28:00

标签: php json spotify guzzle

https://api.spotify.com/v1/me/player/play端点不断抛出错误

  

客户端错误:PUT https://api.spotify.com/v1/me/player/play?device_id=b3be3728123923782d72b3c0b5e7e3d91b9dfb10导致400 Bad Request响应:{“错误”:{“状态”:400,“消息”:“格式错误的json”}}`

这是我的代码:

$client = new GuzzleHttp\Client();
$res = $client->request('PUT', 'https://api.spotify.com/v1/me/player/play?device_id=".$request->device_id', [
    "headers" => [
        "Authorization" =>  ["Bearer " . $session_owner->spotify_token],
        "Content-Type" => "application/x-www-form-urlencoded",
    ],
    "form_params" => [
        "uris" => ["spotify:track:" . $request->spotify_song_id]
    ]
]);

到目前为止,这是唯一一个给我这个问题的终点。如果我注释掉context_uri行,则最后一个活动设备上最后一首曲目的播放将正常启动。

以下是该行转换为:

"form_params": {
    "uris": ["spotify:track:2Hy7ypRUKL4OPqtNlzBHWM"]
}

1 个答案:

答案 0 :(得分:1)

Spotify要求请求正文是JSON字符串,但您要发送URL编码的表单数据。

将内容类型更改为application/json,将form_params更改为json

"headers" => [
    "Authorization" =>  ["Bearer " . $session_owner->spotify_token],
    "Content-Type" => "application/json",
],
"json" => [
    "context_uri" => "spotify:track:" . $request->spotify_song_id,
]

Guzzle现在将JSON作为完整的请求体发送,没有形式参数。