最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Get spotify playlist tracks using php - Stack Overflow

programmeradmin1浏览0评论

I'm trying get playlist tracks from Spotify api, but after many tries i'm just receiving error 404, not found. Can anyone help? The playlist exist! I try get more info from chat GPT but is giving the same error 404.

The access token is working because i can get the artist and albuns.

<?php

// Spotify API credentials
$client_id = 'client_id';
$client_secret = 'client_secret';



$access_token = getAccessToken($client_id, $client_secret);

if (!$access_token) {
    die("Error: Unable to retrieve access token.\n");
}
echo "Access Token: $access_token\n";
// Playlist ID (Ensure it is public)
$playlistId = "37i9dQZEVXbKyJS56d1pgi";
$url = "/$playlistId/tracks";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    "Authorization: Bearer $access_token"
]);
curl_setopt($ch, CURLOPT_FAILONERROR, true); // Fail on error for better debugging

$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

// Debugging API response
if ($httpCode === 200) {
    $data = json_decode($response, true);
    print_r($data);
} else {
    echo "Error fetching playlist: HTTP $httpCode\n";
    echo "Response: $response\n";
}

?>

I'm trying get playlist tracks from Spotify api, but after many tries i'm just receiving error 404, not found. Can anyone help? The playlist exist! I try get more info from chat GPT but is giving the same error 404.

The access token is working because i can get the artist and albuns.

<?php

// Spotify API credentials
$client_id = 'client_id';
$client_secret = 'client_secret';



$access_token = getAccessToken($client_id, $client_secret);

if (!$access_token) {
    die("Error: Unable to retrieve access token.\n");
}
echo "Access Token: $access_token\n";
// Playlist ID (Ensure it is public)
$playlistId = "37i9dQZEVXbKyJS56d1pgi";
$url = "https://api.spotify/v1/playlists/$playlistId/tracks";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    "Authorization: Bearer $access_token"
]);
curl_setopt($ch, CURLOPT_FAILONERROR, true); // Fail on error for better debugging

$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

// Debugging API response
if ($httpCode === 200) {
    $data = json_decode($response, true);
    print_r($data);
} else {
    echo "Error fetching playlist: HTTP $httpCode\n";
    echo "Response: $response\n";
}

?>
Share Improve this question asked Feb 4 at 13:26 Mário Je RochaMário Je Rocha 251 silver badge4 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 3
{
  "error": {
    "status": 404,
    "message": "Resource not found"
  }
}

Your issue is caused by the Playlist ID not existing.

When I try your code with a public playlist, eg 3cEYpjA9oz9GiPac4AsH4n it works fine.

So you'll need to use a correct playlist ID, or make sure your current API key is the OWNER of that playlist.

发布评论

评论列表(0)

  1. 暂无评论