Skip to content

Latest commit

 

History

History
76 lines (51 loc) · 3.73 KB

getting-started.md

File metadata and controls

76 lines (51 loc) · 3.73 KB

Getting Started

Requirements

Autoloading

The Spotify Web API for PHP is compatible with PSR-4. This means that the code makes heavy use of namespaces and the correct files can be loaded automatically. All examples throughout this documentation will assume the use of a PSR-4 compatible autoloader, for example via Composer.

Installation

Installation via Composer

This is the preferred way of installing the Spotify Web API for PHP. Run the following command in the root of your project:

composer require jwilsson/spotify-web-api-php

Then, in every file where you wish to use the Spotify Web API for PHP, include the following line:

require_once 'vendor/autoload.php';

Manual installation

Download the latest release from the releases page. Unzip the files somewhere in your project and include a PSR-4 compatible autoloader in your project.

Configuration and setup

First off, make sure you've created an app on Spotify's developer site.

Note: Applications created after 2021-05-27 might need to perform some extra steps.

Now, before sending requests to Spotify, we need to create a session using your app info:

$session = new SpotifyWebAPI\Session(
    'CLIENT_ID',
    'CLIENT_SECRET',
    'REDIRECT_URI'
);

Replace the values here with the ones given to you from Spotify.

Authentication and authorization

After creating a session it's time to request access to the Spotify Web API. There are three ways to request an access token.

The first method is called Proof Key for Code Exchange (PKCE) and requires some interaction from the user, but in turn gives you some access to the user's account. This is the recommended method if you need access to a user's account.

The second method is called the Authorization Code Flow and just like the PKCE method it requires some interaction from the user, but will also give you access to the user's account.

The last method is called the Client Credentials Flow and doesn't require any user interaction but also doesn't provide any user information. This method is the recommended one if you just need access to Spotify catalog data.

For more info about each authorization method, checkout these examples:

Making requests to the Spotify API

Assuming you've followed one of the authorization guides above and successfully requested an access token, now it's time to create a new file called app.php. In this file we'll tell the API wrapper about the access token to use and then request some data from Spotify!

require 'vendor/autoload.php';

// Fetch your access token from somewhere. A session for example.

$api = new SpotifyWebAPI\SpotifyWebAPI();
$api->setAccessToken($accessToken);

print_r(
    $api->getTrack('4uLU6hMCjMI75M1A2tKUQC')
);

Congratulations! You now know how to use the Spotify Web API for PHP. The next step is to check out some examples and the method reference.