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.
Installation:
composer require spatie/last-fm-now-playing
Publish the config file (if needed):
php artisan vendor:publish --provider="Spatie\LastFmNowPlaying\LastFmNowPlayingServiceProvider"
Configuration:
.env:
LASTFM_API_KEY=your_api_key_here
config/last-fm-now-playing.php:
'username' => 'your_lastfm_username',
First Use Case: Fetch the currently playing track in a controller or service:
use Spatie\LastFmNowPlaying\Facades\LastFmNowPlaying;
$nowPlaying = LastFmNowPlaying::getNowPlaying();
dd($nowPlaying); // Returns an array with track details (e.g., title, artist, album, etc.)
Fetching Now-Playing Data:
$nowPlaying = LastFmNowPlaying::getNowPlaying();
null or an empty array).Integration with User Profiles:
$user = User::find($id);
$nowPlaying = LastFmNowPlaying::getNowPlayingForUser($user->lastfm_username);
return view('profile', compact('user', 'nowPlaying'));
Caching for Performance:
$nowPlaying = Cache::remember("lastfm_{$user->lastfm_username}_now_playing", now()->addMinutes(5), function () use ($user) {
return LastFmNowPlaying::getNowPlayingForUser($user->lastfm_username);
});
Real-Time Updates:
Error Handling:
try-catch block to handle API failures gracefully:
try {
$nowPlaying = LastFmNowPlaying::getNowPlaying();
} catch (\Exception $e) {
Log::error("Last.fm API error: " . $e->getMessage());
$nowPlaying = null;
}
Dynamic User Handling:
$nowPlaying = LastFmNowPlaying::getNowPlayingForUser('dynamic_username');
Combining with Other APIs:
Broadcasting Updates:
broadcast(new NowPlayingUpdated($nowPlaying))->toOthers();
Testing:
LastFmNowPlaying service in tests:
$this->partialMock(Spatie\LastFmNowPlaying\LastFmNowPlaying::class, function ($mock) {
$mock->shouldReceive('getNowPlaying')->andReturn(['track' => 'Test Track']);
});
API Rate Limits:
No Track Playing:
null or an empty response if the user isn’t listening to anything. Always check for this:
if (empty($nowPlaying)) {
return "User is not listening to anything right now.";
}
API Key Restrictions:
Deprecated Methods:
Time Zone Issues:
Enable Debugging:
LASTFM_DEBUG in .env to true to log API requests/responses:
LASTFM_DEBUG=true
Check API Response:
$response = LastFmNowPlaying::getNowPlaying();
Log::debug('Last.fm API Response:', $response);
Validate API Key:
Customize Response:
Spatie\LastFmNowPlaying\LastFmNowPlaying class to modify the response format:
class CustomLastFmNowPlaying extends \Spatie\LastFmNowPlaying\LastFmNowPlaying {
public function getNowPlaying() {
$data = parent::getNowPlaying();
return $this->formatData($data);
}
protected function formatData($data) {
// Custom formatting logic
return $data;
}
}
Add Retry Logic:
retry helper or a package like spatie/retries.Support Multiple Users:
'usernames' => [
'user1',
'user2',
],
Then loop through them in your code.Webhook Integration:
Fallback Data:
$nowPlaying = LastFmNowPlaying::getNowPlaying();
if (empty($nowPlaying)) {
$nowPlaying = LastFmNowPlaying::getRecentTracks()->first();
}
How can I help you explore Laravel packages today?