【问题标题】:Display Artist Info from Last.FM API从 Last.FM API 显示艺术家信息
【发布时间】:2011-12-07 23:35:13
【问题描述】:

我的代码最近遇到了一些问题,它需要从 Last.FM API 提供的艺术家数据,我正在寻找一种不同的解决方案,但我正在努力寻找一种很好的简单方法来处理这个问题。目前我有:

    <?php $feed = simplexml_load_file("http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist=anartistname&api_key=01234567890");
$xml = simplexml_load_file($feed);
$info = $xml->artist->bio->summary; ?>

<?php echo $info; ?> 

显然,这是一个安全问题,因为我不想启用 fopen。谁能指出我的解决方案的方向?大多数人建议使用 cURL,但我不知道如何使用它,因此非常感谢那里的帮助。

【问题讨论】:

    标签: php xml parsing curl last.fm


    【解决方案1】:

    cURL 是一个 PHP 库。以下是一些解释得很好的链接:

    http://themekraft.com/getting-json-data-with-php-curl/ http://www.jonasjohn.de/sn-ps/php/curl-example.htm

    对于可能阅读此内容的其他人: 如果您的托管公司没有启用 fopen,您可以使用此行在您的 Web 根目录中创建一个“本地”php.ini 文件:

    allow_url_fopen = ON
    

    【讨论】:

      【解决方案2】:

      您两次致电simplexml_load_file。 试试:

      <?php
      
      $feed = 'http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist=anartistname&api_key=YOUR_KEY';
      $xml = simplexml_load_file($feed);
      $info = $xml->artist->bio->summary;
      
      echo $info;
      

      cURL 版本:

      <?php
      
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, 'http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist=anartistname&api_key=YOUR_KEY');
      curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($ch, CURLOPT_TIMEOUT, 15);
      $xml = curl_exec($ch);
      curl_close($ch);
      
      $xml = simplexml_load_string($xml);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-11-14
        • 1970-01-01
        • 1970-01-01
        • 2015-12-11
        • 1970-01-01
        • 2012-06-05
        • 2020-06-02
        • 2015-04-12
        相关资源
        最近更新 更多