rgjoni/spotify-web-api-sdk
Composer 安装命令:
composer require rgjoni/spotify-web-api-sdk
包简介
Spotify Web API SDK for PHP
README 文档
README
Spotify Web API SDK
An SDK for the Spotify Web API, covering all available endpoints and offering different authorization flows.
Reference link (external)Table of contents
- Supported API's
- Supported authorization flows
- Dependencies
- Installation
- Namespace
- Usage
- Errors & Exceptions
- Contributing
- License
Supported API's
- Search API
- Browse API
- Follow API
- Playlists API
- Library API
- Artists API
- Player API
- Markets API
- Personalization API
- User Profile API
- Albums API
- Tracks API
- Episodes API
- Shows API
Supported authorization flows
- Authorization Code
- Authorization Code with Proof Key for Code Exchange(PKCE)
- Client Credentials
Dependencies
- PHP 8.0 or higher
- Guzzle
Development dependencies
- PHPUnit
Installation
Using composer:
composer require rgjoni/spotify-web-api-sdk
Namespace
Gjoni\SpotifyWebApiSdk
Usage
First steps
Firstly, create a main Sdk object with the client credentials(client id, client secret), scopes and the redirect uri. For the credentials, you have to create an app on the developer dashboard.
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);
Authorization
After having created the main sdk object, these are the different ways to authorize your App to act on behalf of a user.
Authorization Code Flow
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!
Authorization Code Flow with Proof Key for Code Exchange(PKCE)
- Generate code verifier & challenge, persist the verifier and build the 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();
- After getting an authorization code on behalf of the end-user, seek access.
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!
Client Credentials Flow
require '/path/to/vendor/autoload.php'; use Gjoni\SpotifyWebApiSdk\Authorization; $authorization = new Authorization\ClientCredentials($sdk); # Directly seek access $accessToken = $authorization->requestAccessToken(); # That's it!
Persisting tokens
This SDK does not offer tokens persistence(access & refresh token), so that responsibility falls on the consuming project. In the examples in this README, cookie storage is used; but it's not the only possibility, read this & this(external links) for more information.
Web API Endpoints
Now that you have successfully authorized your App and persisted your tokens, now's the time to have some fun and request some user data!
Current user's profile
require '/path/to/vendor/autoload.php'; use Gjoni\SpotifyWebApiSdk\UsersProfile; $usersProfile = new UsersProfile($sdk); $usersProfile->me();
A public user's profile
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");
The latter outputs the following JSON response:
{
"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"
}
}
Get a single artist
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");
This outputs the following JSON response:
{
"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"
}
}
Errors & Exceptions
If you make an API request when the access token has expired, the following exception will be thrown:
Gjoni\SpotifyWebApiSdk\Exception\AccessTokenExpiredException
with the message:
The access token has expired, please refresh the token.
and the error code 401.
Other, usage errors, such as not providing the right parameter or the wrong amount of them, will cause
the GuzzleHttp\Exception\RequestException to bubble up to the top, necessitating to be handled
by the project.
Contributing
Pull requests and issues are welcome, please refer to CONTRIBUTORS.md
License
rgjoni/spotify-web-api-sdk 适用场景与选型建议
rgjoni/spotify-web-api-sdk 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4 次下载、GitHub Stars 达 10, 最近一次更新时间为 2021 年 05 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 rgjoni/spotify-web-api-sdk 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 rgjoni/spotify-web-api-sdk 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 4
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 10
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: GPL-3.0-only
- 更新时间: 2021-05-24