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


// config/bundles.php

return [
    // ...
    Calliostro\LastFmClientBundle\CalliostroLastFmClientBundle::class => ['all' => true],
];

// src/Controller/SomeController.php

use LastFmClient\Service\Artist;
// ...

class SomeController
{
    public function index(Artist $artistService)
    {
        $artist = $artistService->getInfo('Cher');

        var_dump($artist->getData());

        // ...
    }
}

// src/Controller/SomeController.php

namespace App\Controller;

use LastFmClient;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

class SomeController extends AbstractController
{
    /**
     * @Route("/redirect")
     */
    public function redirectToLastFm(LastFmClient\Client $client)
    {
        $callbackUrl = $this->generateUrl('some_callback', [], UrlGeneratorInterface::ABSOLUTE_URL);
        $authUrl = $client->getAuthUrl($callbackUrl);

        return $this->redirect($authUrl);
    }

    /**
     * @Route("/callback", name="some_callback")
     */
    public function callbackFromLastFm(
        Request $request,
        LastFmClient\Auth $auth,
        LastFmClient\Service\Auth $authService
    ) {
        $token = $request->query->get('token');
        $auth->setToken($token);
        $sessionData = $authService->getSession()->getData();

        // You can store $sessionKey somewhere for later reuse
        $sessionKey = $sessionData['session']['key'];

        $auth->setSession($sessionKey);

        // Now you can use, for example, the LastFmClient\Service\Track service for scrobbling
        // ...
    }
}