metalmatze / lastfm-api-wrapper
dev-master
2014-03-15 22:32 UTC
Requires
- php: >=5.3.0
- kriswallsmith/buzz: dev-master
Requires (Dev)
- mockery/mockery: dev-master
- phpunit/phpunit: 3.7.*
This package is not auto-updated.
Last update: 2016-02-11 09:27:18 UTC
README
简介
当我寻找一个易于使用 Last.fm API 的 PHP 类时,我发现了 dandelionmood/lastfm。
遗憾的是我无法使用这个类进行测试。于是我开始修改它,并得到了这个版本。
为了避免在创建新实例时设置 API 密钥(这对依赖注入不好),我注入了 Buzz\Browser。要设置 API 密钥,请使用 setApiKey($key) 方法。
目前没有内置的身份验证。
关键思想是 官方文档 非常好,与 API 交互时不需要其他任何东西。
安装
您应该通过 Composer / Packagist 来安装它,因为 … 好吧,它很棒!
该包可在 Packagist 上找到。
标准方法
让我们深入这个项目的核心。首先,您需要 注册您的应用程序 以获取 API 密钥和密钥。
完成之后,以下是如何获取实例进行操作的方法
$lastfm = new \MetalMatze\LastFm\LastFm(new \Buzz\Browser);
$lastfm->setApiKey('73af4867ed96f8631cc43145c87d276e');
假设您想要获取某个艺术家的信息?如果您查看 API 文档,您会找到一个名为 artist.getInfo 的方法,它将为我们提供所需的信息(请见此处)。
// Note that the dot is replaced by an underscore in the PHP API.
$pink_floyd = $lastfm->artist_getInfo(
array(
'artist' => 'Pink Floyd'
)
);
您将得到的是一个标准的 PHP 对象。
结语
单元测试位于 tests/MetalMatze/LastFm/LastFmUnitTest.php 中,要运行它们,请使用 ./phpunit 启动 PHPUnit。