我正在使用一个名为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 ),并且libray文件都存储在:
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;
}
如果您有任何建议,我将非常感激。感谢。
答案 0 :(得分:0)
您没有在任何地方初始化$spotify
,并且默认情况下php已将其设置为stdClass
,因为您使用该变量为成员属性分配了值,但是当您尝试调用未显示的方法时它失败了在它上面。
解决方案:在使用之前将其初始化
$spotify = MetaTune::getInstance();