1. Go to this page and download the library: Download emmpaul/laravel-spotify 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/ */
// App\Models\User
use emmpaul\LaravelSpotify\Traits\HasSpotifyAuth;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use HasSpotifyAuth;
// Your existing model code...
}
// Generate Spotify login URL
use emmpaul\LaravelSpotify\Facades\LaravelSpotify;
$spotifyAuthUrl = LaravelSpotify::getAuthUrl();
<a href="{{ route('spotify.auth') }}">Login with Spotify</a>
// Check if user has Spotify authentication
$user->hasSpotifyAuth(); // Returns boolean
// Check if token is expired
$user->isSpotifyTokenExpired(); // Returns boolean
// Update tokens (typically done automatically)
$user->updateSpotifyTokens($accessToken, $refreshToken, $expiresIn);
// Clear tokens
$user->clearSpotifyTokens();
use emmpaul\LaravelSpotify\Facades\LaravelSpotify;
// Set access token
$spotify = LaravelSpotify::setAccessToken($user->spotify_token);
// Get user profile
$profile = $spotify->api()->getCurrentUsersProfile();
// Get user's top tracks
$topTracks = $spotify->api()->getUserTopTracks();
// Get user's playlists
$playlists = $spotify->api()->getCurrentUsersPlaylists();
use emmpaul\LaravelSpotify\Services\SpotifyService;
$spotifyService = new SpotifyService($user->spotify_token);
// Get current user's profile
$response = $spotifyService->getCurrentUsersProfile();
$userData = $response->json();
// Search for tracks
$results = $spotifyService->searchForItem('Bohemian Rhapsody', ['track']);
// Get a specific track
$track = $spotifyService->getTrack('4u7EnebtmKWzUH433cf1Qv');
use emmpaul\LaravelSpotify\Facades\LaravelSpotify;
use Illuminate\Support\Facades\Auth;
// In a controller method
public function getUserStats()
{
$user = Auth::user();
if (!$user->hasSpotifyAuth()) {
return redirect()->route('spotify.auth');
}
$spotify = LaravelSpotify::setAccessToken($user->spotify_token);
// Get user's top artists (last 6 months)
$topArtists = $spotify->api()->getUserTopArtists('medium_term', 10);
// Get recently played tracks
$recentTracks = $spotify->api()->getRecentlyPlayedTracks(20);
return view('spotify.stats', [
'topArtists' => $topArtists->json(),
'recentTracks' => $recentTracks->json(),
]);
}
use emmpaul\LaravelSpotify\Enums\SpotifyTimeRange;
use emmpaul\LaravelSpotify\Enums\SpotifyTopType;
// Get top items with enums
$spotify->api()->getUserTop(SpotifyTopType::TRACKS, SpotifyTimeRange::SHORT_TERM, 20);
$spotify->api()->getUserTop(SpotifyTopType::ARTISTS, SpotifyTimeRange::LONG_TERM, 50);
// Convenience methods
$spotify->api()->getUserTopTracks('short_term', 20);
$spotify->api()->getUserTopArtists('long_term', 50);
// Recently played
$spotify->api()->getRecentlyPlayedTracks(50);
use emmpaul\LaravelSpotify\Enums\SpotifyRepeatState;
// Get player info
$spotify->api()->getPlaybackState();
$spotify->api()->getCurrentlyPlayingTrack();
$spotify->api()->getAvailableDevices();
$spotify->api()->getTheUsersQueue();
// Playback controls
$spotify->api()->resumePlayback($deviceId);
$spotify->api()->pausePlayback($deviceId);
$spotify->api()->skipToNext($deviceId);
$spotify->api()->skipToPrevious($deviceId);
$spotify->api()->seekToPosition(30000, $deviceId); // position in ms
// Playback settings
$spotify->api()->setRepeatMode('track', $deviceId); // 'track', 'context', or 'off'
$spotify->api()->setRepeatMode(SpotifyRepeatState::CONTEXT); // or use the enum
$spotify->api()->toggleShuffle(true, $deviceId);
$spotify->api()->setPlaybackVolume(75, $deviceId); // 0-100, clamped automatically
// Transfer and queue
$spotify->api()->transferPlayback($deviceId, play: true);
$spotify->api()->addItemToPlaybackQueue('spotify:track:xxxx', $deviceId);
use emmpaul\LaravelSpotify\Enums\SpotifyTimeRange;
// Available time ranges:
SpotifyTimeRange::SHORT_TERM; // ~4 weeks
SpotifyTimeRange::MEDIUM_TERM; // ~6 months (default)
SpotifyTimeRange::LONG_TERM; // Several years
// Usage
$topTracks = $spotify->api()->getUserTopTracks(SpotifyTimeRange::SHORT_TERM, 20);