calliostro/last-fm-client-bundle

Symfony bundle for the Last.fm API - scrobbling, music data & integration made easy

v1.0.0 2025-09-06 10:59 UTC

This package is auto-updated.

Last update: 2025-09-06 11:22:51 UTC


README

Latest Stable Version Total Downloads License PHP Version CI (Main) codecov

๐Ÿš€ SYMFONY INTEGRATION! Seamless autowiring for the most lightweight Last.fm API client for PHP. Zero bloats, maximum performance.

Symfony bundle that integrates the ultra-minimalist calliostro/lastfm-client into your Symfony application. Built with modern PHP 8.1+ features, dependency injection, and powered by Guzzle.

๐Ÿ“ฆ Installation

Install via Composer:

composer require calliostro/last-fm-client-bundle

โš™๏ธ Configuration

Configure the bundle in config/packages/calliostro_last_fm_client.yaml:

calliostro_last_fm_client:
    # Get your API credentials from https://www.last.fm/api/account/create
    api_key: '%env(LASTFM_API_KEY)%'
    secret: '%env(LASTFM_SECRET)%'
    
    # Optional: pre-configured session key for user authentication
    # session: '%env(LASTFM_SESSION_KEY)%'
    
    # Optional: HTTP client options
    # http_client_options:
    #     timeout: 30
    #     headers:
    #         'User-Agent': 'MyApp/1.0'

API Key & Secret: You need to register your application at Last.fm to get your API key and secret. Both values are required and cannot be empty.

Session Key: This version supports only a pre-configured user session for scrobbling and user-specific actions. For read-only operations (artist info, charts, search), you're all set with just an API key and secret. For full OAuth workflow support, use the standalone calliostro/lastfm-client library.

User-Agent: By default, the client uses LastFmClient/1.0 (+https://github.com/calliostro/lastfm-client) as User-Agent. You can override this in the http_client_options if needed.

๐Ÿš€ Quick Start

Basic Usage

<?php
// src/Controller/MusicController.php

namespace App\Controller;

use Calliostro\LastFm\LastFmApiClient;
use Symfony\Component\HttpFoundation\JsonResponse;

class MusicController
{
    public function artistInfo(string $name, LastFmApiClient $client): JsonResponse
    {
        $artist = $client->artistGetInfo(['artist' => $name]);
        $topTracks = $client->artistGetTopTracks(['artist' => $name, 'limit' => 5]);

        return new JsonResponse([
            'artist' => $artist['artist']['name'],
            'topTracks' => $topTracks['toptracks']['track']
        ]);
    }
}

Scrobbling and User Actions

// Requires pre-configured session key
$client->trackUpdateNowPlaying([
    'artist' => 'Linkin Park',
    'track' => 'In the End'
]);

$client->trackScrobble([
    'artist' => 'Linkin Park',
    'track' => 'In the End',
    'timestamp' => time()
]);

$client->trackLove(['artist' => 'Adele', 'track' => 'Hello']);

Discovery and Charts

$similar = $client->artistGetSimilar(['artist' => 'Imagine Dragons']);
$topTracks = $client->artistGetTopTracks(['artist' => 'Adele']);
$recentTracks = $client->userGetRecentTracks(['user' => 'username']);
$topArtists = $client->chartGetTopArtists(['limit' => 10]);
$rockTracks = $client->tagGetTopTracks(['tag' => 'rock']);

โœจ Key Features

  • Ultra-Lightweight โ€“ Minimal Symfony integration with zero bloats for the ultra-lightweight Last.fm client
  • Complete API Coverage โ€“ All 60+ Last.fm API endpoints supported
  • Direct API Calls โ€“ $client->trackGetInfo() maps to track.getInfo, no abstractions
  • Type Safe + IDE Support โ€“ Full PHP 8.1+ types, PHPStan Level 8, method autocomplete
  • Symfony Native โ€“ Seamless autowiring with Symfony 6.4, 7.x & 8.x
  • Future-Ready โ€“ PHP 8.5 and Symfony 8.0 compatible (beta/dev testing)
  • Well Tested โ€“ 100% test coverage, PSR-12 compliant
  • Configuration-Based Auth โ€“ Pre-configured session key support

๐ŸŽต All Last.fm API Methods as Direct Calls

  • Track Methods โ€“ trackGetInfo(), trackScrobble(), trackUpdateNowPlaying(), trackLove(), trackUnlove()
  • Artist Methods โ€“ artistGetInfo(), artistGetTopTracks(), artistGetSimilar(), artistSearch()
  • User Methods โ€“ userGetInfo(), userGetRecentTracks(), userGetLovedTracks(), userGetTopArtists()
  • Chart Methods โ€“ chartGetTopArtists(), chartGetTopTracks()
  • Album Methods โ€“ albumGetInfo(), albumSearch()
  • Tag Methods โ€“ tagGetInfo(), tagGetTopTracks(), tagGetTopTags()
  • Auth Methods โ€“ authGetToken(), authGetSession()
  • Geo Methods โ€“ geoGetTopArtists(), geoGetTopTracks()
  • Library Methods โ€“ libraryGetArtists()

All 60+ Last.fm API endpoints are supported with clean documentation โ€” see Last.fm API Documentation for complete method reference

๐Ÿ“‹ Requirements

  • php ^8.1
  • symfony ^6.4|^7.0|^8.0
  • calliostro/lastfm-client

๐Ÿ”ง Service Integration

<?php
// src/Service/MusicService.php

namespace App\Service;

use Calliostro\LastFm\LastFmApiClient;

class MusicService
{
    public function __construct(
        private readonly LastFmApiClient $client
    ) {}

    public function scrobbleTrack(string $artist, string $track): void
    {
        // Requires pre-configured session key
        $this->client->trackScrobble([
            'artist' => $artist,
            'track' => $track,
            'timestamp' => time()
        ]);
    }

    public function updateNowPlaying(string $artist, string $track): void
    {
        $this->client->trackUpdateNowPlaying([
            'artist' => $artist,
            'track' => $track
        ]);
    }
}

๐Ÿงช Testing

Run the test suite:

composer test

Run static analysis:

composer analyse

Check code style:

composer cs

๐Ÿ“– API Documentation Reference

For complete API documentation including all available parameters, visit the Last.fm API Documentation.

Popular Methods

Track Methods

  • trackGetInfo($params) โ€“ Get track information
  • trackSearch($params) โ€“ Search for tracks
  • trackScrobble($params) โ€“ Scrobble a track (auth required)
  • trackUpdateNowPlaying($params) โ€“ Update now playing (auth required)
  • trackLove($params) โ€“ Love a track (auth required)
  • trackUnlove($params) โ€“ Remove love from track (auth required)

Artist Methods

  • artistGetInfo($params) โ€“ Get artist information
  • artistGetTopTracks($params) โ€“ Get artist's top tracks
  • artistGetSimilar($params) โ€“ Get similar artists
  • artistSearch($params) โ€“ Search for artists

User Methods

  • userGetInfo($params) โ€“ Get user profile information
  • userGetRecentTracks($params) โ€“ Get user's recent tracks
  • userGetLovedTracks($params) โ€“ Get user's loved tracks
  • userGetTopArtists($params) โ€“ Get user's top artists

๐Ÿค Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

Please ensure your code follows PSR-12 standards and includes tests.

๐Ÿ“„ License

This project is licensed under the MIT License โ€” see the LICENSE file for details.

๐Ÿ™ Acknowledgments

โญ Star this repo if you find it useful! It helps others discover this lightweight solution.