<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Code not Printing playlist details in Spotify for Developers</title>
    <link>https://community.spotify.com/t5/Spotify-for-Developers/Code-not-Printing-playlist-details/m-p/5539979#M8807</link>
    <description>&lt;P&gt;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.&lt;BR /&gt;(I just noticed it after I had a chance to run the code myself.)&lt;/P&gt;&lt;DIV&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/DIV&gt;</description>
    <pubDate>Sat, 15 Apr 2023 16:46:39 GMT</pubDate>
    <dc:creator>Ximzend</dc:creator>
    <dc:date>2023-04-15T16:46:39Z</dc:date>
    <item>
      <title>Code not Printing playlist details</title>
      <link>https://community.spotify.com/t5/Spotify-for-Developers/Code-not-Printing-playlist-details/m-p/5536139#M8676</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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?&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="php"&gt;&amp;lt;?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 "&amp;lt;pre&amp;gt;";
            echo "Status Code: " . $status_code . "\n";
            echo json_encode( $data, JSON_PRETTY_PRINT );
            echo "&amp;lt;/pre&amp;gt;";
        }
    }

    // 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 "&amp;lt;h1&amp;gt;{$name}&amp;lt;/h1&amp;gt;";
        echo "&amp;lt;p&amp;gt;{$description}&amp;lt;/p&amp;gt;";

        // 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 "&amp;lt;p&amp;gt;{$track_name} by {$artist_name}&amp;lt;/p&amp;gt;";
        }
    }
?&amp;gt;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 07 Apr 2023 22:25:09 GMT</pubDate>
      <guid>https://community.spotify.com/t5/Spotify-for-Developers/Code-not-Printing-playlist-details/m-p/5536139#M8676</guid>
      <dc:creator>jackm91</dc:creator>
      <dc:date>2023-04-07T22:25:09Z</dc:date>
    </item>
    <item>
      <title>Re: Code not Printing playlist details</title>
      <link>https://community.spotify.com/t5/Spotify-for-Developers/Code-not-Printing-playlist-details/m-p/5536342#M8682</link>
      <description>&lt;P&gt;Client credentials only work with playlists that are public&amp;nbsp;&lt;STRONG&gt;and&lt;/STRONG&gt; are displayed on your/someone's profile.&lt;/P&gt;</description>
      <pubDate>Sat, 08 Apr 2023 12:56:31 GMT</pubDate>
      <guid>https://community.spotify.com/t5/Spotify-for-Developers/Code-not-Printing-playlist-details/m-p/5536342#M8682</guid>
      <dc:creator>Ximzend</dc:creator>
      <dc:date>2023-04-08T12:56:31Z</dc:date>
    </item>
    <item>
      <title>Re: Code not Printing playlist details</title>
      <link>https://community.spotify.com/t5/Spotify-for-Developers/Code-not-Printing-playlist-details/m-p/5536777#M8705</link>
      <description>&lt;P&gt;The playlist is public and attached to my profile.&lt;/P&gt;</description>
      <pubDate>Sun, 09 Apr 2023 19:01:40 GMT</pubDate>
      <guid>https://community.spotify.com/t5/Spotify-for-Developers/Code-not-Printing-playlist-details/m-p/5536777#M8705</guid>
      <dc:creator>jackm91</dc:creator>
      <dc:date>2023-04-09T19:01:40Z</dc:date>
    </item>
    <item>
      <title>Re: Code not Printing playlist details</title>
      <link>https://community.spotify.com/t5/Spotify-for-Developers/Code-not-Printing-playlist-details/m-p/5539979#M8807</link>
      <description>&lt;P&gt;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.&lt;BR /&gt;(I just noticed it after I had a chance to run the code myself.)&lt;/P&gt;&lt;DIV&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/DIV&gt;</description>
      <pubDate>Sat, 15 Apr 2023 16:46:39 GMT</pubDate>
      <guid>https://community.spotify.com/t5/Spotify-for-Developers/Code-not-Printing-playlist-details/m-p/5539979#M8807</guid>
      <dc:creator>Ximzend</dc:creator>
      <dc:date>2023-04-15T16:46:39Z</dc:date>
    </item>
  </channel>
</rss>

