VOOZH about

URL: https://www.cdata.com/kb/tech/spotify-odbc-php.rst

⇱ Natively Connect to Spotify Data in PHP


Natively Connect to Spotify Data in PHP

👁 Jerod Johnson
Jerod Johnson
Director, Technology Evangelism
The CData ODBC driver for Spotify enables you to create PHP applications with connectivity to Spotify data. Leverage the native support for ODBC in PHP.

Drop the CData ODBC Driver for Spotify into your LAMP or WAMP stack to build Spotify-connected Web applications. This article shows how to use PHP's ODBC built-in functions to connect to Spotify data, execute queries, and output the results.

Configure a DSN

If you have not already, first specify connection properties in an ODBC DSN (data source name). This is the last step of the driver installation. You can use the Microsoft ODBC Data Source Administrator to create and configure ODBC DSNs.

Using OAuth Authentication

Spotify uses OAuth 2.0 for authentication. You will need to create an application in the Spotify Developer Dashboard to obtain your client credentials.

Setting Up Your Spotify Application

  1. Visit the Spotify Developer Dashboard.
  2. Log in with your Spotify account and click Create app.
  3. Provide an app name, description, and set a Redirect URI (e.g.,
    http://localhost:33333
    for desktop applications).
  4. Copy your Client ID and Client Secret from the app settings.

Connection Properties

After setting the following connection properties, you are ready to connect:

  • AuthScheme: Set this to OAuth.
  • InitiateOAuth: Set this to GETANDREFRESH. You can use InitiateOAuth to manage the process to obtain the OAuthAccessToken.
  • OAuthClientId: Set this to your Spotify application's Client ID.
  • OAuthClientSecret: Set this to your Spotify application's Client Secret.
  • Scope: Set this to the required OAuth scopes (space-separated). The default includes all read scopes needed for the tables in this profile.
  • CallbackURL: Set this to the Redirect URI configured in your Spotify application (e.g., http://localhost:33333).

Example Connection String

Profile=C:\profiles\Spotify.apip;AuthScheme=OAuth;InitiateOAuth=GETANDREFRESH;OAuthClientId=your_client_id;OAuthClientSecret=your_client_secret;CallbackURL=http://localhost:33333;

Available OAuth Scopes

  • user-read-private: Read access to user's subscription details and explicit content settings.
  • user-read-email: Read access to user's email address.
  • user-library-read: Read access to a user's saved tracks, albums, episodes, shows, and audiobooks.
  • playlist-read-private: Read access to user's private playlists.
  • playlist-read-collaborative: Read access to collaborative playlists the user follows.
  • user-follow-read: Read access to the list of artists the current user follows.
  • user-read-playback-state: Read access to a user's player state (device, current track, progress).
  • user-read-currently-playing: Read access to a user's currently playing content.
  • user-read-playback-history: Read access to a user's recently played tracks.
  • user-top-read: Read access to a user's top artists and tracks.

Establish a Connection

Open the connection to Spotify by calling the or methods. To close connections, use or .

$conn = odbc_connect("CData ODBC API Source","user","password");

Connections opened with are closed when the script ends. Connections opened with the method are still open after the script ends. This enables other scripts to share that connection when they connect with the same credentials. By sharing connections among your scripts, you can save system resources, and queries execute faster.

$conn = odbc_pconnect("CData ODBC API Source","user","password");
...
odbc_close($conn); //persistent connection must be closed explicitly

Create Prepared Statements

Create prepared statements and parameterized queries with the function.

$query = odbc_prepare($conn, "SELECT * FROM Albums WHERE Id = ?");

Execute Queries

Execute prepared statements with .

$conn = odbc_connect("CData ODBC API Source","user","password");
$query = odbc_prepare($conn, "SELECT * FROM Albums WHERE Id = ?");
$success = odbc_execute($query, array('4aawyAB9vmqN3uQ7FjRGTy'));
 

Execute nonparameterized queries with .

$conn = odbc_connect("CData ODBC API Source","user","password");
$query = odbc_exec($conn, "SELECT , FROM Albums WHERE Id = '4aawyAB9vmqN3uQ7FjRGTy'");
 

Process Results

Access a row in the result set as an array with the function.

$conn = odbc_connect("CData ODBC Spotify data Source","user","password");
$query = odbc_exec($conn, "SELECT , FROM Albums WHERE Id = '4aawyAB9vmqN3uQ7FjRGTy'");
while($row = odbc_fetch_array($query)){
 echo $row[""] . "\n";
}

Display the result set in an HTML table with the function.

$conn = odbc_connect("CData ODBC Spotify data Source","user","password");
$query = odbc_prepare($conn, "SELECT * FROM Albums WHERE Id = ?");
$success = odbc_execute($query, array('4aawyAB9vmqN3uQ7FjRGTy'));
if($success)
 odbc_result_all($query);

More Example Queries

You will find complete information on the driver's supported SQL in the help documentation. The code examples above are Spotify-specific adaptations of the PHP community documentation for all ODBC functions.