【问题标题】:Can't get Last.FM API PHP Proxy to work无法让 Last.FM API PHP 代理工作
【发布时间】:2013-07-04 04:12:09
【问题描述】:

我正在尝试从 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&amp;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 有帮助吗?
  • 是的!你是最好的!提交它作为答案!
  • 仅供参考,Last.fm API 实际上确实支持 JSONP 请求:last.fm/api/rest

标签: php javascript proxy jsonp last.fm


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    你需要 urlencode() GETs,像这样:

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

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-03
      相关资源
      最近更新 更多