LastFM
extends OAuthProvider
in package
implements
UserInfo
Last.fm
Tags
Table of Contents
Interfaces
- UserInfo
- Indicates whether the service can provide information about the currently authenticated user, usually via a "/me", "/user" or "/tokeninfo" endpoint.
Constants
- IDENTIFIER = 'LASTFM'
- PERIOD_12MONTH = '12month'
- PERIOD_1MONTH = '1month'
- PERIOD_3MONTH = '3month'
- PERIOD_6MONTH = '6month'
- PERIOD_7DAY = '7day'
- PERIOD_OVERALL = 'overall'
- PERIODS = [self::PERIOD_OVERALL, self::PERIOD_7DAY, self::PERIOD_1MONTH, self::PERIOD_3MONTH, self::PERIOD_6MONTH, self::PERIOD_12MONTH]
Properties
- $accessTokenURL : string
- The access token exchange URL
- $apiDocs : string|null
- An optional link to the provider's API docs
- $apiURL : string
- The API base URL
- $applicationURL : string|null
- An optional URL to the provider's credential registration/application page
- $authorizationURL : string
- The authorization URL
- $http : ClientInterface
- The PSR-18 HTTP client
- $logger : LoggerInterface
- A PSR-3 logger
- $name : string
- The name of the provider/class
- $options : OAuthOptions|SettingsContainerInterface
- The options instance
- $requestFactory : RequestFactoryInterface
- A PSR-17 request factory
- $revokeURL : string
- An optional URL for application side token revocation
- $scrobbles : array<int, array<string, scalar>>
- $storage : OAuthStorageInterface
- A storage instance
- $streamFactory : StreamFactoryInterface
- A PSR-17 stream factory
- $uriFactory : UriFactoryInterface
- A PSR-17 URI factory
- $userRevokeURL : string|null
- An optional link to the page where a user can revoke access tokens
Methods
- __construct() : mixed
- OAuthProvider constructor.
- addScrobble() : static
- Adds a track to scrobble
- clearScrobbles() : static
- getAccessToken() : AccessToken
- Obtains an authentication token
- getAccessTokenFromStorage() : AccessToken
- Gets an access token from the current OAuthStorageInterface (shorthand/convenience)
- getApiDocURL() : string|null
- Returns the link to the provider's API docs, or null if the value is not set
- getApplicationURL() : string|null
- Returns the link to the provider's credential registration/application page, or null if the value is not set
- getAuthorizationURL() : UriInterface
- Prepares the URL with optional $params which redirects to the provider's authorization prompt and returns a PSR-7 UriInterface with all necessary parameters set.
- getName() : string
- Returns the name of the provider/class
- getRequestAuthorization() : RequestInterface
- Authorizes the $request with the credentials from the given $token and returns a PSR-7 RequestInterface with all necessary headers and/or parameters set
- getStorage() : OAuthStorageInterface
- Returns the current OAuthStorageInterface
- getUserRevokeURL() : string|null
- Returns the link to the page where a user can revoke access tokens, or null if the value is not set
- InvalidateAccessToken() : bool
- implements TokenInvalidate
- me() : AuthenticatedUser
- Returns information about the currently authenticated user (usually a /me or /user endpoint).
- request() : ResponseInterface
- Prepares an API request to $path with the given parameters, gets authorization, fires the request and returns a PSR-7 ResponseInterface with the corresponding API response
- scrobble() : array<string|int, mixed>
- Scrobbles an array of one or more tracks
- sendRequest() : ResponseInterface
- setLogger() : static
- Sets an optional PSR-3 LoggerInterface
- setRequestFactory() : static
- Sets an optional PSR-17 RequestFactoryInterface
- setStorage() : static
- Sets an optional OAuthStorageInterface
- setStreamFactory() : static
- Sets an optional PSR-17 StreamFactoryInterface
- setUriFactory() : static
- Sets an optional PSR-17 UriFactoryInterface
- storeAccessToken() : static
- Sets an access token in the current OAuthStorageInterface (shorthand/convenience)
- addBasicAuthHeader() : RequestInterface
- Adds an "Authorization: Basic <base64(key:secret)>" header to the given request
- addSignature() : array<string, string>
- returns the signature for the set of parameters
- cleanBodyParams() : array<string, string>
- Cleans an array of body parameters
- cleanQueryParams() : array<string, string>
- Cleans an array of query parameters
- construct() : void
- A replacement constructor that you can call in extended classes, so that you don't have to implement the monstrous original `__construct()`
- createAccessToken() : AccessToken
- Creates an access token with the provider set to $this->name
- getAccessTokenRequestBodyParams() : array<string, scalar|bool|null>
- prepares the request body parameters for the access token request
- getAuthorization() : array<string, scalar|bool|null>
- adds the authorization parameters to the request parameters
- getMeResponseData() : array<int|string, mixed>
- fetches the provider's "me" endpoint and returns the JSON data as an array
- getRequestHeaders() : array<string, string>
- Prepare request headers
- getRequestTarget() : string
- Determine the request target from the given URI (path segment or URL) with respect to $apiURL, anything except host and path will be ignored, scheme will always be set to "https".
- getRequestURL() : string
- Prepares the request URL
- handleMeResponseError() : void
- handles errors for the `me()` endpoints - one horrible block of code to catch them all
- nonce() : string
- returns a 32 byte random string (in hexadecimal representation) for use as a nonce
- parseTokenResponse() : AccessToken
- parseTrack() : array<string|int, mixed>
- sendAccessTokenRequest() : ResponseInterface
- sends a request to the access token endpoint $url with the given $params as URL query
- sendMeRequest() : ResponseInterface
- prepares and sends the request to the provider's "me" endpoint and returns a ResponseInterface
- sendScrobbles() : array<string|int, mixed>|null
- setRequestBody() : RequestInterface
- Prepares the request body and sets it in the given RequestInterface, along with a Content-Length header
Constants
IDENTIFIER
public
mixed
IDENTIFIER
= 'LASTFM'
PERIOD_12MONTH
public
mixed
PERIOD_12MONTH
= '12month'
PERIOD_1MONTH
public
mixed
PERIOD_1MONTH
= '1month'
PERIOD_3MONTH
public
mixed
PERIOD_3MONTH
= '3month'
PERIOD_6MONTH
public
mixed
PERIOD_6MONTH
= '6month'
PERIOD_7DAY
public
mixed
PERIOD_7DAY
= '7day'
PERIOD_OVERALL
public
mixed
PERIOD_OVERALL
= 'overall'
PERIODS
public
mixed
PERIODS
= [self::PERIOD_OVERALL, self::PERIOD_7DAY, self::PERIOD_1MONTH, self::PERIOD_3MONTH, self::PERIOD_6MONTH, self::PERIOD_12MONTH]
Properties
$accessTokenURL
The access token exchange URL
protected
string
$accessTokenURL
= 'https://ws.audioscrobbler.com/2.0'
$apiDocs
An optional link to the provider's API docs
protected
string|null
$apiDocs
= 'https://www.last.fm/api/'
$apiURL
The API base URL
protected
string
$apiURL
= 'https://ws.audioscrobbler.com/2.0'
$applicationURL
An optional URL to the provider's credential registration/application page
protected
string|null
$applicationURL
= 'https://www.last.fm/api/account/create'
$authorizationURL
The authorization URL
protected
string
$authorizationURL
= 'https://www.last.fm/api/auth'
$http
The PSR-18 HTTP client
protected
ClientInterface
$http
$logger
A PSR-3 logger
protected
LoggerInterface
$logger
$name
The name of the provider/class
protected
string
$name
= ''
$options
The options instance
protected
OAuthOptions|SettingsContainerInterface
$options
$requestFactory
A PSR-17 request factory
protected
RequestFactoryInterface
$requestFactory
$revokeURL
An optional URL for application side token revocation
protected
string
$revokeURL
= ''
Tags
$scrobbles
protected
array<int, array<string, scalar>>
$scrobbles
= []
$storage
A storage instance
protected
OAuthStorageInterface
$storage
$streamFactory
A PSR-17 stream factory
protected
StreamFactoryInterface
$streamFactory
$uriFactory
A PSR-17 URI factory
protected
UriFactoryInterface
$uriFactory
$userRevokeURL
An optional link to the page where a user can revoke access tokens
protected
string|null
$userRevokeURL
= 'https://www.last.fm/settings/applications'
Methods
__construct()
OAuthProvider constructor.
public
final __construct(OAuthOptions|SettingsContainerInterface $options, ClientInterface $http, RequestFactoryInterface $requestFactory, StreamFactoryInterface $streamFactory, UriFactoryInterface $uriFactory[, OAuthStorageInterface $storage = new MemoryStorage() ][, LoggerInterface $logger = new NullLogger() ]) : mixed
Parameters
- $options : OAuthOptions|SettingsContainerInterface
- $http : ClientInterface
- $requestFactory : RequestFactoryInterface
- $streamFactory : StreamFactoryInterface
- $uriFactory : UriFactoryInterface
- $storage : OAuthStorageInterface = new MemoryStorage()
- $logger : LoggerInterface = new NullLogger()
addScrobble()
Adds a track to scrobble
public
addScrobble(array<string, scalar> $track) : static
Parameters
- $track : array<string, scalar>
Return values
staticclearScrobbles()
public
clearScrobbles() : static
Tags
Return values
staticgetAccessToken()
Obtains an authentication token
public
getAccessToken(string $session_token) : AccessToken
Parameters
- $session_token : string
Return values
AccessTokengetAccessTokenFromStorage()
Gets an access token from the current OAuthStorageInterface (shorthand/convenience)
public
final getAccessTokenFromStorage() : AccessToken
Tags
Return values
AccessTokengetApiDocURL()
Returns the link to the provider's API docs, or null if the value is not set
public
final getApiDocURL() : string|null
Tags
Return values
string|nullgetApplicationURL()
Returns the link to the provider's credential registration/application page, or null if the value is not set
public
final getApplicationURL() : string|null
Tags
Return values
string|nullgetAuthorizationURL()
Prepares the URL with optional $params which redirects to the provider's authorization prompt and returns a PSR-7 UriInterface with all necessary parameters set.
public
getAuthorizationURL([array<string|int, mixed>|null $params = null ][, array<string|int, mixed>|null $scopes = null ]) : UriInterface
If the provider supports RFC-9126 "Pushed Authorization Requests (PAR)", a request to the PAR endpoint shall be made within this method in order to send authorization data and obtain a temporary request URI.
Parameters
- $params : array<string|int, mixed>|null = null
- $scopes : array<string|int, mixed>|null = null
Return values
UriInterfacegetName()
Returns the name of the provider/class
public
final getName() : string
Tags
Return values
stringgetRequestAuthorization()
Authorizes the $request with the credentials from the given $token and returns a PSR-7 RequestInterface with all necessary headers and/or parameters set
public
getRequestAuthorization(RequestInterface $request[, AccessToken|null $token = null ]) : RequestInterface
Parameters
- $request : RequestInterface
- $token : AccessToken|null = null
Return values
RequestInterfacegetStorage()
Returns the current OAuthStorageInterface
public
final getStorage() : OAuthStorageInterface
Tags
Return values
OAuthStorageInterfacegetUserRevokeURL()
Returns the link to the page where a user can revoke access tokens, or null if the value is not set
public
final getUserRevokeURL() : string|null
Tags
Return values
string|nullInvalidateAccessToken()
implements TokenInvalidate
public
InvalidateAccessToken([AccessToken|null $token = null ]) : bool
Parameters
- $token : AccessToken|null = null
Tags
Return values
boolme()
Returns information about the currently authenticated user (usually a /me or /user endpoint).
public
me() : AuthenticatedUser
Tags
Return values
AuthenticatedUserrequest()
Prepares an API request to $path with the given parameters, gets authorization, fires the request and returns a PSR-7 ResponseInterface with the corresponding API response
public
request(string $path[, array<string|int, mixed>|null $params = null ][, string|null $method = null ][, StreamInterface|array<string|int, mixed>|string|null $body = null ][, array<string|int, mixed>|null $headers = null ][, string|null $protocolVersion = null ]) : ResponseInterface
Parameters
- $path : string
- $params : array<string|int, mixed>|null = null
- $method : string|null = null
- $body : StreamInterface|array<string|int, mixed>|string|null = null
- $headers : array<string|int, mixed>|null = null
- $protocolVersion : string|null = null
Return values
ResponseInterfacescrobble()
Scrobbles an array of one or more tracks
public
scrobble(array<string|int, mixed> $tracks) : array<string|int, mixed>
There is no limit for adding tracks, they will be sent to the API in chunks of 50 automatically. The return value of this method is an array that contains a response array for each 50 tracks sent, if an error happened, the element will be null.
Each track array may consist of the following values
- artist : [required] The artist name.
- track : [required] The track name.
- timestamp : [required] The time the track started playing, in UNIX timestamp format (UTC time zone).
- album : [optional] The album name.
- context : [optional] Sub-client version (not public, only enabled for certain API keys)
- streamId : [optional] The stream id for this track received from the radio.getPlaylist service, if scrobbling Last.fm radio (unavailable)
- chosenByUser: [optional] Set to 1 if the user chose this song, or 0 if the song was chosen by someone else (such as a radio station or recommendation service). Assumes 1 if not specified
- trackNumber : [optional] The track number of the track on the album.
- mbid : [optional] The MusicBrainz Track ID.
- albumArtist : [optional] The album artist - if this differs from the track artist.
- duration : [optional] The length of the track in seconds.
Parameters
- $tracks : array<string|int, mixed>
Tags
Return values
array<string|int, mixed>sendRequest()
public
final sendRequest(RequestInterface $request) : ResponseInterface
Parameters
- $request : RequestInterface
Tags
Return values
ResponseInterfacesetLogger()
Sets an optional PSR-3 LoggerInterface
public
final setLogger(LoggerInterface $logger) : static
Parameters
- $logger : LoggerInterface
Tags
Return values
staticsetRequestFactory()
Sets an optional PSR-17 RequestFactoryInterface
public
final setRequestFactory(RequestFactoryInterface $requestFactory) : static
Parameters
- $requestFactory : RequestFactoryInterface
Tags
Return values
staticsetStorage()
Sets an optional OAuthStorageInterface
public
final setStorage(OAuthStorageInterface $storage) : static
Parameters
- $storage : OAuthStorageInterface
Tags
Return values
staticsetStreamFactory()
Sets an optional PSR-17 StreamFactoryInterface
public
final setStreamFactory(StreamFactoryInterface $streamFactory) : static
Parameters
- $streamFactory : StreamFactoryInterface
Tags
Return values
staticsetUriFactory()
Sets an optional PSR-17 UriFactoryInterface
public
final setUriFactory(UriFactoryInterface $uriFactory) : static
Parameters
- $uriFactory : UriFactoryInterface
Tags
Return values
staticstoreAccessToken()
Sets an access token in the current OAuthStorageInterface (shorthand/convenience)
public
final storeAccessToken(AccessToken $token) : static
Parameters
- $token : AccessToken
Tags
Return values
staticaddBasicAuthHeader()
Adds an "Authorization: Basic <base64(key:secret)>" header to the given request
protected
addBasicAuthHeader(RequestInterface $request) : RequestInterface
Parameters
- $request : RequestInterface
Return values
RequestInterfaceaddSignature()
returns the signature for the set of parameters
protected
addSignature(array<string, string> $params) : array<string, string>
Parameters
- $params : array<string, string>
Return values
array<string, string>cleanBodyParams()
Cleans an array of body parameters
protected
cleanBodyParams(array<string, scalar|bool|null> $params) : array<string, string>
Parameters
- $params : array<string, scalar|bool|null>
Return values
array<string, string>cleanQueryParams()
Cleans an array of query parameters
protected
cleanQueryParams(array<string, scalar|bool|null> $params) : array<string, string>
Parameters
- $params : array<string, scalar|bool|null>
Return values
array<string, string>construct()
A replacement constructor that you can call in extended classes, so that you don't have to implement the monstrous original `__construct()`
protected
construct() : void
createAccessToken()
Creates an access token with the provider set to $this->name
protected
final createAccessToken() : AccessToken
Tags
Return values
AccessTokengetAccessTokenRequestBodyParams()
prepares the request body parameters for the access token request
protected
getAccessTokenRequestBodyParams(string $session_token) : array<string, scalar|bool|null>
Parameters
- $session_token : string
Return values
array<string, scalar|bool|null>getAuthorization()
adds the authorization parameters to the request parameters
protected
getAuthorization(array<string, scalar|bool|null> $params[, AccessToken|null $token = null ]) : array<string, scalar|bool|null>
Parameters
- $params : array<string, scalar|bool|null>
- $token : AccessToken|null = null
Return values
array<string, scalar|bool|null>getMeResponseData()
fetches the provider's "me" endpoint and returns the JSON data as an array
protected
final getMeResponseData(string $endpoint[, array<string, scalar|bool|null>|null $params = null ]) : array<int|string, mixed>
Parameters
- $endpoint : string
- $params : array<string, scalar|bool|null>|null = null
Tags
Return values
array<int|string, mixed>getRequestHeaders()
Prepare request headers
protected
final getRequestHeaders([array<string, string>|null $headers = null ]) : array<string, string>
Parameters
- $headers : array<string, string>|null = null
Return values
array<string, string>getRequestTarget()
Determine the request target from the given URI (path segment or URL) with respect to $apiURL, anything except host and path will be ignored, scheme will always be set to "https".
protected
getRequestTarget(string $uri) : string
Throws if the host of a given URL does not match the host of $apiURL.
Parameters
- $uri : string
Tags
Return values
stringgetRequestURL()
Prepares the request URL
protected
final getRequestURL(string $path[, array<string, scalar|bool|null>|null $params = null ]) : string
Parameters
- $path : string
- $params : array<string, scalar|bool|null>|null = null
Return values
stringhandleMeResponseError()
handles errors for the `me()` endpoints - one horrible block of code to catch them all
protected
final handleMeResponseError(ResponseInterface $response) : void
we could simply throw a ProviderException and be done with it, but we're nice and try to provide a message too
Parameters
- $response : ResponseInterface
Tags
nonce()
returns a 32 byte random string (in hexadecimal representation) for use as a nonce
protected
nonce([int $bytes = 32 ]) : string
Parameters
- $bytes : int = 32
Tags
Return values
stringparseTokenResponse()
protected
parseTokenResponse(ResponseInterface $response) : AccessToken
Parameters
- $response : ResponseInterface
Tags
Return values
AccessTokenparseTrack()
protected
parseTrack(array<string, scalar> $track) : array<string|int, mixed>
Parameters
- $track : array<string, scalar>
Tags
Return values
array<string|int, mixed>sendAccessTokenRequest()
sends a request to the access token endpoint $url with the given $params as URL query
protected
sendAccessTokenRequest(string $url, array<string, scalar|bool|null> $params) : ResponseInterface
Parameters
- $url : string
- $params : array<string, scalar|bool|null>
Return values
ResponseInterfacesendMeRequest()
prepares and sends the request to the provider's "me" endpoint and returns a ResponseInterface
protected
sendMeRequest(string $endpoint[, array<string|int, mixed>|null $params = null ]) : ResponseInterface
Parameters
- $endpoint : string
- $params : array<string|int, mixed>|null = null
Return values
ResponseInterfacesendScrobbles()
protected
sendScrobbles(array<string, string> $body) : array<string|int, mixed>|null
Parameters
- $body : array<string, string>
Tags
Return values
array<string|int, mixed>|nullsetRequestBody()
Prepares the request body and sets it in the given RequestInterface, along with a Content-Length header
protected
final setRequestBody(StreamInterface|array<string, scalar|bool|null>|string $body, RequestInterface $request) : RequestInterface
Parameters
- $body : StreamInterface|array<string, scalar|bool|null>|string
- $request : RequestInterface