嘗試使用PHP腳本解析iTunes Atom提要。如果您訪問iTunes RSS Generator,您可以生成一個Atom feed這樣的:用PHP解析iTunes RSS Atom feed?
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&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&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&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腳本,我可以得到如標題的結果,id,im:image爲每個[條目]在腳本輸出中使用。我需要的是來自鏈接條目之一的網址。特別我需要的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鏈接在腳本結果中用於每個條目的結果。
如何捕獲上述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 preview url」添加到每個條目的上述腳本的任何想法?
感謝任何幫助。
啊,我覺得'[1]'的'$ val->鏈接[1] [ 「HREF」]'是什麼我失蹤,使其跳過那是每個條目中第一個「備用」鏈接。謝謝! :) – jca 2012-04-01 15:30:39