Question

I'm trying to regularly retrieve JSON album data from the Last.FM API, and since they don't support JSONP I'm using a PHP Proxy to circumvent the cross-domain limitation. Here's my PHP proxy:

<?php
$artist = $_GET["artist"];
$album = $_GET["album"];
$content = file_get_contents('http://ws.audioscrobbler.com/2.0/?method=album.getinfo&api_key=APIKEY=' . $artist . '&album=' . $album . '&format=json');
echo $content;
?>

As you can see I'm using GET to specify the artist and album, so the URL would look like albumartproxy.php?artist=Jake%20Bugg&album=Jake%20Bugg

But for some reason I get the following error when I try to run the proxy:

Warning: file_get_contents(http://ws.audioscrobbler.com/2.0/?method=album.getinfo&api_key=037358e302c80571663e6a7a66b1dc05&artist=Jake Bugg&album=Jake Bugg&format=json) [function.file-get-contents]: failed to open stream: HTTP request failed! in /MYDOMAIN/albumartproxy.php on line 6

When I access the API URL directly it works? Can someone help? What am I doing wrong?

Was it helpful?

Solution

You'll need to urlencode() the GET parameters that you are passing along with the request.

OTHER TIPS

You need urlencode() the GETs, like so:

<?php

$apikey = "273f3dc84c2d55501f3dc25a3d61eb39";

$artist = urlencode($_GET["artist"]);
$album = urlencode($_GET["album"]);
$autocorrect = 0;

$content = "http://ws.audioscrobbler.com/2.0/?method=album.getinfo&api_key=".$apikey."&artist=".$artist."&album=".$album."&autocorrect=".$autocorrect;

echo file_get_contents($content);

?>

Sample output: http://pastie.org/8114551

Hope this helps!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top