Guney Cobanoglu
Guney Cobanoglu

Reputation: 733

Getting and parsing the Spotify Web API JSON with PHP

My aim is getting the album and its cover image with a given string like "Artist Name - Song Name". For that, I use Spotify Web API and I guess it doesn't require OAuth for these kinds of requests.

$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.spotify.com/v1/search?q=".$query."&type=track");
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
$data=curl_exec($ch);
curl_close($ch);
echo $data;

If you click the following link, you can get the JSON but this request returns me an error page. I've tried with Ajax as well but nothing changed. Can somebody help me about that?

Upvotes: 3

Views: 931

Answers (1)

Danny Kopping
Danny Kopping

Reputation: 5200

You should urlencode() your query before sending it to Spotify, otherwise you will get a 400 Bad Request response.

Upvotes: 2

Related Questions