在LastFM API和PHP中检索BIO信息

时间:2012-06-05 08:42:57

标签: php api curl last.fm

我正在尝试用LASTFM api查找艺术家的BIO信息,但我遇到了麻烦。

我正在使用PHP和一个将结果作为XML返回的CURL请求。

如果我使用CURL发送的url并且只是在浏览器中正常加载,它会根据需要显示所有内容。但是,CURL在PHP脚本中返回的结果缺少XML的某些属性。

例如:artist-> bio->摘要,但没有内容。

你可以复制我正在做的事情复制这里的基本概念:

在您的浏览器中打开它: http://ws.audioscrobbler.com/2.0/artist/cher/info.xml

将其复制并粘贴到PHP文件中并打开:

$url = 'http://ws.audioscrobbler.com/2.0/artist/cher/info.xml';
 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, $url);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 curl_setopt($ch, CURLOPT_TIMEOUT, 20);
 curl_setopt($ch, CURLOPT_HEADER, 0);
 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
 $retData = curl_exec($ch);
 curl_close($ch);
 echo '<pre>';
 echo $retData;
 echo '</pre>';

您会看到返回的结果类似但在curl请求中缺少内容。我也尝试过这样做一个simplexml_load_file(),但同样的事情发生了。我错过了什么?欢呼任何帮助!

2 个答案:

答案 0 :(得分:3)

尝试替换

echo $retData;

echo htmlspecialchars($retData);

你会发现一切都在那里。您在HTML上下文中打印原始XML,并且XML中的某些标记被解释为HTML标记。

答案 1 :(得分:2)

网址错了,但这可能是一个错字; p。缺少其内容的原因是<![CDATA[标记,在解析之前删除它,你应该很好。

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://ws.audioscrobbler.com/2.0/artist/cher/info.xml');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$retData = curl_exec($ch);

$retData = str_replace(array('<![CDATA[',']]>'),'',$retData);

$xml = simplexml_load_string($retData);
$lastFM = json_decode(json_encode($xml),TRUE);

print_r($lastFM);

/*Array
(
    [name] => Cher
    [mbid] => bfcc6d75-a6a5-4bc6-8282-47aec8531818
    [url] => http://www.last.fm/music/Cher
    [image] => Array
        (
            [0] => http://userserve-ak.last.fm/serve/34/63530405.png
            [1] => http://userserve-ak.last.fm/serve/64/63530405.png
            [2] => http://userserve-ak.last.fm/serve/126/63530405.png
            [3] => http://userserve-ak.last.fm/serve/252/63530405.png
            [4] => http://userserve-ak.last.fm/serve/500/63530405/Cher.png
        )

    [streamable] => 1
    [stats] => Array
        (
            [listeners] => 768175
            [playcount] => 7895613
        )

    [similar] => Array
        (
            [artist] => Array
                (
                    [0] => Array
                        (
                            [name] => Sonny & Cher
                            [url] => http://www.last.fm/music/Sonny+&+Cher
                            [image] => Array
                                (
                                    [0] => http://userserve-ak.last.fm/serve/34/71168880.png
                                    [1] => http://userserve-ak.last.fm/serve/64/71168880.png
                                    [2] => http://userserve-ak.last.fm/serve/126/71168880.png
                                    [3] => http://userserve-ak.last.fm/serve/252/71168880.png
                                    [4] => http://userserve-ak.last.fm/serve/500/71168880/Sonny++Cher.png
                                )

                        )

                    [1] => Array
                        (
                            [name] => Cyndi Lauper
                            [url] => http://www.last.fm/music/Cyndi+Lauper
                            [image] => Array
                                (
                                    [0] => http://userserve-ak.last.fm/serve/34/76056286.png
                                    [1] => http://userserve-ak.last.fm/serve/64/76056286.png
                                    [2] => http://userserve-ak.last.fm/serve/126/76056286.png
                                    [3] => http://userserve-ak.last.fm/serve/252/76056286.png
                                    [4] => http://userserve-ak.last.fm/serve/_/76056286/Cyndi+Lauper++for+Uniqlo.png
                                )

                        )

                    [2] => Array
                        (
                            [name] => Madonna
                            [url] => http://www.last.fm/music/Madonna
                            [image] => Array
                                (
                                    [0] => http://userserve-ak.last.fm/serve/34/78270650.png
                                    [1] => http://userserve-ak.last.fm/serve/64/78270650.png
                                    [2] => http://userserve-ak.last.fm/serve/126/78270650.png
                                    [3] => http://userserve-ak.last.fm/serve/252/78270650.png
                                    [4] => http://userserve-ak.last.fm/serve/_/78270650/Madonna+Shes+a+bad+girl.png
                                )

                        )

                    [3] => Array
                        (
                            [name] => Kylie Minogue
                            [url] => http://www.last.fm/music/Kylie+Minogue
                            [image] => Array
                                (
                                    [0] => http://userserve-ak.last.fm/serve/34/78217522.png
                                    [1] => http://userserve-ak.last.fm/serve/64/78217522.png
                                    [2] => http://userserve-ak.last.fm/serve/126/78217522.png
                                    [3] => http://userserve-ak.last.fm/serve/252/78217522.png
                                    [4] => http://userserve-ak.last.fm/serve/500/78217522/Kylie+Minogue+KylieBestOf.png
                                )

                        )

                    [4] => Array
                        (
                            [name] => Tina Turner
                            [url] => http://www.last.fm/music/Tina+Turner
                            [image] => Array
                                (
                                    [0] => http://userserve-ak.last.fm/serve/34/74998404.jpg
                                    [1] => http://userserve-ak.last.fm/serve/64/74998404.jpg
                                    [2] => http://userserve-ak.last.fm/serve/126/74998404.jpg
                                    [3] => http://userserve-ak.last.fm/serve/252/74998404.jpg
                                    [4] => http://userserve-ak.last.fm/serve/_/74998404/Tina+Turner+Queen+3.jpg
                                )

                        )

                )

        )

    [tags] => Array
        (
            [tag] => Array
                (
                    [0] => Array
                        (
                            [name] => pop
                            [url] => http://www.last.fm/tag/pop
                        )

                    [1] => Array
                        (
                            [name] => female vocalists
                            [url] => http://www.last.fm/tag/female%20vocalists
                        )

                    [2] => Array
                        (
                            [name] => 80s
                            [url] => http://www.last.fm/tag/80s
                        )

                    [3] => Array
                        (
                            [name] => dance
                            [url] => http://www.last.fm/tag/dance
                        )

                    [4] => Array
                        (
                            [name] => rock
                            [url] => http://www.last.fm/tag/rock
                        )

                )

        )

    [bio] => Array
        (
            [published] => Tue, 1 May 2012 12:51:56 +0000
            [summary] => Cher (born Cherilyn Sarkisian on May 20, 1946) is an American singer, actress, songwriter, author and entertainer. Among her many career accomplishments in music, television and film, she has won an Academy Award, a Grammy Award, an Emmy Award and three Golden Globe Awards among others.  Referred to as the Goddess of Pop, Cher first rose to prominence in 1965 as one half of the pop/rock duo Sonny & Cher.
            [content] => Cher (born Cherilyn Sarkisian on May 20, 1946) is an American singer, actress, songwriter, author and entertainer. Among her many career accomplishments in music, television and film, she has won an Academy Award, a Grammy Award, an Emmy Award and three Golden Globe Awards among others.

 Referred to as the Goddess of Pop, Cher first rose to prominence in 1965 as one half of the pop/rock duo Sonny & Cher. She also established herself as a solo recording artist, releasing 25 albums, contributing to numerous compilations, and tallying 34 Billboard Top 40 entries over her career, both solo and with Sonny. These include eighteen Top 10 singles and five number one singles (four solo). Cher has had 16 top ten hits in the UK between 1965 and 2003, four of which reached number one (two solo, one with Sonny, one as part of a charity single).

 In a career surpassing 40 years, Cher has been described as an enduring pop icon and one of the most popular female artists in music history. Since her debut in 1964, Cher has sold as a solo artist over 180 million records worldwide and an estimated 70 million singles and with duo Sonny and Cher over 80 million records becoming one of the biggest-selling artists of all time.

 She became a television star in the 1970s and a film actress in the 1980s. In 1987, she won the Academy Award for Best Actress for her role in the romantic comedy Moonstruck.

 During 2007, in the seventh volume of Chrome Hearts, Cher once again confirmed that she is working on her twenty-sixth studio album. No recordings have taken place as of yet, and no release date had been scheduled.

 It is rumoured that Cher will collaborate with Lady Gaga in a song for her next album Born This Way. This album is expected to be released in Spring 2011.

 On February 7, 2008 Cher, at 61, announced that she had reached a deal to perform 200 shows over three years live at the Colosseum at Caesar's Palace in Las Vegas. Her new show, entitled Cher at the Colosseum, debuted on May 6, 2008. The show reportedly includes 18 dancers, 4 aerialists, and multiple costumes designed by Bob Mackie. Choreography will be directed by Doriana Sanchez who also worked with Cher on her past three major tours. Her show will perform four nights a week for a month, and a second leg of the show will continue in August. Cher will share the stage on a rotating basis with contemporaries Bette Midler (whose The Showgirl Must Go On opened on February 20, 2008) and Elton John (whose The Red Piano which opened in 2004, will continues its run of about 50 shows a year).

 Cher made her musical debut and first film role in a decade in , starring alongside Christina Aguilera, who also made her musical and film debut. The film was released in November 2010.

User-contributed text is available under the Creative Commons By-SA License and may also be available under the GNU FDL.
        )

)


*/