对 Last.fm API 的简单封装。

v0.8 2019-06-29 09:34 UTC

This package is auto-updated.

Last update: 2024-08-29 03:21:13 UTC


README

介绍

当我寻找一个可以与 Last.fm API 一起工作的 API 时,我只能找到不完整的实现或非常复杂的实现。我喜欢简单的事物,所以我决定抓住机会,设计一个以最简单的方式完成所有事情的简单类。

关键思想是官方文档非常好,你不需要其他任何东西来与 API 一起工作。

安装

你应该通过 Composer / Packagist 来安装它,因为…… 嗯,它很棒 !

该包可在Packagist 这里找到。

标准方法(不需要身份验证)

让我们深入了解这个项目的核心。首先,你需要注册你的应用程序以获取 API 密钥和密钥。

一旦完成,下面是如何获取一个实例来使用

// The secret is only needed if you want to access authenticated methods
$lastfm = new \Dandelionmood\LastFm\LastFm( $lastfm_api_key, $lastfm_api_secret );

现在假设你想获取某个艺术家的信息?如果你查看 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 对象。

身份验证方法

某些方法需要你首先对用户进行身份验证。PHP API 提供了两种方法来做到这一点。这与 OAuth 和 OpenID 身份验证非常相似,所以如果你以前实现过,你应该感到很自在。

身份验证

请查看 examples/authentication.php 文件以找到实现它的 Slim 应用程序。我将在这里使用该文件的某些部分来一步步引导你。

首先,我们需要让用户允许我们的应用程序,这是由 Last.fm 处理的;他们需要知道当用户说“是”时应该调用什么 URL

$app->get('/auth/connect', function() use($app) {
	$lastfm = new LastFm( LASTFM_API_KEY, LASTFM_API_SECRET );
	
	// We need to compute a callback URL that will be called when the user
	// allows the application.
	$callback_url = $lastfm->auth_get_url(
		$app->request()->getUrl()
			.$app->urlFor('auth_callback')
	);
	
	$app->redirect( $callback_url );
});

其次,我们需要处理当用户验证表单时将被调用的回调 URL

$app->get('/auth/callback', function() use($app) {
	
	$lastfm = new LastFm( LASTFM_API_KEY, LASTFM_API_SECRET );
	$token = $app->request()->get('token');
	
	try {	
		// We try to get a session using the token we're given
		$session = $lastfm->auth_get_session( $token );
		echo "Yes ! The session key is : $session->session->key";
	} catch( Exception $e ) {
		echo "Sorry, something went wrong : ".$e->getMessage();
	}
	
})->name('auth_callback');

我决定打印出会话密钥,但你应该将其保存在数据库或 $_SESSION 变量中:YMMV …

身份验证方法

用户现在已经通过身份验证,我们现在知道它的 session_key;我们可以将其用作构造函数的第三个参数。

$lastfm = new LastFm( LASTFM_API_KEY, LASTFM_API_SECRET, $session_key );

这是一个简单的身份验证方法,它使用 URL 中的 session_id 并在我的墙上发布一条消息

$app->get('/shout/:session_key', function($session_key) use($app) {

	// This time, note that we pass a third parameter, which is the session
	// key. This will allow us to call methods that need authentication.
	$lastfm = new LastFm( LASTFM_API_KEY, LASTFM_API_SECRET, $session_key );
	
	// We try to publish something on my wall.
	// Note the «true» in the last parameter, this tells the class that it's
	// a call that need authentication (session_key + signature are added).
	$r = $lastfm->user_shout(array(
		'user' => 'dandelionmood',
		'message' => "I just installed your Last.fm API wrapper :) !"
	), true);
	
	// We print a message to let know everything worked out allright !
	echo "A message has been successfully posted to 'dandelionmood' wall :) !<br/>";
	echo '<code>'.print_r($r, true).'</code>';
	
})->name('shout');

你需要调用 shout() 函数时添加一个第三个参数,让 API 知道这是一个身份验证调用。

结束语

你可以使用 ./apigen 命令生成类文档。

目前单元测试非常少,但如果你指定自己的 API 密钥和密钥,它们应该会工作,请查看 tests/LastFmTest.php。你可以通过调用 ./phpunit 命令来启动它们。