1. Go to this page and download the library: Download calliostro/spotify-bundle library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
calliostro / spotify-bundle example snippets
namespace App\Controller;
use Calliostro\Spotify\SpotifyClient;
use Symfony\Component\HttpFoundation\JsonResponse;
final class MusicController
{
public function search(string $query, SpotifyClient $spotify): JsonResponse
{
$results = $spotify->search(query: $query, type: ['artist', 'album'], limit: 5);
return new JsonResponse($results);
}
public function artist(string $id, SpotifyClient $spotify): JsonResponse
{
$artist = $spotify->getArtist($id);
$topTracks = $spotify->getArtistTopTracks($id);
return new JsonResponse([
'artist' => $artist,
'top_tracks' => $topTracks['tracks'] ?? [],
]);
}
}
namespace App\Controller;
use Calliostro\Spotify\AuthHelper;
use Calliostro\Spotify\SpotifyClientFactory;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
final class SpotifyAuthController
{
public function login(AuthHelper $authHelper, UrlGeneratorInterface $urlGenerator): RedirectResponse
{
$redirectUri = $urlGenerator->generate('spotify_callback', [], UrlGeneratorInterface::ABSOLUTE_URL);
$authorizeUrl = $authHelper->getAuthorizationUrl($redirectUri, [
'user-read-private',
'user-read-email',
]);
return new RedirectResponse($authorizeUrl);
}
public function callback(
Request $request,
AuthHelper $authHelper,
UrlGeneratorInterface $urlGenerator
): Response {
$code = $request->query->getString('code');
$redirectUri = $urlGenerator->generate('spotify_callback', [], UrlGeneratorInterface::ABSOLUTE_URL);
// Exchange authorization code for access & refresh tokens
$tokenData = $authHelper->requestAccessToken($code, $redirectUri);
// Create a user-authenticated client
$userClient = SpotifyClientFactory::createWithUserAuth(
clientId: $authHelper->getClientId() ?? '',
clientSecret: $authHelper->getClientSecret() ?? '',
accessToken: $tokenData['access_token'],
refreshToken: $tokenData['refresh_token'],
expiresAt: time() + $tokenData['expires_in']
);
$me = $userClient->getCurrentUser();
return new Response('Hello, ' . ($me['display_name'] ?? 'User'));
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.