rgjoni/spotify-web-api-sdk

Spotify Web API SDK for PHP

v1.1.0 2021-11-03 15:57 UTC

This package is auto-updated.

Last update: 2024-09-29 05:56:34 UTC


README

GitHub GitHub packagist GitHub

Spotify Web API SDK

Spotify Web API的SDK,涵盖了所有可用端点,并提供不同的授权流程。

参考链接 (外部链接)

目录

  1. 支持的API
  2. 支持的授权流程
  3. 依赖项
    1. 开发依赖项
  4. 安装
  5. 命名空间
  6. 用法
    1. 第一步
    2. 授权
      1. 授权码流
      2. 带有代码交换证明密钥的授权码流(PKCE)
      3. 客户端凭证流
      4. 持久化令牌
    3. Web API端点
  7. 错误 & 异常
  8. 贡献
  9. 许可证

支持的API

  • 搜索API
  • 浏览API
  • 关注API
  • 播放列表API
  • 库API
  • 艺术家API
  • 播放器API
  • 市场API
  • 个性化API
  • 用户资料API
  • 专辑API
  • 曲目API
  • 剧集API
  • 节目API

支持的授权流程

  • 授权码
  • 带有代码交换证明密钥的授权码(PKCE)
  • 客户端凭证

依赖项

  • PHP 8.0 或更高版本
  • Guzzle

开发依赖项

  • PHPUnit

安装

使用composer

composer require rgjoni/spotify-web-api-sdk

命名空间

Gjoni\SpotifyWebApiSdk 

用法

第一步

首先,使用客户端凭证(客户端ID、客户端密钥)、作用域和重定向URI创建一个主Sdk对象。对于凭证,您必须在 开发者仪表板 上创建一个应用。

require '/path/to/vendor/autoload.php';

use Gjoni\SpotifyWebApiSdk;

# The scopes your app would need.
$scopes = [
    "user-read-private",
    "user-read-email",
    "playlist-read-private",
    "playlist-modify-public",
    "playlist-modify-private",
    "ugc-image-upload",
];
$redirectUri = "https://my.frontend.io/redirect";

$sdk = new SpotifyWebApiSdk\Sdk("clientId", "clientSecret", $scopes, $redirectUri);

授权

创建主sdk对象后,以下是授权您的应用程序代表用户执行操作的几种不同方式。

授权码流

require '/path/to/vendor/autoload.php';

use Gjoni\SpotifyWebApiSdk\Authorization;

$authorization = new Authorization\AuthorizationCode($sdk);

# Build the URL and have your frontend make a GET request to it.
$url = $authorization->buildUrl();

# After this, you should have an authorization code; use it in another request
# to seek access to user resources.
$accessToken = $authorization->requestAccessToken("auth_code");

# When the access token expires, just refresh it
$newAccessToken = $authorization->refreshAccessToken("refresh_token");

# That's it!

带有代码交换证明密钥的授权码流(PKCE)

  1. 生成代码验证器 & 挑战,持久化验证器并构建URL。
require '/path/to/vendor/autoload.php';

use Gjoni\SpotifyWebApiSdk\Authorization;

$authorization = new Authorization\AuthorizationCodePKCE($sdk);

# Generate a code verifier and a code challenge based on it
$verifier = $authorization->generateCodeVerifier();
$challenge = $authorization->generateCodeChallenge($verifier);

# Set the code challenge, it will be needed in the next step
$authorization->setCodeChallenge($challenge);

# Have some way to persist the code verifier, it will be needed in the second request.
$cookie = new Cookie($this->cookieConfig);

if (empty($cookie->code_verifier)) {
    $cookie->code_verifier = $verifier;
}

# Build the URL and have your frontend make a GET request to it.
$url = $authorization->buildUrl();
  1. 在代表最终用户获得授权码后,寻求访问权限。
use Gjoni\SpotifyWebApiSdk\Authorization;

$cookie = new Cookie($this->cookieConfig);
$authorization = new Authorization\AuthorizationCodePKCE($this->sdk);

# Get the code verifier from your persistence method, in this case, a cookie
if (!empty($_COOKIE["code_verifier"])) {
    $authorization->setCodeVerifier($_COOKIE["code_verifier"]);
}

# Get your authorization code
$authCode = "my-auth-code";

# Request access
$accessToken = $authorization->requestAccessToken($authCode);

# When needed, refresh access
$newAccessToken = $authorization->refreshAccessToken("refresh-token");

# That's it!

客户端凭证流

require '/path/to/vendor/autoload.php';

use Gjoni\SpotifyWebApiSdk\Authorization;

$authorization = new Authorization\ClientCredentials($sdk);

# Directly seek access
$accessToken = $authorization->requestAccessToken();

# That's it!

持久化令牌

此SDK不提供令牌持久性(访问 & 刷新令牌),因此责任落在使用该SDK的项目上。在本README中的示例中,使用了cookie存储;但这不是唯一的选择,请阅读 & (外部链接) 了解更多信息。

Web API端点

现在您已成功授权您的应用程序并持久化您的令牌,现在是时候尽情享受并请求一些用户数据了!

当前用户的资料

require '/path/to/vendor/autoload.php';

use Gjoni\SpotifyWebApiSdk\UsersProfile;

$usersProfile = new UsersProfile($sdk);

$usersProfile->me();

公共用户的资料

require '/path/to/vendor/autoload.php';

use Gjoni\SpotifyWebApiSdk\UsersProfile;

$usersProfile = new UsersProfile($sdk);

# For a user with the id of 'wizzler'
$usersProfile->getUserProfile("wizzler");

后者输出以下JSON响应

{
  "data": {
    "display_name": "Ronald Pompa",
    "external_urls": {
      "spotify": "https://open.spotify.com/user/wizzler"
    },
    "followers": {
      "href": null,
      "total": 4032
    },
    "href": "https://api.spotify.com/v1/users/wizzler",
    "id": "wizzler",
    "images": [
      {
        "height": null,
        "url": "https://i.scdn.co/image/ab6775700000ee85b5d374d281b9e510eda15fdf",
        "width": null
      }
    ],
    "type": "user",
    "uri": "spotify:user:wizzler"
  }
}

获取单个艺术家

require '/path/to/vendor/autoload.php';

use Gjoni\SpotifyWebApiSdk\Artists;

$artists = new Artists($sdk);

# For an artist with the id of '0OdUWJ0sBjDrqHygGUXeCF'(Band of Horses)
$artists->getSingle("0OdUWJ0sBjDrqHygGUXeCF");

这会输出以下JSON响应

{
  "data": {
    "external_urls": {
      "spotify": "https://open.spotify.com/artist/0OdUWJ0sBjDrqHygGUXeCF"
    },
    "followers": {
      "href": null,
      "total": 873614
    },
    "genres": [
      "indie folk",
      "indie pop",
      "indie rock",
      "modern rock",
      "stomp and holler"
    ],
    "href": "https://api.spotify.com/v1/artists/0OdUWJ0sBjDrqHygGUXeCF",
    "id": "0OdUWJ0sBjDrqHygGUXeCF",
    "images": [
      {
        "height": 640,
        "url": "https://i.scdn.co/image/0f9a5013134de288af7d49a962417f4200539b47",
        "width": 640
      },
      {
        "height": 320,
        "url": "https://i.scdn.co/image/8ae35be1043f330173de198c35a49161337e829c",
        "width": 320
      },
      {
        "height": 160,
        "url": "https://i.scdn.co/image/602dd7b3a2ee3f3fd86c6c4f50ab9b5a82e23c59",
        "width": 160
      }
    ],
    "name": "Band of Horses",
    "popularity": 65,
    "type": "artist",
    "uri": "spotify:artist:0OdUWJ0sBjDrqHygGUXeCF"
  }
}

错误 & 异常

如果您在访问令牌过期时发出API请求,将抛出以下异常

Gjoni\SpotifyWebApiSdk\Exception\AccessTokenExpiredException

消息

The access token has expired, please refresh the token.

错误代码 401

其他使用错误,例如未提供正确的参数或参数数量不正确,将导致 GuzzleHttp\Exception\RequestException 向上冒泡,需要项目处理。

贡献

欢迎提交拉取请求和问题,请参阅 CONTRIBUTORS.md

许可证

GPLv3