In the Spotify documentation, it indicates that it is possible to send the instructions to play the song.
He tried to transfer this to php but did not respond to the request. I don't know what I will do wrong, I hope to solve this problem.
This my request body
{
"uris": [
"spotify:track:7fODjB7BrQTGqh0hogW6XD"
]
}
From the web it works, but in php I can't send the request.
{ "error": { "status": 401, "message": "Invalid access token" } }
This is my php code [UPDATED - 2019/11/8]
function sendTrack($token){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.spotify.com/v1/me/player/play' );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(['uris' => ['spotify:track:7fODjB7BrQTGqh0hogW6XD']]));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer '.$token.''));
$result=curl_exec($ch);
echo $result;
}
Your CURL_POSTFIELDS are in wrong format. Try this
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(['uris' => ['spotify:track:7fODjB7BrQTGqh0hogW6XD']]);
Edit: Also according to spotify documentation you need to send a PUT request so instead of
curl_setopt($ch, CURLOPT_POST, 1 );
try
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
Edit2: just tried this code and it works, only creating error that no active device was found which im sure happened because i missed a step or two.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.spotify.com/v1/me/player/play' );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(['uris' => ['spotify:track:7fODjB7BrQTGqh0hogW6XD']]));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: json', 'Authorization: Bearer ' . $token));
$result=curl_exec($ch);
echo $result;