matt-bartlett/php-spotify-api
Composer 安装命令:
composer require matt-bartlett/php-spotify-api
包简介
PHP Package for the Spotify Web API
README 文档
README
This is a framework agnostic PHP library for interacting with the Spotify Web API.
Install
Install using Composer
$ composer require matt-bartlett/php-spotify-api
Head over to Spotify and create an application. You'll need the following for later use:
- Spotify Client ID
- Spotify Client Secret
- Redirect URL
Basic Usage
This library revolves around Resources. Resources encapsulate specific Spotify functionality. Session handling has been abstracted, and while this library ships with it's own session handlers, you are free to write your own and bind it to the implementation. I strongly advise using some sort of dependency container to configure this library. I will go through some examples.
Generic Example
If you're working in a plain PHP environment, then examples/vanilla_php_example.php should cover basic usage of the library.
Laravel Example
This library ships with a LaravelSessionHandler. This class abstracts the session handler baked into Laravel, allowing you to use the same session driver controlling your application. As Laravel comes with it's own dependency container, we won't have to provide as much configuration. We will however, need to resolve the concrete implementations. We can do that using a service provider.
Create a new service provider within App\Provider and paste the following:
<?php namespace App\Providers; use Spotify\Http\Request; use Illuminate\Session\Store; use Spotify\Auth\Credentials; use Spotify\Auth\Authenticator; use Spotify\Contracts\Store\Session; use Illuminate\Support\ServiceProvider; use Spotify\Sessions\LaravelSessionHandler; use Spotify\Contracts\Auth\Authenticator as AuthenticatorInterface; class SpotifyServiceProvider extends ServiceProvider { /** * Register any application services. * * @return void */ public function register() { // Bind Credentials. $this->app->singleton(Credentials::class, function ($app) { return new Credentials( getenv('SPOTIFY_CLIENT_ID'), getenv('SPOTIFY_CLIENT_SECRET'), getenv('SPOTIFY_REDIRECT_URL') ); }); // Bind Authenticator. $this->app->bind(AuthenticatorInterface::class, function ($app) { return new Authenticator( $app->make(Request::class), $app->make(Credentials::class) ); }); // Bind the Laravel Session handler. $this->app->bind(Session::class, function ($app) { return new LaravelSessionHandler( $app->make(Store::class) ); }); } }
Next, in your config/app.php, be sure to add this service provider to the providers array.
We'll need to add our Spotify credentials to our .env file. Replace the values and paste it in.
SPOTIFY_CLIENT_ID=client-id
SPOTIFY_CLIENT_SECRET=client-secret
SPOTIFY_REDIRECT_URL=your-redirect-url
With everything configured, let's run through some examples.
Client Credential Flow
The Client Credentials authorization flow doesn't require any user authorization in order to generate an access token. Typically, this access token only allows fetching/reading actions, such as finding a Playlist and all it's tracks.
<?php namespace App\Http\Controllers; use Spotify\Resources\Playlist; class ExampleController extends Controller { protected $service; public function __construct(Playlist $service) { $this->service = $service; } public function show(Request $request) { $playlist = $this->service->getPlaylist($request->get('playlist-id')); return $playlist; } }
Authorization Code Flow
Actions such as creating a Playlist will need user authorization. In order to obtain to an access token, we must first ask the user to authorize and agree to the actions we wish to perform. Upon confirming, they will be redirected to the redirect_url set on the Spotify\Auth\Credentials class, along with a code we can exchange for an access token.
To demonstrate, I'll add 2 routes to our application. The first route will handle the redirection from Spotify. The second route will create a Playlist.
Route::get('/spotify/redirect', 'SpotifyController@redirect'); Route::post('/spotify/playlist/create', 'SpotifyController@playlist');
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Spotify\Constants\Scope; use Spotify\Resources\Playlist; use Spotify\Exceptions\UserRequiresAuthorizationException; class SpotifyController extends Controller { protected $service; public function __construct(Playlist $service) { $this->service = $service; } /** * @param \Illuminate\Http\Request $request * * @return Illuminate\Http\RedirectResponse */ public function redirect(Request $request) { $code = $request->get('code'); $error = $request->get('error'); if ($error) { // Do something when an error is returned. } // Exchange code for access token. $this->service->getManager()->handleCallback($code); // Redirect the user back to the `playlist` route. $redirect = $request->session()->get('redirect', '/an/alternative/endpoint'); // Let's clean up after ourselves, remove it from the session. $request->session()->forget('redirect'); return redirect()-to($redirect); } /** * @param \Illuminate\Http\Request $request * * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse */ public function playlist(Request $request) { try { // Get the Playlist name from the request. $name = $request->get('playlist-name'); // Create a Playlist with the given name. $playlist $this->service->createPlaylist($name)); return response()->json($playlist); } catch (UserRequiresAuthorizationException $e) { // Generate authorization URL. We'll need the `playlist-modify-public` scope to make a playlist. $url = $this->service->getManager()->getAuthorizationUrl([Scope::PLAYLIST_MODIFY_PUBLIC], true); $currentRoute = url()->full(); // To make this a bit more seamless, we'll set the intended URL to the session. $request->session()->put(['redirect' => $currentRoute]); return redirect()->to($url); } } }
You may have a dedicated controller that handles redirection. This wouldn't necessarily tie in with a particular Resource. In this event, you can use the Manager.
<?php namespace App\Http\Controllers; use Spotify\Manager; use Spotify\Constants\Scope; use Illuminate\Http\Request; use Spotify\Exceptions\UserRequiresAuthorizationException; class SpotifyRedirectController extends Controller { protected $manager; public function __construct(Manager $manager) { $this->manager = $manager; } /** * @param \Illuminate\Http\Request $request * * @return Illuminate\Http\RedirectResponse */ public function redirect(Request $request) { $code = $request->get('code'); $error = $request->get('error'); if ($error) { // Do something when an error is returned. } // Exchange code for access token. $this->manager->handleCallback($code); return redirect('route.name'); } }
matt-bartlett/php-spotify-api 适用场景与选型建议
matt-bartlett/php-spotify-api 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 53 次下载、GitHub Stars 达 3, 最近一次更新时间为 2019 年 11 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「spotify」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 matt-bartlett/php-spotify-api 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 matt-bartlett/php-spotify-api 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 matt-bartlett/php-spotify-api 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Easy Spotify Api interface for Symfony2 projects
PHP Package for the (unofficial) Spotify Artists API
PHP Package for logging in to Spotify
Spotify WebApi PHP Library
Alfabank REST API integration
Larafy is a Laravel API wrapper for Spotify.
统计信息
- 总下载量: 53
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 8
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-11-03