可以';t使Last.FM API PHP代理运行


Can't get Last.FM API PHP Proxy to work

我正在尝试定期从Last.FM API检索JSON相册数据,由于它们不支持JSONP,我正在使用PHP代理来规避跨域限制。这是我的PHP代理:

<?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;
?>

正如你所看到的,我使用GET来指定艺术家和专辑,所以URL看起来像albumartproxy.php?artist=Jake%20Bugg&album=Jake%20Bugg

但是由于某些原因,当我尝试运行代理时,我得到了以下错误:

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

当我直接访问API URL时,它是否有效?有人能帮忙吗?我做错了什么?

您需要urlencode()与请求一起传递的GET参数。

您需要urlender()GET s,如下所示:

<?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);
?>

样本输出:http://pastie.org/8114551

希望这能有所帮助!