【问题标题】:Parse iTunes RSS Atom feed with PHP?用 PHP 解析 iTunes RSS Atom 提要?
【发布时间】:2012-04-15 11:03:15
【问题描述】:

尝试使用 PHP 脚本解析 iTunes Atom 提要。如果您访问iTunes RSS Generator,您可以像这样生成一个 Atom 提要:

http://itunes.apple.com/us/rss/topsongs/limit=10/genre=16/explicit=true/xml

它会给出这样的 iTunes RSS 提要结果:

<?xml version="1.0" encoding="utf-8"?>
<feed xmlns:im="http://itunes.apple.com/rss" xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
    <id>http://itunes.apple.com/us/rss/topsongs/limit=10/genre=16/explicit=true/xml</id><title>iTunes Store: Top Songs in Soundtrack</title><updated>2012-04-01T07:22:41-07:00</updated><link rel="alternate" type="text/html" href="http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewTop?id=17&amp;popId=1"/><link rel="self" href="http://itunes.apple.com/us/rss/topsongs/limit=10/genre=16/explicit=true/xml"/><icon>http://phobos.apple.com/favicon.ico</icon><author><name>iTunes Store</name><uri>http://www.apple.com/itunes/</uri></author><rights>Copyright 2008 Apple Inc.</rights>
        <entry>
            <updated>2012-04-01T07:22:41-07:00</updated>
                <id im:id="509605055">http://itunes.apple.com/us/album/eyes-open/id509605019?i=509605055&amp;uo=2</id>
                <title>Eyes Open - Taylor Swift</title>
                <im:name>Eyes Open</im:name>
                <link rel="alternate" type="text/html" href="http://itunes.apple.com/us/album/eyes-open/id509605019?i=509605055&amp;uo=2"/>
                <im:contentType term="Music" label="Music"><im:contentType term="Track" label="Track"/></im:contentType>
                <category term="Soundtrack" scheme="http://itunes.apple.com/us/genre/music-soundtrack/id16?uo=2" label="Soundtrack"/>

                <link title="Preview" rel="enclosure" type="audio/x-m4a" href="http://a2.mzstatic.com/us/r1000/116/Music/88/70/a6/mzi.gcauwkkw.aac.p.m4a" im:assetType="preview"><im:duration>30000</im:duration></link>

                <im:artist href="http://itunes.apple.com/us/artist/taylor-swift/id159260351?uo=2">Taylor Swift</im:artist>
                <im:price amount="1.29000" currency="USD">$1.29</im:price>
                <im:image height="55">http://a3.mzstatic.com/us/r1000/069/Music/v4/15/59/19/15591949-a525-99e8-0c50-45697b0ec78b/UMG_cvrart_00602527969206_01_RGB72_1200x1200_12UMGIM10247.55x55-70.jpg</im:image>
                <im:image height="60">http://a5.mzstatic.com/us/r1000/069/Music/v4/15/59/19/15591949-a525-99e8-0c50-45697b0ec78b/UMG_cvrart_00602527969206_01_RGB72_1200x1200_12UMGIM10247.60x60-50.jpg</im:image>
                <im:image height="170">http://a3.mzstatic.com/us/r1000/069/Music/v4/15/59/19/15591949-a525-99e8-0c50-45697b0ec78b/UMG_cvrart_00602527969206_01_RGB72_1200x1200_12UMGIM10247.170x170-75.jpg</im:image>
                <rights>2012 Universal Republic Records, a division of UMG Recordings, Inc.</rights>
                <im:releaseDate label="March 20, 2012">2012-03-20T00:00:00-07:00</im:releaseDate>
                <im:collection><im:name>The Hunger Games (Songs from District 12 and Beyond)</im:name><link rel="alternate" type="text/html" href="http://itunes.apple.com/us/album/hunger-games-songs-from-district/id509605019?uo=2"/><im:contentType term="Music" label="Music"><im:contentType term="Album" label="Album"/></im:contentType></im:collection>

(等等...)

使用 PHP 脚本,我可以为每个 titleidim:image 获得结果em>[entry] 在脚本输出中使用。我需要得到的是 link 条目之一的 url。特别是我需要“预览”链接中的网址:

<link title="Preview" rel="enclosure" type="audio/x-m4a" href="http://a2.mzstatic.com/us/r1000/116/Music/88/70/a6/mzi.gcauwkkw.aac.p.m4a" im:assetType="preview"><im:duration>30000</im:duration></link>

在这种情况下,我们需要 a2.mzstatic.com/us/r1000/116/Music/88/70/a6/mzi.gcauwkkw.aac.p.m4a 链接才能使用在 10 个条目中的每一个的脚本结果中。

如何为上述 Atom 提要中的 .m4a 音频文件“预览”链接捕获该 href?

这是 PHP 脚本的一部分,我们在其中获取 iTunes Atom url 的内容,循环浏览 10 个结果,并通过在站点中调用的 $rssresults 为每个条目生成 HTML模板。

    $string = file_get_contents('http://itunes.apple.com/us/rss/topsongs/limit=10/genre=16/explicit=true/xml');

// Remove the colon ":" in the <xxx:yyy> to be <xxxyyy>
$string = preg_replace("/(<\/?)(\w+):([^>]*>)/", "$1$2$3", $string);

if ($f = @fopen($cache_file, 'w')) {
    fwrite ($f, $string, strlen($string));
    fclose($f);
    }
}
$xml = simplexml_load_string($string);

// Output
$rssresults = '';
$count = 1;
$max = 11;

foreach ($xml->entry as $val) {
    if ($count < $max) {

    $rssresults .= '
        <a href="'.$val->id.'" title="'.$val->title.'"><img src="'.$val->imimage[2].'" alt="'.$val->title.'"></a>

        // .m4a preview url?
        <div><a href=" ">Preview</div>

        <div><strong>'.$count.'. <a href="'.$val->id.'" title="'.$val->title.'">'.$val->title.'</a></strong></div>
        <div> from '.$val->imcollection->imname.'</div>;
    }
    $count++;
}

关于如何为每个条目将“.m4a 预览网址”添加到上述脚本中的任何想法?

感谢任何帮助。

【问题讨论】:

    标签: php rss simplexml itunes atom-feed


    【解决方案1】:

    在您的foreach 循环中尝试$val-&gt;link[1]["href"] 会给您网址

    foreach ($xml->entry as $val) {
        // echo the link of Preview
        echo $val->link[1]["href"];
    }
    

    说明:
    由于有多个 link 条目,您可以通过数组索引访问它们。所以索引1 用于访问第二个link 条目。元素的每个属性都可以通过其名称作为元素的键来访问。因此$val-&gt;link[1]["href"] 会给你http://a2.mzstatic.com/us/r1000/116/Music/88/70/a6/mzi.gcauwkkw.aac.p.m4a

    Viper-7

    【讨论】:

    • 啊,我认为$val-&gt;link[1]["href"] 中的[1] 是我所缺少的,以使其跳过每个条目中的第一个“备用”链接。谢谢! :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-24
    • 1970-01-01
    • 1970-01-01
    • 2013-09-25
    • 2012-05-03
    • 1970-01-01
    相关资源
    最近更新 更多