PHP code example of calliostro / lastfm-client
1. Go to this page and download the library: Download calliostro/lastfm-client 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 / lastfm-client example snippets
$lastfm = LastFmClientFactory::createWithApiKey('your-api-key', 'your-secret');
$artist = $lastfm->getArtistInfo('Billie Eilish'); // Get artist info
$release = $lastfm->getAlbumInfo('The Weeknd', 'Dawn FM'); // Album info
$charts = $lastfm->getTopArtistsChart(); // Global charts
$lastfm = LastFmClientFactory::createWithApiKey('your-api-key', 'your-secret');
// Positional parameters (traditional)
$results = $lastfm->searchArtists('Taylor Swift', 20);
$tracks = $lastfm->searchTracks('Anti-Hero', 'Taylor Swift');
// Named parameters (PHP 8.0+, recommended for clarity)
$results = $lastfm->searchArtists(artist: 'Taylor Swift', limit: 20);
$tracks = $lastfm->searchTracks(track: 'Anti-Hero', artist: 'Taylor Swift');
$lastfm = LastFmClientFactory::createWithSession('your-api-key', 'your-secret', 'your-session-key');
$collection = $lastfm->getUserRecentTracks('your-username');
$loved = $lastfm->getUserLovedTracks('your-username');
// Scrobble and love tracks with named parameters
$lastfm->scrobbleTrack(
artist: 'Bad Bunny',
track: 'Un Verano Sin Ti',
timestamp: time()
);
$lastfm = LastFmClientFactory::createWithMobileAuth('your-api-key', 'your-secret', 'your-username', 'your-password');
$identity = $lastfm->getUserInfo();
use Calliostro\LastFm\LastFmClientFactory;
$lastfm = LastFmClientFactory::createWithApiKey('your-api-key', 'your-secret');
use Calliostro\LastFm\LastFmClientFactory;
use GuzzleHttp\{HandlerStack, Middleware};
$handler = HandlerStack::create();
$handler->push(Middleware::retry(
fn ($retries, $request, $response) => $retries < 3 && $response?->getStatusCode() === 429,
fn ($retries) => 1000 * 2 ** ($retries + 1) // Rate limit handling
));
$lastfm = LastFmClientFactory::createWithApiKey('your-api-key', 'your-secret', [
'timeout' => 30,
'handler' => $handler,
'headers' => [
'User-Agent' => 'MyApp/1.0 (+https://myapp.com)',
]
]);
// authorize.php
use Calliostro\LastFm\AuthHelper;
$apiKey = 'your-api-key';
$secret = 'your-secret';
$callbackUrl = 'https://yourapp.com/callback.php';
$auth = new AuthHelper($apiKey, $secret);
// For web apps, you can skip token generation and redirect directly:
$authUrl = "https://www.last.fm/api/auth/?api_key={$apiKey}&cb=" . urlencode($callbackUrl);
// For desktop apps, generate token first:
// $tokenData = $auth->getToken();
// $authUrl = $auth->getAuthorizationUrl($tokenData['token']);
header("Location: {$authUrl}");
exit;
// callback.php
\{AuthHelper, LastFmClientFactory};
$apiKey = 'your-api-key';
$secret = 'your-secret';
$token = $_GET['token'];
$auth = new AuthHelper($apiKey, $secret);
$sessionData = $auth->getSession($token);
$sessionKey = $sessionData['session']['key'];
$username = $sessionData['session']['name'];
// Store tokens for future use
$_SESSION['lastfm_session_key'] = $sessionKey;
$_SESSION['lastfm_username'] = $username;
$lastfm = LastFmClientFactory::createWithSession($apiKey, $secret, $sessionKey);
$user = $lastfm->getUserInfo();
echo "Hello " . $user['user']['name'];