Download the PHP package rennokki/larafy without Composer
On this page you can find all versions of the php package rennokki/larafy. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package larafy
This package is no longer maintained!
Larafy
Larafy is a PHP API Wrapper for Spotify API. This wrapper is more oriented over Client Credentials authenticated endpoints and provides great interface and and eloquent way to search for tracks, seed genres or simply just provide custom tracks/albums listings for your users.
Installation
Install the package:
If your Laravel version does not support package discovery, add this line in the providers
array in your config/app.php
file:
Setting up the API
For this, you will need an APP ID and an APP SECRET from your Spotify API Dashboard.
For a cleaner approach, add the following to your config/services.php
file:
To use the API, you just pass no construct parameters to Rennokki\Larafy\Larafy
:
If you want to change the credentials on-demand, you can do so by declaring a new API instance with your App ID and your App Secret:
Showcase
All requests throw exceptions, either if the data is invalid, either if the request is... bad. To do so, there are provided two exceptions that can be caught:
\Rennokki\Larafy\Exceptions\SpotifyAPIException
- thrown when the API is authenticated correctly, but the data passed is invalid.\Rennokki\Larafy\Exceptions\SpotifyAuthorizationException
- thrown when the API can't authenticate using credentials provided.
Since Spotify is related to music, it has some certain restrictions over markets and locales. This happens because, for example, some albums that are available in a country may not be available in other countries, such as the AC/DC
's The Razor Edge
album, that has different versions across countries like Austrialia
or the US
.
To help give a better search over the market and, respectively, locales, use the setMarket()
and setLocale()
methods within the API instance. These are by default set to US
and en_US
. These are optional.
You can chain them:
Or set them on demand:
Some requests need an offset
and a limit
, which are optional and set by default to 0
and respectively 10
. The limit is how many results can be shown in the request and the offset indicates how many results to offset, starting from the first. See the next examples on how you can use this feature.
When retrieving multiple data, you can either pass an array or a string with comma-separated values:
When issuing requests on endpoints that might return more data, usually this is done by accessing the items
property:
For the getArtistAlbums()
method, there is an option to search artist's albums based on the fact it is a single or the artist appears on that album. To do so, there is applied the same rule as the comma-separated values or array method.
API Usage
Recommendations
Recommendations allows you to query up the Spotify API into getting something personalized. The Recommendations endpoint allows you to do that by seeding tracks, genres and artists and a bunch of adjustable properties such as energy, key and danceability to get personalized tracks for your users.
Before getting into it, make sure you are familiar with the Recommendations Endpoint.
Each track has its own properties such as key, danceability, energy or loudness. To do so, the \Rennokki\Larafy\LarafySeed
class helps you build your seed easier. Let's start with an example:
In the previous example, we just have searched tracks from Death Metal, Hard Rock and Black Metal whose valence (the positivity; the higher, the more positive) is around 90.3
, and then we set some ranges for speechiness (the higher, the less musical it is: podcasts), loudness and liveness (the higher, the more chances it is a live performance).
To get the available seeds, you may use the $api->getGenreSeeds()
to get a list of possible values for the setGenres()
method.
There is a difference between:
set{property}($min, $max)
setTarget{property}($value)
.
When setting up a range, it might vary between two values. When setting up a target value, it just gets around it as much as possible. Read more about each seed here
To add artists, tracks or genres, you may use:
For tunable properties of the songs, you can use the following methods:
Note: While building your seed, you can chain how many properties you want.