title: How can I use the new Spotify API using PHP cURL?
body: <p>I'm trying to get some data from Spotify, since they recently changed their API I wrote a new function that follows new directives:</p>
  
  <pre><code>function get_Spotify() {
      $getTrackID = $this-&gt;db-&gt;query("SELECT `trackid` FROM `spotify` ORDER BY RAND() LIMIT 1");
      $TrackID = $getTrackID-&gt;fetch_assoc();
      $id=$TrackID['trackid'];
  
      $client_id = '********************'; 
      $client_secret = '**********************'; 
  
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL,            'https://accounts.spotify.com/api/token' );
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
      curl_setopt($ch, CURLOPT_POST,           1 );
      curl_setopt($ch, CURLOPT_POSTFIELDS,     'grant_type=client_credentials' ); 
      curl_setopt($ch, CURLOPT_HTTPHEADER,     array('Authorization: Basic '.base64_encode($client_id.':'.$client_secret))); 
      $result=curl_exec($ch); 
      $result = json_decode($result, true);
      curl_close($ch);    
  
      //find spotify data
      $spotifyURL = 'https://api.spotify.com/v1/tracks/'.$id;
      $authorization = 'Authorization: Bearer '.$result["access_token"];
  
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json' , $authorization ));
      curl_setopt($ch, CURLOPT_URL, $spotifyURL);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:x.x.x) Gecko/20041107 Firefox/x.x");
      curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
      $json = curl_exec($ch);
      $json = json_decode($json, true);
      curl_close($ch);
  
      $spotifyURL_artist = $json['artists'][0]['href'];
  
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json' , $authorization ));
      curl_setopt($ch, CURLOPT_URL, $spotifyURL_artist);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:x.x.x) Gecko/20041107 Firefox/x.x");
      curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
      $json_artist = curl_exec($ch);
      $data_artist = json_decode($json_artist, true);
      curl_close($ch);
  
      $Spotify_Player = '
      &lt;div class="container-playerspot"&gt;
          &lt;div class="playerspot"&gt;
              &lt;div class="bg_spotify" style="background:url('.$data_artist['images'][0]['url'].');"&gt;&lt;div class="ert-spotify"&gt;&lt;/div&gt;&lt;/div&gt;
              &lt;ul class="player_spot-info info-one"&gt;
                  &lt;li&gt;'.$json['artists'][0]['name'].'&lt;/li&gt;
                  &lt;li&gt;'.$json['name'].'&lt;/li&gt;
              &lt;/ul&gt;
              &lt;ul class="player_spot-info info-two"&gt;
                  &lt;li&gt;'.$json['artists'][0]['name'].'&lt;/li&gt;
                  &lt;li&gt;'.$json['name'].'&lt;/li&gt;
                  &lt;li&gt;&lt;span id="duration"&gt;&lt;/span&gt;&lt;i&gt; / &lt;/i&gt;0.30&lt;/li&gt;
              &lt;/ul&gt;
              &lt;audio id="audio-player_spot" ontimeupdate="SeekBar()" ondurationchange="CreateSeekBar()" preload="none" loop&gt;
              &lt;source src="'.$json['preview_url'].'" type="audio/mpeg"&gt;Your browser does not support the audio element.
              &lt;/audio&gt;
          &lt;/div&gt;
      &lt;/div&gt;';
  
      return $Spotify_Player;
  
  }
  </code></pre>
  
  <p>But my page load slow so after investigating using <code>microtime()</code> I found that this function (<code>get_Spotify()</code>) takes about <strong>300 Milliseconds</strong>.</p>
  
  <p><strong>Is there anything wrong with my code that can lead to such a disastrous result?</strong> </p>
  
  <p>P.S.</p>
  
  <p>MySQL table actually has only 4 results, so I exclude my query from possible reason.</p>