Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kirilkirkov/Spotify-WebApi-PHP-SDK
Easy to use sdk with all the functionality of the Spotify Web Api through Guzzle with support of automated updates of access token and integrated pagination for results.
https://github.com/kirilkirkov/Spotify-WebApi-PHP-SDK
guzzle php-sdk sdk spotify spotify-api spotify-api-wrapper spotify-connect spotify-library spotify-php spotify-plugin spotify-sdk spotify-web-api spotify-web-api-laravel spotify-web-api-php spotify-web-sdk
Last synced: 3 months ago
JSON representation
Easy to use sdk with all the functionality of the Spotify Web Api through Guzzle with support of automated updates of access token and integrated pagination for results.
- Host: GitHub
- URL: https://github.com/kirilkirkov/Spotify-WebApi-PHP-SDK
- Owner: kirilkirkov
- License: mit
- Created: 2020-01-04T17:03:28.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2024-03-03T07:34:01.000Z (11 months ago)
- Last Synced: 2024-10-28T09:47:33.593Z (3 months ago)
- Topics: guzzle, php-sdk, sdk, spotify, spotify-api, spotify-api-wrapper, spotify-connect, spotify-library, spotify-php, spotify-plugin, spotify-sdk, spotify-web-api, spotify-web-api-laravel, spotify-web-api-php, spotify-web-sdk
- Language: PHP
- Homepage: https://developer.spotify.com/documentation/web-api/
- Size: 137 KB
- Stars: 15
- Watchers: 5
- Forks: 9
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
- awesome - kirilkirkov/Spotify-WebApi-PHP-SDK - Easy to use sdk with all the functionality of the Spotify Web Api through Guzzle with support of automated updates of access token and integrated pagination for results. (PHP)
README
# PHP SDK For Spotify Web Api
requires php >= 7.2
- Integrated Pagination
- Automated Token Refresh
- Separate Services Files For All Api References
- Guzzle Requests## Installation
composer require kirilkirkov/spotify-webapi-sdkExample usage with code:
https://github.com/kirilkirkov/Spotify-WebApi-PHP-SDK/wiki/Example-Usage-with-Code## Doesnt have token?
### Option 1 - Get access token with client credentials
```
use SpotifyWebAPI\SpotifyWebApi;try {
$spotifyWebApi = new SpotifyWebApi();
$token_obj = $spotifyWebApi->getAccessTokenWithCredentials(
'CLIENT_ID',
'CLIENT_SECRET'
);
echo $token_obj->access_token;
// echo $token_obj->token_type;
// echo $token_obj->expires_in;
} catch(\SpotifyWebAPI\SpotifyWebAPIException $e) {
echo $e->getMessage();
}
```### Option 2 - Get access token with code authorization (recommended)
Before make requests you must add yours Redirect URIs to https://developer.spotify.com/dashboardGet redirect url for code:
```
use SpotifyWebAPI\SpotifyWebApi;try {
$spotifyWebApi = new SpotifyWebApi([
'clientId' => 'CLIENT_ID',
'clientSecret' => 'CLIENT_SECRET',
]);$callBackUrl = 'http://yoursite.com/callback';
$url = $spotifyWebApi->getUrlForCodeToken($callBackUrl);
header("Location: {$url}");
} catch(\SpotifyWebAPI\SpotifyWebAPIException $e) {
echo $e->getMessage();
}
```After signup in spotify you will be redirected back to provided above callback url (http://yoursite.com/callback) with parameter **$_GET['code']** with the code that can get token with following command:
```
use SpotifyWebAPI\SpotifyWebApi;try {
$spotifyWebApi = new SpotifyWebApi();
$tokens = $spotifyWebApi->getAccessTokenWithCode(
'YOUR_CODE',
'http://yoursite.com/callback'
);
} catch(\SpotifyWebAPI\SpotifyWebAPIException $e) {
echo $e->getMessage();
}
```And you will receive array with *accessToken* and *refreshToken* in the example above **$tokens**.
### Access/Refresh Tokens
Spotify tokens are valid 1 hour. If your token is expired and you make a call, the sdk auto renew access token with provided refresh token in every query (as there is no safe place to automatically save it).If you set $spotifyWebApi->returnNewTokenIfIsExpired(true); before your request calls, if access token is expired will be returned from the query, object with the new access_token, then you can save it in database and recall request with a fresh Access token.
You can also generate access token with refresh token manually with
```
use SpotifyWebAPI\SpotifyWebApi;try {
$spotifyWebApi = new SpotifyWebApi([
'clientId' => 'CLIENT_ID',
'clientSecret' => 'CLIENT_SECRET',
'accessToken' => $oldAccessToken,
'refreshToken' => 'REFRESH_TOKEN',
]);
$result = $spotifyWebApi->refreshAccessToken();
} catch(\SpotifyWebAPI\SpotifyWebAPIException $e) {
echo $e->getMessage();
}
```and save final expire timestamp with time() + $result->expires_in. You can manualy generate new access token every time when saved in your database expired time is end.
### Suggestions
It is good practise to add ip of the api that you call in the hosts file in yours server os because Guzzle sometime cannot resolve the dns.
Can increase your execution time of scripts
ini_set('max_execution_time', XXX); and set_time_limit(XXX);### Functions
In the wiki of this repository you can find all functions available in this sdk (all the ones supported by Spotify have been integrated so far)
- https://github.com/kirilkirkov/Spotify-WebApi-PHP-SDK/wiki/Functions-and-examples
- https://github.com/kirilkirkov/Spotify-WebApi-PHP-SDK/wiki/Pagination Integrated Pagination Example## Donate
If this project help you reduce time to develop, you can give me a cup of coffee to continue its development. Thank you! :)
[![Donate](https://www.paypalobjects.com/en_US/i/btn/btn_donateCC_LG.gif)](https://www.paypal.com/donate/?hosted_button_id=7U9TVUV3URTK6)