PHP code example of matt-bartlett / php-spotify-api
1. Go to this page and download the library: Download matt-bartlett/php-spotify-api 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/ */
matt-bartlett / php-spotify-api example snippets
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)
);
});
}
}
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;
}
}
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);
}
}
}
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');
}
}
bash
$ composer
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.