Question

I am trying to print out the album information for a certain artist and album using the last.fm api. I have used dump-autoload for the api library (so the classes should be available). In one of my controllers, LastFMController.php, I have the following:

public function some_function() {
        $authVars['apiKey'] = '************************';
        $auth = new lastfmApiAuth('setsession', $authVars);

        $artistName= "Coldplay";
        $albumName = "Mylo Xyloto";
        $album = Album::getInfo($artistName, $albumName);
        echo '<div>';
        echo 'Number of Plays: ' . $album->getPlayCount() . ' time(s)<br>';
        echo 'Cover: <img src="' . $album->getImage(4) . '"><br>';
        echo 'Album URL: ' . $album->getUrl() . '<br>';
        echo '</div>';

    }

I have a route that runs this code. When I run this, I get the following error:

Class 'Album' not found

Any idea what I'm doing wrong? Thank you.

Was it helpful?

Solution

You you are using this package: https://github.com/fxb/php-last.fm-api

You might have forgotten to autoload api classes:

require __DIR__ . "/src/lastfm.api.php";

Or you can add it to composer.json, as an example:

"autoload": {
    "files": [
        "/var/www/yourproject/libraries/lastfm.api/src"
    ],
},

And execute:

composer dump-autoload

EDIT:

You are using one package and the example from another one. There is no Album class in the package you are using, here is a full example of it:

<?php

// Include the API
require '../../lastfmapi/lastfmapi.php';

// Get the session auth data
$file = fopen('../auth.txt', 'r');
// Put the auth data into an array
$authVars = array(
        'apiKey' => trim(fgets($file)),
        'secret' => trim(fgets($file)),
        'username' => trim(fgets($file)),
        'sessionKey' => trim(fgets($file)),
        'subscriber' => trim(fgets($file))
);
$config = array(
        'enabled' => true,
        'path' => '../../lastfmapi/',
        'cache_length' => 1800
);
// Pass the array to the auth class to eturn a valid auth
$auth = new lastfmApiAuth('setsession', $authVars);

// Call for the album package class with auth data
$apiClass = new lastfmApi();
$albumClass = $apiClass->getPackage($auth, 'album', $config);

// Setup the variables
$methodVars = array(
        'artist' => 'Green day',
        'album' => 'Dookie'
);

if ( $album = $albumClass->getInfo($methodVars) ) {
        // Success
        echo '<b>Data Returned</b>';
        echo '<pre>';
        print_r($album);
        echo '</pre>';
}
else {
        // Error
        die('<b>Error '.$albumClass->error['code'].' - </b><i>'.$albumClass->error['desc'].'</i>');
}

?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top