How to disable the maximum number of results to return in spotify API?

Viewed 24

I want to get all tracks from an artist but when I run the code it has a 20 tracks limit and I can not get all tracks and albums. I am searching for something to disable the limit or change it to a higher value.

This is what I have:

<?php 

    $artist = 'artist_id';
    $client_id     = 'client_id'; 
    $client_secret = 'client_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)->access_token;
    $err = curl_error($curl);
    curl_close($curl);

    if ($err) {
      echo "cURL Error #:" . $err;
      curl_close($curl);
    } else {
          $curl = curl_init();
          curl_setopt_array($curl, array(
          CURLOPT_URL => "https://api.spotify.com/v1/artists/".$artist."/albums",
          CURLOPT_RETURNTRANSFER => true,
          CURLOPT_FOLLOWLOCATION => true,
          CURLOPT_ENCODING => "",
          CURLOPT_MAXREDIRS => 10,
          CURLOPT_TIMEOUT => 30,
          CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
          CURLOPT_CUSTOMREQUEST => "GET",
          CURLOPT_HTTPHEADER => array(
            "Content-Type: application/json",
            "Authorization: Bearer ".$token,
          ),
        ));
            $track_info = curl_exec($curl);
            $value1 = str_replace("\n", "", $track_info);
            $yummy = json_decode($value1);
            $value = (object) get_object_vars($yummy);
            $valuu = get_object_vars($value)['items']['6'];
            $yummy1 = json_decode(json_encode($valuu), true);
            $arrdatas = [];
            foreach($yummy1['artists'] as $artists) {
              array_push($arrdatas, $artists['name']);
            }
          echo implode(" & ", $arrdatas);
          echo "<br>";
            foreach($yummy1 as $yummy11) {
              if(!is_array($yummy11) && $yummy11 != "album" && $yummy11 != "single" && !preg_match("/api.spotify.com/", $yummy11) && !preg_match("/spotify:/", $yummy11)) {
               echo $yummy11."<br>";
              } else {
                if(!empty($yummy11['0'])) {
                  if(!empty($yummy11['0']['url'])) {
                    echo $yummy11['0']['url'];
                    echo "<br>";
                  }
                }
              }
            }

          curl_close($curl);
    }

?>

On line 42 $valuu = get_object_vars($value)['items']['6']; I am not able to set more than ['19'] after ['items'] because the API sends just 20 tracks and albums. What should I do to get all of them or at least set a higher limit?

1 Answers

I just added ?limit=50 in front of this line:

  CURLOPT_URL => "https://api.spotify.com/v1/artists/".$artist."/albums",

It should be like this:

  CURLOPT_URL => "https://api.spotify.com/v1/artists/".$artist."/albums?limit=50",
Related