[GH-ISSUE #90] Follow playlist example #47

Closed
opened 2026-02-27 19:25:46 +03:00 by kerem · 3 comments
Owner

Originally created by @Pau1fitz on GitHub (Jun 12, 2017).
Original GitHub issue: https://github.com/jwilsson/spotify-web-api-php/issues/90

Hi @vdesabou and @jwilsson. Apologies for the basic question. I am more of a javascript person and have used the spotify api with node before, however for this project I need to use php.

I am trying to follow a user playlist but for some reason it isn't working. If it is something really basic apologies in advance.

My code looks like this.

step1.php

<?php

require 'vendor/autoload.php';

$session = new SpotifyWebAPI\Session(
    '****',
    '*****',
    'http://***/step2.php'
);

$options = [
    'scope' => [
        'playlist-read-private',
        'user-read-private',
		'playlist-modify-public',
		'playlist-modify-private'
    ],
];

header('Location: ' . $session->getAuthorizeUrl($options));
die();

?>

My step2.php file looks like this

<?php

require 'vendor/autoload.php';

$session = new SpotifyWebAPI\Session('******', '*****', 'http://****/step2.php');
$api = new SpotifyWebAPI\SpotifyWebAPI();

// Request a access token using the code from Spotify
$session->requestAccessToken($_GET['code']);
$accessToken = $session->getAccessToken();

// Set the access token on the API wrapper
$api->setAccessToken($accessToken);

$me = $api->me();
$id = $me->id;

$api->followPlaylist($id, '2v3iNvBX8Ay1Gt2uXtUKUT');

echo 'success';
?>
Originally created by @Pau1fitz on GitHub (Jun 12, 2017). Original GitHub issue: https://github.com/jwilsson/spotify-web-api-php/issues/90 Hi @vdesabou and @jwilsson. Apologies for the basic question. I am more of a javascript person and have used the spotify api with node before, however for this project I need to use php. I am trying to follow a user playlist but for some reason it isn't working. If it is something really basic apologies in advance. My code looks like this. `step1.php` ``` <?php require 'vendor/autoload.php'; $session = new SpotifyWebAPI\Session( '****', '*****', 'http://***/step2.php' ); $options = [ 'scope' => [ 'playlist-read-private', 'user-read-private', 'playlist-modify-public', 'playlist-modify-private' ], ]; header('Location: ' . $session->getAuthorizeUrl($options)); die(); ?> ``` My `step2.php` file looks like this ``` <?php require 'vendor/autoload.php'; $session = new SpotifyWebAPI\Session('******', '*****', 'http://****/step2.php'); $api = new SpotifyWebAPI\SpotifyWebAPI(); // Request a access token using the code from Spotify $session->requestAccessToken($_GET['code']); $accessToken = $session->getAccessToken(); // Set the access token on the API wrapper $api->setAccessToken($accessToken); $me = $api->me(); $id = $me->id; $api->followPlaylist($id, '2v3iNvBX8Ay1Gt2uXtUKUT'); echo 'success'; ?> ```
kerem 2026-02-27 19:25:46 +03:00
  • closed this issue
  • added the
    question
    label
Author
Owner

@jwilsson commented on GitHub (Jun 12, 2017):

Hi again!
When I'm running your code I'm getting a "Playlist doesn't exist" error back from the API. Whenever an error is returned from the API this library will throw.

Try adding this to all your API calls so you'll be able to see any errors returned:

try {
    $api->followPlaylist($id, '2v3iNvBX8Ay1Gt2uXtUKUT');
} catch (SpotifyWebAPI\SpotifyWebAPIException $e) {
    echo $e->getMessage();
    die();
}

Hope this helps!

<!-- gh-comment-id:307734226 --> @jwilsson commented on GitHub (Jun 12, 2017): Hi again! When I'm running your code I'm getting a "Playlist doesn't exist" error back from the API. Whenever an error is returned from the API this library will throw. Try adding this to all your API calls so you'll be able to see any errors returned: ```php try { $api->followPlaylist($id, '2v3iNvBX8Ay1Gt2uXtUKUT'); } catch (SpotifyWebAPI\SpotifyWebAPIException $e) { echo $e->getMessage(); die(); } ``` Hope this helps!
Author
Owner

@Pau1fitz commented on GitHub (Jun 12, 2017):

@jwilsson I see that I the id needs to be the owner of the playlists id.

<!-- gh-comment-id:307739153 --> @Pau1fitz commented on GitHub (Jun 12, 2017): @jwilsson I see that I the id needs to be the owner of the playlists id.
Author
Owner

@jwilsson commented on GitHub (Jun 12, 2017):

I'm taking that as it's working now? If not, just give me a shout!

<!-- gh-comment-id:307792772 --> @jwilsson commented on GitHub (Jun 12, 2017): I'm taking that as it's working now? If not, just give me a shout!
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/spotify-web-api-php#47
No description provided.