解析iTunes前100名

时间:2014-01-30 11:55:27

标签: php xml rss simplexml itunes

我想从iTunes前100名RSS中获取以下信息,但我遇到了以下问题。

function itunes(){
$itunes_feed = "https://itunes.apple.com/au/rss/topsongs/limit=100/explicit=true/xml";
$itunes_feed = file_get_contents($itunes_feed);
$itunes_feed = preg_replace("/(<\/?)(\w+):([^>]*>)/", "$1$2$3", $itunes_feed);
$itunes_xml = new SimpleXMLElement($itunes_feed);
$itunes_entry = $itunes_xml->entry;

    foreach($itunes_entry as $entry){
        // Get the value of the entry ID, by using the 'im' namespace within the <id> attribute
        $entry_id['im'] = $entry->id->attributes('im', TRUE);
        echo $entry_id['im']['id']."<br>";
        //echo $entry_id['artist']."<br>";
    }
}

我可以从<id im:id="783656917">

获取ID

但是我无法得到以下内容

<im:artist href="https://itunes.apple.com/au/artist/pharrell-williams/id14934728?uo=2">Pharrell Williams</im:artist>

我想得到Pharrell Williams我试过了 $entry->id->attributes->im[artist] $entry_id['artist'] 甚至$entry_id->artist

但无论我做什么,我都无法让它给我艺术家的名字。

1 个答案:

答案 0 :(得分:1)

您需要将对象强制转换为字符串

$value = (string)($entry->imartist);