1

I'm trying to get information from the Spotify API. When accessing this URL in my browser, it all works perfect; https://api.spotify.com/v1/search?q=Led%20Zeppelin%20Kashmir&type=track

However, then I use this code to try to get the data I'm just getting a white page. I've Googled and searched Stackoverflow, but still no cigar. Does anyone know why this code doesn't work?

Appreciate any help on this.

$artist = 'Led Zeppelin';
$title = 'Kashmir';

$spotifyURL = 'https://api.spotify.com/v1/search?q='.$artist.'%20'.$title.'&type=track';

$ch = curl_init();
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);
curl_close($ch);

echo '<pre>'.print_r($json, true).'</pre>';
4
  • Turn on PHP errors ? Commented Aug 29, 2015 at 19:27
  • PHP errors are enabled. No errors are returned. Commented Aug 29, 2015 at 19:31
  • Check to see if the page redirects, if it does there is an option in curl to follow redirects. Commented Aug 29, 2015 at 19:32
  • artist and $title need to be urlencoded in the URL. In this case the problem is the space in Led Zeppelin. Commented Aug 29, 2015 at 19:41

1 Answer 1

1

Your URL contains spaces. Use the following line instead:

$spotifyURL = 'https://api.spotify.com/v1/search?q='.urlencode($artist.' '.$title).'&type=track';
Sign up to request clarification or add additional context in comments.

1 Comment

You're absolutely right! Thanks! This solved it for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.