Download the PHP package matt-bartlett/php-spotify-api without Composer
On this page you can find all versions of the php package matt-bartlett/php-spotify-api. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download matt-bartlett/php-spotify-api
More information about matt-bartlett/php-spotify-api
Files in matt-bartlett/php-spotify-api
Package php-spotify-api
Short Description PHP Package for the Spotify Web API
License MIT
Homepage https://github.com/matt-bartlett/php-spotify-api
Informations about the package php-spotify-api
Spotify PHP Library
This is a framework agnostic PHP library for interacting with the Spotify Web API.
Install
Install using Composer
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:
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.
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.
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.
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
.