Spotify: Search for Artist and Title at the same time

I am new to the Spotify API. I am trying to figure out how to search for an artist and a title at the same time.

I'm using an AJAX call with both artist and title. If either is blank, I can simply search for the other, but I can not seem to figure out how to search for the artist and title at the same time. The purpose of this is to narrow down the result to find a specific track.

Here's what I have:

header('Content-Type: application/json');

$search_artist = rawurlencode($_POST['search_artist']);
$search_title = rawurlencode($_POST['search_title']);


if($search_artist && !$search_title) {
    //Search for Artist only
    $url = "https://api.spotify.com/v1/search?q=*".$search_artist."*&type=artist";
} else if(!$search_artist && $search_title) {
    //Search for title only
    $url = "https://api.spotify.com/v1/search?q=*".$search_title."*&type=track";
} else if($search_artist && $search_title) {
    //Search for Artist AND Title
    $url = "https://api.spotify.com/v1/search?q=*".$search_artist."*&type=artist&q=*".$search_title."*&type=track";
}

$contents = file_get_contents($url);

echo json_encode($contents);

As mentioned, if I search for only artist or only title, it works just fine. I just don't know how to search for both at the same time using Spotify's API.


If I rephrase your question it sounds like:

  • If only search_artist is filled in, you want to the find an artist
  • If only search_track is filled in, you want to find a track
  • If both are filled in, you still want to find a track
  • Then the type should just be track.

    The documentation also doesn't mention that you can have q and type parameters twice. https://developer.spotify.com/web-api/search-item/ It does mention you can search for tracks and artist by having them comma-separated, type=track,artist but reading your question this is probably not what you want.

    $url = "https://api.spotify.com/v1/search?q=".$search_artist."+".$search_title."&type=track";
    
    链接地址: http://www.djcxy.com/p/9468.html

    上一篇: 搜索艺术家的曲目

    下一篇: Spotify:同时搜索Artist和Title