1
votes

I am currently new to using php and Laravel and working with an API however I have been following the Spotify PHP tutorial https://github.com/jwilsson/spotify-web-api-php.

I've also put in bold some of my questions that I wanted to ask , hopefully someone can help.

I have followed all steps but need help just to get it working.

Put the following code in its own file, lets call it auth.php. Replace CLIENT_ID and CLIENT_SECRET with the values given to you by Spotify.

(Where abouts should I save this file?)

The REDIRECT_URI is the one you entered when creating the Spotify app, make sure it's an exact match.

(I used my localhost:8888/callback/ not sure if that is correct?) Obviously I haven't put me details in here on this website as for security reasons.

<?php
require 'vendor/autoload.php';

$session = new SpotifyWebAPI\Session(
    'CLIENT_ ID',
    'CLIENT_SECRET',
    'REDIRECT_URL'
);

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

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



?>

When the user has approved your app, Spotify will redirect the user together with a code to the specifed redirect URI. You'll need to use this code to request a access token from Spotify.

put this code in a new file called callback.php:

Do replace client id and secret with my detail? also how do I save the access token?

require 'vendor/autoload.php';

$session = new SpotifyWebAPI\Session(
    'CLIENT_ID',
    'CLIENT_SECRET',
    'REDIRECT_URI'
);

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

$accessToken = $session->getAccessToken();
$refreshToken = $session->getRefreshToken();

// Store the access and refresh tokens somewhere. In a database for example.

// Send the user along and fetch some data!
header('Location: app.php');
die();

In a third file, app.php, tell the API wrapper which access token to use, and then make some API calls!

(Where do i also save this file and how do I make these calls in my Laravel Controllers?)

require 'vendor/autoload.php';

$api = new SpotifyWebAPI\SpotifyWebAPI();

// Fetch the saved access token from somewhere. A database for example.
$api->setAccessToken($accessToken);

// It's now possible to request data about the currently authenticated user
print_r(
    $api->me()
);

// Getting Spotify catalog data is of course also possible
print_r(
    $api->getTrack('7EjyzZcbLxW7PaaLua9Ksb')
);
1

1 Answers

1
votes

(Where abouts should I save this file?)

You can save this file in differents places in laravel, for testing you could write it in a controller (not the best but you can).

Do replace client id and secret with my detail?

Yes of course !

also how do I save the access token?

You can save in a database or in a session or where you want. If you store it in a session you will have to make a new request to get a new Access token if the user logged out of your application. In a database you can reuse it.

Many access token are only available for a specific duration. The spotify doc should speak of it.

(Where do i also save this file and how do I make these calls in my Laravel Controllers?)

For testing you can do this in your controller, but it's a good idea to have a service layer where you put the business logic of your application.

Do not copy require 'vendor/autoload.php'; in your file laravel handle the composer autoload already.