Announcements

Help Wizard

Step 1

NEXT STEP

Code not Printing playlist details

Code not Printing playlist details

Hi,

 

I have this code but whenever I submit a URL into the box, it should output the details of the playlist but nothing is showing. Can someone help please?

 

<?php
// Check if user has submitted the form
// Check if user has submitted the form
if ( isset( $_POST[ 'playlist_url' ] ) ) {
    // Retrieve playlist ID from Spotify Playlist URL
    $playlist_url = $_POST[ 'playlist_url' ];
    $playlist_parts = explode( '/', parse_url( $playlist_url, PHP_URL_PATH ) );
    $playlist_id = end( $playlist_parts );

    // Spotify Web API endpoint for retrieving a playlist
    $endpoint = "https://api.spotify.com/v1/playlists/{$playlist_id}";

    // Set up cURL request with Spotify API credentials
    $client_id = mdjm_get_option( 'spotify_api_key' );
    $client_secret = mdjm_get_option( 'spotify_secret_key' );

    $spc_init = curl_init();
    curl_setopt( $spc_init, CURLOPT_URL, 'https://accounts.spotify.com/api/token' );
    curl_setopt( $spc_init, CURLOPT_RETURNTRANSFER, 1 );
    curl_setopt( $spc_init, CURLOPT_POST, 1 );
    curl_setopt( $spc_init, CURLOPT_POSTFIELDS, 'grant_type=client_credentials' );
    curl_setopt( $spc_init, CURLOPT_HTTPHEADER, array( 'Authorization: Basic ' . base64_encode( $client_id . ':' . $client_secret ) ) );

    $result = curl_exec( $spc_init );
    $status_code = curl_getinfo( $spc_init, CURLINFO_HTTP_CODE );

    if ( curl_errno( $spc_init ) ) {
        $error_message = curl_error( $spc_init );
        echo "cURL Error: " . $error_message;
    } else {
        // Decode the JSON response from Spotify
        $data = json_decode( $result, true );

        if ( $data === null ) {
            echo "Error: Unable to decode the JSON response from Spotify.";
        } else {
            // Print the JSON response and status code to the screen
            echo "<pre>";
            echo "Status Code: " . $status_code . "\n";
            echo json_encode( $data, JSON_PRETTY_PRINT );
            echo "</pre>";
        }
    }

    // Close cURL session
    curl_close( $spc_init );
}

    // Check for errors in the Spotify response
    if ( isset( $data[ 'error' ][ 'message' ] ) ) {
        $error_message = $data[ 'error' ][ 'message' ];
        echo "Error: " . $error_message;
    } else {
        // Extract relevant details from playlist JSON
        $name = $data[ 'name' ];
        $description = $data[ 'description' ];
        $tracks = $data[ 'tracks' ][ 'items' ];

        // Print playlist details
        echo "<h1>{$name}</h1>";
        echo "<p>{$description}</p>";

        // Loop through tracks and print track details
        foreach ( $tracks as $track ) {
            $track_name = $track[ 'track' ][ 'name' ];
            $artists = array();
            foreach ( $track[ 'track' ][ 'artists' ] as $artist ) {
                $artists[] = $artist[ 'name' ];
            }
            $artist_name = implode( ', ', $artists );
            echo "<p>{$track_name} by {$artist_name}</p>";
        }
    }
?>

 

Reply
3 Replies

Client credentials only work with playlists that are public and are displayed on your/someone's profile.

XimzendSpotify Star
Help others find this answer and click "Accept as Solution".
If you appreciate my answer, maybe give me a Like.
Note: I'm not a Spotify employee.

The playlist is public and attached to my profile.

The data of your playlist is not printed, because it's never requested. You only specified the URL of the $endpoint, but aren't using it anywhere in your code. The $data your code is trying to echo is the acces_token, and not the data of your playlist.
(I just noticed it after I had a chance to run the code myself.)

 
XimzendSpotify Star
Help others find this answer and click "Accept as Solution".
If you appreciate my answer, maybe give me a Like.
Note: I'm not a Spotify employee.

Suggested posts

Let's introduce ourselves!

Hey there you,   Yeah, you! 😁   Welcome - we're glad you joined the Spotify Community!   While you here, let's have a fun game and get…

ModeratorStaff / Moderator/ 4 years ago  in Social & Random