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.