Skip to content

Commit

Permalink
Merge branch 'hvt-consistent_function_names'
Browse files Browse the repository at this point in the history
  • Loading branch information
jwilsson committed Mar 22, 2015
2 parents b608ca0 + c786d67 commit d68ee32
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 55 deletions.
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ $user = $api->getUser('username');
print_r($user);
```

Get a user's playlists
Get user's playlists

```php
$playlists = $api->getUserPlaylists('username');
Expand Down Expand Up @@ -341,10 +341,9 @@ $api->userFollowsPlaylist('owner_id', 'playlist_id', array(
));
```

Follow/Unfollow artist or user
Follow and unfollow an artist or user

```php

$api->followArtistsOrUsers('artist', '74ASZWbe4lXaubB36ztrGX');

$api->unfollowArtistsOrUsers('artist', '74ASZWbe4lXaubB36ztrGX');
Expand All @@ -365,7 +364,7 @@ $api->unfollowArtistsOrUsers('user', array(
));
```

Check if current user follows artist or user
Check if the current user follows a user or artist

```php
$follows = $api->currentUserFollows('user', array(
Expand Down
8 changes: 6 additions & 2 deletions demo.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@

Dotenv::load(__DIR__);

$session = new SpotifyWebAPI\Session(getenv('SPOTIFY_CLIENT_ID'), getenv('SPOTIFY_CLIENT_SECRET'), getenv('SPOTIFY_REDIRECT_URI'));
$session = new SpotifyWebAPI\Session(
getenv('SPOTIFY_CLIENT_ID'),
getenv('SPOTIFY_CLIENT_SECRET'),
getenv('SPOTIFY_REDIRECT_URI')
);
$api = new SpotifyWebAPI\SpotifyWebAPI();

if (isset($_GET['code'])) {
$session->requestToken($_GET['code']);
$session->requestAccessToken($_GET['code']);
$api->setAccessToken($session->getAccessToken());

print_r($api->me());
Expand Down
2 changes: 1 addition & 1 deletion src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public function send($method, $url, $parameters = array(), $headers = array())
return array(
'body' => $body,
'headers' => $headers,
'status' => $status
'status' => $status,
);
}

Expand Down
59 changes: 39 additions & 20 deletions src/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function getAuthorizeUrl($options = array())
$defaults = array(
'scope' => array(),
'show_dialog' => false,
'state' => ''
'state' => '',
);

$options = array_merge($defaults, (array) $options);
Expand All @@ -61,7 +61,7 @@ public function getAuthorizeUrl($options = array())
'response_type' => 'code',
'scope' => implode(' ', $options['scope']),
'show_dialog' => $options['show_dialog'] ? 'true' : 'false',
'state' => $options['state']
'state' => $options['state'],
);

return Request::ACCOUNT_URL . '/authorize/?' . http_build_query($parameters);
Expand Down Expand Up @@ -98,9 +98,9 @@ public function getClientSecret()
}

/**
* Get the number of seconds before the access token expires.
* Get the number of seconds for which the access token is valid.
*
* @return int The expires time.
* @return int The time period (in seconds) for which the access token is valid.
*/
public function getExpires()
{
Expand Down Expand Up @@ -128,21 +128,30 @@ public function getRefreshToken()
}

/**
* Refresh a access token.
* @deprecated Use Session::refreshAccessToken instead. This dummy function will be removed in 1.0.0.
*/
public function refreshToken()
{
trigger_error('Use Session::refreshAccessToken instead', E_USER_DEPRECATED);
return $this->refreshAccessToken();
}

/**
* Refresh an access token.
*
* @return bool Whether the access token was successfully refreshed.
*/
public function refreshToken()
public function refreshAccessToken()
{
$payload = base64_encode($this->getClientId() . ':' . $this->getClientSecret());

$parameters = array(
'grant_type' => 'refresh_token',
'refresh_token' => $this->refreshToken
'refresh_token' => $this->refreshToken,
);

$headers = array(
'Authorization' => 'Basic ' . $payload
'Authorization' => 'Basic ' . $payload,
);

$response = $this->request->account('POST', '/api/token', $parameters, $headers);
Expand All @@ -159,23 +168,23 @@ public function refreshToken()
}

/**
* Request a access token using the Client Credentials Flow.
* Request an access token using the Client Credentials Flow.
*
* @param array $scope Optional. Scope(s) to request from the user.
*
* @return bool Whether a access token was successfully granted.
* @return bool True when an access token was successfully granted, false otherwise.
*/
public function requestCredentialsToken($scope = array())
{
$payload = base64_encode($this->getClientId() . ':' . $this->getClientSecret());

$parameters = array(
'grant_type' => 'client_credentials',
'scope' => implode(' ', $scope)
'scope' => implode(' ', $scope),
);

$headers = array(
'Authorization' => 'Basic ' . $payload
'Authorization' => 'Basic ' . $payload,
);

$response = $this->request->account('POST', '/api/token', $parameters, $headers);
Expand All @@ -192,29 +201,39 @@ public function requestCredentialsToken($scope = array())
}

/**
* Request a access token.
* @deprecated Use Session::requestAccessToken instead. This dummy function will be removed in 1.0.0.
*/
public function requestToken($code)
{
trigger_error('Use Session::requestAccessToken instead', E_USER_DEPRECATED);
return $this->requestAccessToken($code);
}

/**
* Request an access token given an authorization code.
*
* @param string $code The authorization code from Spotify.
* @param string $authorizationCode The authorization code from Spotify.
*
* @return bool Whether a access token was successfully granted.
* @return bool True when the access token was successfully granted, false otherwise.
*/
public function requestToken($code)
public function requestAccessToken($authorizationCode)
{
$parameters = array(
'client_id' => $this->getClientId(),
'client_secret' => $this->getClientSecret(),
'code' => $code,
'code' => $authorizationCode,
'grant_type' => 'authorization_code',
'redirect_uri' => $this->getRedirectUri()
'redirect_uri' => $this->getRedirectUri(),
);

$response = $this->request->account('POST', '/api/token', $parameters);
$response = $response['body'];

if (isset($response->access_token)) {
if (isset($response->refresh_token)
&& isset($response->access_token)) {
$this->refreshToken = $response->refresh_token;
$this->accessToken = $response->access_token;
$this->expires = $response->expires_in;
$this->refreshToken = $response->refresh_token;

return true;
}
Expand Down
Loading

0 comments on commit d68ee32

Please sign in to comment.