calliostro/last-fm-client-bundle
🚀 SYMFONY INTEGRATION! Seamless autowiring for the complete Last.fm music API. Zero bloat, maximum performance.
Symfony bundle that integrates the modern calliostro/lastfm-client into your Symfony application. Built with modern PHP 8.1+ features, dependency injection, and powered by Guzzle.
Install via Composer:
composer require calliostro/lastfm-bundle
Configure the bundle in config/packages/calliostro_lastfm.yaml:
calliostro_lastfm:
# Required: API Key for all practical operations (get from https://www.last.fm/api/account/create)
api_key: '%env(LASTFM_API_KEY)%'
# Required for authenticated operations: API Secret
api_secret: '%env(LASTFM_SECRET)%'
# Optional: Session key for scrobbling and user operations
# Get this via Last.fm OAuth flow or use a pre-generated session key
# session_key: '%env(LASTFM_SESSION_KEY)%'
# Optional: HTTP User-Agent header for API requests
# user_agent: 'MyApp/1.0 +https://myapp.com'
# Optional: Professional rate limiting (requires symfony/rate-limiter)
# rate_limiter: lastfm_api # Your configured RateLimiterFactory service
API Key: You need to create an API account at Last.fm to get your API key. This is required for all operations.
API Secret: Required for authenticated write operations like scrobbling, loving tracks, or updating now playing status. Used together with API Key to generate signatures for authenticated requests.
Session Key: Required for user-specific authenticated operations like scrobbling tracks, loving tracks, updating now playing status, or accessing user's personal data. Obtain this through Last.fm's authentication flow or use a pre-generated session key.
User-Agent: By default, the client uses LastfmClient/2.0.0 (+https://github.com/calliostro/lastfm-client) as User-Agent. You can override this in the configuration if needed.
<?php
// src/Controller/MusicController.php
namespace App\Controller;
use Calliostro\LastFm\LastFmClient;
use Symfony\Component\HttpFoundation\JsonResponse;
class MusicController
{
public function artistInfo(string $artist, LastFmClient $client): JsonResponse
{
$artistInfo = $client->getArtistInfo(artist: $artist);
$topTracks = $client->getArtistTopTracks(artist: $artist, limit: 5);
return new JsonResponse([
'artist' => $artistInfo['artist']['name'],
'bio' => $artistInfo['artist']['bio']['summary'] ?? null,
'topTracks' => $topTracks['toptracks']['track'],
]);
}
}
// Scrobbling requires API Key, API Secret AND Session Key
// All three are automatically injected from configuration
$client->scrobbleTrack(
artist: 'The Weeknd',
track: 'Blinding Lights',
timestamp: time()
);
$client->loveTrack(artist: 'Olivia Rodrigo', track: 'good 4 u');
$recentTracks = $client->getUserRecentTracks(user: 'username', limit: 10);
$topArtists = $client->getUserTopArtists(user: 'username', period: '1month');
$artistInfo = $client->getArtistInfo(artist: 'Billie Eilish');
$albumInfo = $client->getAlbumInfo(artist: 'Taylor Swift', album: 'Midnights');
$trackInfo = $client->getTrackInfo(artist: 'The Weeknd', track: 'Blinding Lights');
$similarArtists = $client->getArtistSimilar(artist: 'Olivia Rodrigo');
$topTracks = $client->getArtistTopTracks(artist: 'Dua Lipa', limit: 10);
$topAlbums = $client->getArtistTopAlbums(artist: 'Ariana Grande');
$client->getArtistInfo(artist: 'name') maps to /2.0/?method=artist.getinfo, no abstractionsAll Last.fm API endpoints are supported with clean documentation — see Last.fm API Documentation for complete method reference
<?php
// src/Service/MusicService.php
namespace App\Service;
use Calliostro\LastFm\LastFmClient;
class MusicService
{
public function __construct(
private readonly LastFmClient $client
) {
}
public function getArtistWithTopTracks(string $artist): array
{
$artistInfo = $this->client->getArtistInfo(artist: $artist);
$topTracks = $this->client->getArtistTopTracks(
artist: $artist,
limit: 10
);
return [
'artist' => $artistInfo,
'topTracks' => $topTracks['toptracks']['track'],
];
}
public function scrobbleCurrentTrack(string $artist, string $track): void
{
// Requires API Key, API Secret AND Session Key
$this->client->scrobbleTrack(
artist: $artist,
track: $track,
timestamp: time()
);
}
}
For high-volume applications, use the powerful symfony/rate-limiter component:
composer require symfony/rate-limiter
# config/packages/rate_limiter.yaml
rate_limiter:
lastfm_api:
policy: 'sliding_window'
limit: 5 # Last.fm allows 5 requests per second per IP
interval: '1 second'
# config/packages/calliostro_lastfm.yaml
calliostro_lastfm:
api_key: '%env(LASTFM_API_KEY)%'
api_secret: '%env(LASTFM_SECRET)%'
rate_limiter: lastfm_api
Choose your rate limit based on your usage:
The bundle uses a Guzzle middleware that automatically handles rate limiting by:
This seamless integration ensures your application never exceeds API limits without requiring any code changes. Higher rates may result in HTTP 429 responses if rate limiting is not configured.
Contributions are welcome! Please see DEVELOPMENT.md for detailed setup instructions, testing guide, and development workflow.
This project is licensed under the MIT License — see the LICENSE file for details.
⭐ Star this repo if you find it useful! It helps others discover this lightweight solution.
How can I help you explore Laravel packages today?