calliostro/lastfm-client
Lightweight Last.fm API client for PHP 8.1+ with clean, modern methods for artists, albums, tracks, search, and charts. Uses Guzzle and supports API-key access for all calls plus session authentication for write actions like scrobbling and loving tracks.
HttpClient facade or GuzzleHttp\Client).getArtistInfo(), scrobbleTrack()), reducing boilerplate for API calls. This fits Laravel’s service-layer pattern (e.g., a LastFmService facade or repository).ConfigCache singleton) and Guzzle middleware support for retries/rate limiting.guzzlehttp/guzzle (v7.x).LastFmClient to the container).HandlerStack, which can be integrated with Laravel’s HTTP middleware pipeline.| Risk Area | Mitigation Strategy |
|---|---|
| API Key Management | Store credentials in Laravel’s .env or Vault (e.g., HashiCorp Vault). Use env() helper for configuration. |
| Rate Limiting | Implement Guzzle middleware for retries/exponential backoff (example provided in README). |
| Authentication Flow | For session-based auth, use Laravel’s session driver to persist tokens (e.g., AuthHelper + session()). |
| Breaking Changes | Version 2.x introduces method renaming (e.g., artistGetInfo → getArtistInfo). Test thoroughly during migration. |
| Error Handling | Last.fm returns HTTP errors (e.g., 403 for invalid keys). Wrap calls in try-catch and log errors via Laravel’s Log facade. |
| Performance | Cache responses (e.g., Cache::remember()) for read-heavy endpoints (e.g., charts). |
createWithApiKey() or createWithSession() is needed.App\Exceptions\Handler.HttpClient mocking or PestPHP for API testing.// app/Providers/AppServiceProvider.php
public function register(): void
{
$this->app->singleton(LastFmClient::class, fn () =>
LastFmClientFactory::createWithApiKey(
config('services.lastfm.api_key'),
config('services.lastfm.api_secret')
)
);
}
config/services.php:
'lastfm' => [
'api_key' => env('LASTFM_API_KEY'),
'api_secret' => env('LASTFM_API_SECRET'),
'session_key' => env('LASTFM_SESSION_KEY'), // Optional
],
HttpClient facade as a fallback if Guzzle middleware is needed:
$response = Http::withOptions(['timeout' => 30])
->get('https://ws.audioscrobbler.com/2.0/', [
'method' => 'artist.getInfo',
'artist' => 'Billie Eilish',
'api_key' => config('services.lastfm.api_key'),
]);
use GuzzleHttp\Middleware;
$stack = HandlerStack::create();
$stack->push(Middleware::retry(
fn ($retries) => $retries < 3,
fn ($retries) => 1000 * (2 ** $retries)
));
$lastfm = LastFmClientFactory::createWithApiKey(..., ['handler' => $stack]);
createWithApiKey() for public data (e.g., artist/track info, charts).$client = app(LastFmClient::class);
$artist = $client->getArtistInfo('Taylor Swift');
AuthHelper for OAuth flow (see README example).session_key in Laravel’s session or database.$auth = new AuthHelper(config('services.lastfm.api_key'), config('services.lastfm.api_secret'));
$sessionKey = $auth->getSession($_GET['token']); // From OAuth callback
session(['lastfm_session_key' => $sessionKey]);
$client = LastFmClientFactory::createWithSession(..., $sessionKey);
Scrobbled event).event(new Scrobbled($trackData));
.env.getArtistInfo).try-catch and log errors.composer update calliostro/lastfm-client)..env or Vault to avoid hardcoding.config() helper for dynamic access.How can I help you explore Laravel packages today?