PHP code example of rgjoni / spotify-web-api-sdk

1. Go to this page and download the library: Download rgjoni/spotify-web-api-sdk library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

rgjoni / spotify-web-api-sdk example snippets




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);




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!




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();



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!




use Gjoni\SpotifyWebApiSdk\Authorization;

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

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

# That's it!




use Gjoni\SpotifyWebApiSdk\UsersProfile;

$usersProfile = new UsersProfile($sdk);

$usersProfile->me();




use Gjoni\SpotifyWebApiSdk\UsersProfile;

$usersProfile = new UsersProfile($sdk);

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




use Gjoni\SpotifyWebApiSdk\Artists;

$artists = new Artists($sdk);

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