sourceout/lastfm-php-sdk

LastFM SDK for PHP

1.0.0 2018-07-02 20:49 UTC

This package is not auto-updated.

Last update: 2024-09-20 08:15:46 UTC


README

Latest Version Software License Build Status Code Coverage Scrutinizer Code Quality

该仓库包含库,允许您从您的PHP应用程序访问Last.fm平台。

有关如何认证和获取API密钥的详细信息,请参阅API简介下的入门指南

https://www.last.fm/api/intro阅读官方API文档以获取更多信息。

功能

以下是该库提供的功能列表

  • 支持自定义实现(提供者)
  • 用于发送HTTP请求的库选择
  • 框架无关
  • 灵活且易于扩展

安装

您可以使用以下命令通过composer安装此包

composer require php-http/guzzle6-adapter guzzlehttp/psr7 php-http/message sourceout/lastfm-php-sdk

虽然默认安装说明推荐Guzzle Http客户端,但并非只能使用此客户端。有关客户端选择,请参阅以下列表php-http/client-implementation

有关此方法的更多信息,请参阅此文档

使用方法

use Sourceout\LastFm\ProviderInterface;
use Sourceout\LastFm\Client as LastFmClient;
use Sourceout\LastFm\Provider\LastFm\LastFm;

/** @var ProviderInterface $provider */
$provider = new LastFm(['api_key' => 'your_api_key_here']);

/** @var LastFmClient $lastFmClient */
$lastFmClient = new LastFmClient();

/** @var Collection $topArtists */
$topArtists = $lastFmClient
    ->getServiceFactory($provider)
    ->getGeoService()
    ->getTopArtists(
        'united states',    // location
        1,                  // page number
        50                  // results per page
    );

您还可以注册自己的自定义提供者而不是使用默认提供者,例如。

use Sourceout\LastFm\Client as LastFmClient;

$lastFmClient = new LastFmClient();

$lastFmClient->registerCustomProviders(
    [
        \path\to\custom\provider::class
        ...
        ...
    ]
);

此外,尽管该包具有自动发现http包/客户端的功能,但您也可以设置自己的HttpClient,以下示例展示了您提供Guzzle6实例的情况。

方法1

use Sourceout\LastFm\Http\Http;
use Sourceout\LastFm\Http\HttpInterface;
use Sourceout\LastFm\Client as LastFmClient;
use Http\Adapter\Guzzle6\Client as GuzzleClient;
use Http\Message\MessageFactory\GuzzleMessageFactory;

/** @var HttpInterface $http */
$http = new Http();

$http->setHttpClient(new GuzzleClient());
$http->setMessageFactory(new GuzzleMessageFactory());

/** @var LastFmClient $lastFmClient */
$lastFmClient = new LastFmClient($http);

方法2

use Sourceout\LastFm\Http\Http;
use Sourceout\LastFm\Http\HttpInterface;
use Sourceout\LastFm\Client as LastFmClient;
use Http\Adapter\Guzzle6\Client as GuzzleClient;
use Http\Message\MessageFactory\GuzzleMessageFactory;

/** @var HttpInterface $http */
$http = new Http();

$http->setHttpClient(new GuzzleClient());
$http->setMessageFactory(new GuzzleMessageFactory());

/** @var LastFmClient $lastFmClient */
$lastFmClient = new LastFmClient();
$lastFmClient->setHttpClient($http);

支持的方法

LastFm提供了大量API(参考:https://www.last.fm/api),以下是该库目前支持的方法列表

测试

您可以使用以下命令运行测试

vendor/bin/phpunit

贡献

有关详细信息,请参阅CONTRIBUTING

安全性

如果您发现任何安全问题,请通过电子邮件pulkit.swarup@gmail.com而不是使用问题跟踪器。

许可证

有关更多信息,请参阅许可证文件