Spotify API GET curl with auth using php - is this code good? $spotify_track = '1HbcclMpw0q2WDWpdGCKdS';
    $client_id = '[my_id]'; 
    $client_secret = '[my_secret]'; 

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

    $response = curl_exec($curl);
    $token = json_decode($response)-&gt;access_token;
    $err = curl_error($curl);
    curl_close($curl);

    if ($err) {
      echo &quot;cURL Error #:&quot; . $err;
      curl_close($curl);
    } else {
          $curl = curl_init();
          curl_setopt_array($curl, array(
          CURLOPT_URL =&gt; &quot;https://api.spotify.com/v1/audio-features/&quot;.$spotify_track,
          CURLOPT_RETURNTRANSFER =&gt; true,
          CURLOPT_FOLLOWLOCATION =&gt; true,
          CURLOPT_ENCODING =&gt; &quot;&quot;,
          CURLOPT_MAXREDIRS =&gt; 10,
          CURLOPT_TIMEOUT =&gt; 30,
          CURLOPT_HTTP_VERSION =&gt; CURL_HTTP_VERSION_1_1,
          CURLOPT_CUSTOMREQUEST =&gt; &quot;GET&quot;,
          CURLOPT_HTTPHEADER =&gt; array(
            &quot;Content-Type: application/json&quot;,
            &quot;Authorization: Bearer &quot;.$token,
          ),
        ));
          $track_info = curl_exec($curl);
          var_dump($track_info);
          curl_close($curl);
    }

I've modified the code suggested on stackoverflow and came up with my own solution. I successfully fetch the song information. However, I wonder if this is the proper way in handling API using curl in php. I also noticed the page reloads a little bit slower compared to without the code aforementioned.
There's not much you can do wrong here. It works. I don't want to go into details of how to handle Spotify's API, but I would like to comment on your code.
One thing I noticed is that, if there's an error, you call curl_close() twice. That looks like a bug.
You're using curl two times, and in quite a different way. The first time you set individual options with curl_setopt(), the second time you combine all options and call curl_setopt_array(). Your code would be more consistent if you used the same method both times.
Better still, why not make a simple function to do the curl call for you? Something like this:
function callSpotifyApi($options) 
{
    $curl  = curl_init();
    curl_setopt_array($curl, $options); 
    $json  = curl_exec($curl);
    $error = curl_error($curl);
    curl_close($curl);
    if ($error) {
        return ['error'   =&gt; TRUE,
                'message' =&gt; $error];
    }
    $data  = json_decode($json);
    if (is_null($data)) {
        return ['error'   =&gt; TRUE,
                'message' =&gt; json_last_error_msg()];
    }
    return $data; 
}

This function combines all the things that are common between the two curl calls, and therefore saves you writing code twice, and the reader reading it twice. This function also checks if the returned json code was valid and tells you what was wrong. You would normally use json_last_error() to detect json errors, but if a Spotify call returns null then there's surely an error.
Given the above function, the first curl call would now become:
$headers  = ['Authorization: Basic '.base64_encode($client_id.':'.$client_secret)];
$url      = 'https://accounts.spotify.com/api/token';
$options  = [CURLOPT_URL            =&gt; $url,
             CURLOPT_RETURNTRANSFER =&gt; TRUE,
             CURLOPT_POST           =&gt; TRUE,
             CURLOPT_POSTFIELDS     =&gt; 'grant_type=client_credentials',
             CURLOPT_HTTPHEADER     =&gt; $headers];
$credentials = callSpotifyApi($options); 
$token = $credentials-&gt;access_token;

I haven't tested this code, so there might be errors. The second call will now be very similar:
$headers  = ['Content-Type: application/json',
             'Authorization: Bearer '.$token];
$url      = 'https://api.spotify.com/v1/audio-features/'.$spotify_track;
$options  = [CURLOPT_URL            =&gt; $url,
             CURLOPT_RETURNTRANSFER =&gt; TRUE,
             CURLOPT_FOLLOWLOCATION =&gt; TRUE,
             CURLOPT_ENCODING       =&gt; '',
             CURLOPT_MAXREDIRS      =&gt; 10,
             CURLOPT_TIMEOUT        =&gt; 30,
             CURLOPT_HTTP_VERSION   =&gt; CURL_HTTP_VERSION_1_1,
             CURLOPT_CUSTOMREQUEST  =&gt; 'GET',
             CURLOPT_HTTPHEADER     =&gt; $headers];
$features = callSpotifyApi($options); 
var_dump($features);

Writing calls to Spotify consistently this way makes it easier to read the code, and find and correct bugs.
I didn't really review how you use the curl options, or whether this is the best way to use the Spotify API. Others may have more experience with this.