PHP code example of calliostro / last-fm-client-bundle

1. Go to this page and download the library: Download calliostro/last-fm-client-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

calliostro / last-fm-client-bundle example snippets



// 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 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');


// 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()
        );
    }
}