PHP code example of barryvanveen / lastfm

1. Go to this page and download the library: Download barryvanveen/lastfm 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/ */

    

barryvanveen / lastfm example snippets


php  artisan vendor:publish --provider="Barryvanveen\Lastfm\LastfmServiceProvider"

'providers' => [
    ...
    Barryvanveen\Lastfm\LastfmServiceProvider::class,
];

use Barryvanveen\Lastfm\Lastfm;
use GuzzleHttp\Client;
 
$lastfm = new Lastfm(new Client(), 'YourApiKey');
    
$albums = $lastfm->userTopAlbums('AnyUsername')->get();

use Barryvanveen\Lastfm\Lastfm;
 
public function index(Lastfm $lastfm)
{
    $albums = $lastfm->userTopAlbums('AnyUsername')->get();
    
    return view('home', compact('albums'));
}

// Get top albums for user
$albums = $lastfm->userTopAlbums('AnyUsername')->get();
 
// Get top artists for user
$artists = $lastfm->userTopArtists('AnyUsername')->get();
 
// Get recent tracks for user
$tracks = $lastfm->userRecentTracks('AnyUsername')->get();
 
// Get user info
$info = $lastfm->userInfo('AnyUsername')->get();
 
// Get track that user is now listening to, or FALSE
$trackOrFalse = $lastfm->nowListening('AnyUsername'); 
 
// Get the weekly top albums given a starting day 
$albums = $lastfm->userWeeklyTopAlbums('AnyUsername', new \DateTime('2017-01-01'));                      
 
// Get the weekly top artists given a starting day 
$artists = $lastfm->userWeeklyTopArtists('AnyUsername', new \DateTime('2017-01-01'));
 
// Get the weekly top tracks given a starting day 
$tracks = $lastfm->userWeeklyTopTracks('AnyUsername', new \DateTime('2017-01-01'));

// Define time period for results
$lastfm->userTopAlbums('AnyUsername')
       ->period(Barryvanveen\Lastfm\Constants::PERIOD_WEEK)
       ->get();
                  
// Limit number of results
$lastfm->userTopAlbums('AnyUsername')
       ->limit(5)
       ->get();     
                 
// Retrieve paginated results
$lastfm->userTopAlbums('AnyUsername')
       ->limit(5)
       ->page(2)
       ->get();     

// use these constants as an argument to ->period()
Barryvanveen\Lastfm\Constants::PERIOD_WEEK     = '7day';
Barryvanveen\Lastfm\Constants::PERIOD_MONTH    = '1month';
Barryvanveen\Lastfm\Constants::PERIOD_3_MONTHS = '3month';
Barryvanveen\Lastfm\Constants::PERIOD_6_MONTHS = '6month';
Barryvanveen\Lastfm\Constants::PERIOD_YEAR     = '12month';
Barryvanveen\Lastfm\Constants::PERIOD_OVERALL  = 'overall';