snapshotpl/last-fm-client

此包最新版本(1.0)没有可用的许可证信息。

1.0 2015-01-06 15:44 UTC

This package is auto-updated.

Last update: 2024-08-29 03:36:48 UTC


README

用于 PHP 的现代 Last.fm API 客户端

Last.fm API 文档

用法

您可以使用现有服务调用 API 方法

<?php

require __DIR__ . '/vendor/autoload.php';

$auth = new LastFmClient\Auth();
$auth->setApiKey('your-api-key');
$auth->setSecret('your-secret');
$auth->setToken('user-token');
$auth->setSession('user-session');

$transport = new LastFmClient\Transport\Curl();

$client = new LastFmClient\Client($auth, $transport);

$trackService = new LastFmClient\Service\Track();
$trackService->setClient($client);

$response = $trackService->getInfo('Numb', 'Linkin Park');

var_dump($response->getData());

如果您想调用自定义方法,请使用 LastFmClient\Client

$client->call('resource.getAwesomeness', [], LastFmClient\Transport\TransportInterface::METHOD_GET);

如何 scrobble?

非常简单!准备带有 API 密钥、秘密、令牌和会话密钥的 LastFmClient\Auth 对象。然后只需调用 LastFmClient\Service\Track 中的方法即可。

$trackService->scrobble('Seven Lions', 'Days to Come', $timestamp);

$timestamp 是可选的,可以是整数时间戳或 DateTime 对象。

您可以在一个请求中 scrobble 多个曲目

$trackService->scrobbleBatch([
    [
        'artist' => 'Linkin Park',
        'track' => 'Numb',
        'timestamp' => time()-1000,
    ],
    [
        'artist' => 'Seven Lions',
        'track' => 'Days to Come',
        'timestamp' => time()-1200,
    ],
]);

如何获取 TokenSession

您需要将用户重定向到 Last.Fm 的授权页面

$url = $client->getAuthUrl();
header('Location: '.$url);

在回调 URL 中,您将收到查询字符串参数 token

$authService = LastFmClient\Service\Auth();
$authService->setClient($client);
$data = $authService->getSession()->getData();
var_dump($data);

更多信息

传输

为了在 API 中进行调用,我们提供简单的 CURL 传输。我们计划实现其他传输,例如

  • Guzzle
  • Httpfull
  • Zend\Http

要使用自己的传输,只需实现 LastFmClient\Transport\TransportInterface

安装

添加到 composer.json

{
    "require": {
        "snapshotpl/last-fm-client": "~1.0"
    }
}