0

我正在使用一个名为 MetaTune 的 Spotify 库,并且能够在 CodeIgniter 中轻松地做到这一点,但是在 Yii 中出现了一些初期问题,但目前它已经开始说:

Fatal error: Call to undefined method stdClass::searchTrack() in ....public_html/Yii/news/protected/controllers/NewsController.php on line 67

但是,功能在那里。这个库中的文件都有一个.class.php后缀(例如MetaTune.class.php)并且库文件都存储在:

yii/application/protected/vendors/Metatune

使用 Codeigniter,我在文件夹之外创建了一个额外的 spotify.php 并将其自动加载到我的控制器,但我不确定这是否有必要。

我已将其加载到我的config.php中:

    'import'=>array(
    'application.models.*',
    'application.components.*',
            'application.vendors.metatune.*',
),

这是控制器代码:

public function actionView($id)
{
 $model=$this->loadModel($id);

$spotify = MetaTune::getSomething();
$hello =  $model->title;
Yii::import('application.vendors.metatune.MetaTune');
$spotify->autoAddTracksToPlayButton = true; // Will add all searches for tracks into a list.
$spotify->playButtonHeight = 330; // For viewing the entire playlist
$spotify->playButtonTheme = "dark"; // Changing theme
$spotify->playButtonView = "coverart"; // Changing view
try
{
    $tracks = $spotify->searchTrack($hello);
    $tracks = $spotify->getPlayButtonAutoGenerated($hello);
}
catch (MetaTuneException $ex)
{
    die("<pre>Error\n" . $ex . "</pre>");
}

$song = 'tracks';

    $this->render('view',array(
        'model'=>$this->loadModel($id),
    ));
}

另请参阅下面的代码,其中有一个名为getInstance的函数,由于某种原因,它不能与 Yii 很好地配合,我不确定是否可以更改它,因为我使用它将 MetaTune 导入 CodeIgniter 控制器而没有任何问题。

只是MetaTune.class.php代码的一部分:

 Yii::import('application.vendors.metatune.Artist');
 Yii::import('application.vendors.metatune.Album');
 Yii::import('application.vendors.metatune.Track');
 Yii::import('application.vendors.metatune.CacheRequest');
 Yii::import('application.vendors.metatune.MBSimpleXMLElement');
 Yii::import('application.vendors.metatune.SpotifyItem');
 Yii::import('application.vendors.metatune.MetaTuneException');

 ....

 class MetaTune {
const CACHE_DIR = 'application/vendors/metatune/cache/'; // Cache directory (must be writable) relative to this file
const USE_CACHE = false; // Should caching be activated?
const CACHE_PREFIX = "METATUNE_CACHE_"; // prefix for cache-files. 

const SERVICE_BASE_URL_SEARCH = "http://ws.spotify.com/search/1/";
const SERVICE_BASE_URL_LOOKUP = "http://ws.spotify.com/lookup/1/";
const PLAYBUTTON_BASE_URL = "https://embed.spotify.com/?uri=";

public $autoAddTracksToPlayButton = false;

private $list = array();

// Holds instance
private static $instance;

.....

public static function getSomething()
{
    if (!isset(self::$instance))
    {
        $class = __CLASS__;
        self::$instance = new $class;
    }

    return self::$instance;
}

.....

 public function searchTrack($name, $page = 1)
{
    $url = self::SERVICE_BASE_URL_SEARCH . "track?q=" . $this->translateString($name) .  
    $this->addPageSuffix($page);
    $contents = $this->requestContent($url);
    $xml = new MBSimpleXMLElement($contents);

    $tracks = array();
    foreach ($xml->track as $track)
    {
        $tracks[] = $this->extractTrackInfo($track);
    }

    if ($this->autoAddTracksToPlayButton) {
        $this->appendTracksToTrackList($tracks);
    }

    return $tracks;
    } 

如果您有任何建议,我将不胜感激。谢谢。

4

1 回答 1

0

您没有$spotify在任何地方初始化,并且 phpstdClass默认情况下将其设置为,因为您正在使用该变量为成员属性分配值,但是当您尝试对其调用不存在的方法时它失败了。

解决方法:使用前先初始化

$spotify = MetaTune::getInstance();

于 2013-03-18T19:51:14.577 回答