spatie/last-fm-now-playing
Fetch the currently playing track for any Last.fm user. Provide your API key and username to retrieve artist, album, track name, artwork, and track URL, or false if nothing is playing. Throws BadResponse on errors.
LastFmNowPlaying), it can be integrated into Laravel’s service container or used as a one-off utility without tight coupling to other systems.LastFm facade or service binding aligns with Laravel’s conventions..env or a secrets manager).now_playing table or broadcasting via WebSockets).LastFmNowPlaying class to the container for dependency injection.Http facade or Guzzle for API calls (the package uses Guzzle under the hood).composer require spatie/last-fm-now-playing..env:
LASTFM_API_KEY=your_api_key_here
AppServiceProvider:
$this->app->singleton(LastFmNowPlaying::class, function ($app) {
return new LastFmNowPlaying(config('lastfm.api_key'));
});
use Spatie\LastFmNowPlaying\Facades\LastFm;
$track = LastFm::getNowPlaying('username');
public function __construct(private LastFmNowPlaying $lastFm) {}
$track = Cache::remember("lastfm_{$username}", 60, function () use ($username) {
return LastFm::getNowPlaying($username);
});
| Failure Scenario | Impact | Mitigation |
|---|---|---|
| Last.fm API downtime | "Now playing" data unavailable | Cache responses; show stale data or placeholder. |
| Rate limit exceeded | API requests blocked | Implement exponential backoff; use caching. |
| Invalid user ID | Empty/incorrect data returned | Validate user IDs before API calls. |
| API key revoked/expired | All requests fail | Monitor API key status; auto-rotate keys. |
| High traffic spike | Database/queue overload | Use read replicas; scale workers. |
LastFmNowPlaying class to test business logic without hitting the API.How can I help you explore Laravel packages today?