1

这是我第一次使用 XML 文档。我想要做的是从 spotify 搜索中提取一些信息。例如此页面:Spotify 结果

我对范围界定不是那么好,到目前为止我所知道的一切都来自教程。

我希望最终结果看起来像这样:

(Album Name) - (Artist Name) (year)

(track number) - (Track Name) - (length)
(track number) - (Track Name) - (length)
(track number) - (Track Name) - (length)
...

这是我到目前为止的代码。

<?php
 function print_r2($val){
        echo '<pre>';
        print_r($val);
        echo  '</pre>';
}

$URL = "http://ws.spotify.com/lookup/1/?uri=spotify:album:77Tgq2oSMvgP4Y9pBVKRJa&extras=trackdetail";

$xml = simplexml_load_file($URL) 
   or die("Error: Cannot create object");

//print_r2($xml);

foreach($xml->children() as $album){
    echo($album->name);
    echo($album->artist->name);
    echo($album->released);

    foreach($album->children() as $track => $data){
      echo $data->{'track-number'};
      echo("  -  ");
      echo $data->name;
      echo("  -  ");
      echo $data->length;
      echo "<br />";
    }
}

?>

我遇到的问题是没有显示艺术家姓名和年份,而且它还添加了更多的“-”,然后就需要了。(猜测它与 children() 有关,但不输出任何数据。)

另外,我正在考虑将这些信息保存到一个数组中,然后将这些数据处理到一个 SQL 数据库中。如果您知道使用数组编写它的更好方法,那就太棒了。(一举两得)。

4

3 回答 3

2

根节点是“专辑”。$root->children()当您应该将根节点视为专辑对象时,您正在引用。

$url = "http://ws.spotify.com/lookup/1/?uri=spotify:album:77Tgq2oSMvgP4Y9pBVKRJa&extras=trackdetail";

$album = simplexml_load_file($url)
    or die("Error: Cannot create object");

echo("name: " . $album->name . "\n");
echo("artist: " . $album->artist->name . "\n");
echo("released: " . $album->released . "\n");
echo "Tracks:\n" ;
foreach($album->tracks->children() as $track){
    echo "  " . $track->{'track-number'} .
        "  -  " .
            $track->name .
            "  -  " .
            $track->length .
            "<br />\n";
}

输出:

name: Be Lifted High
artist: Bethel Live
released: 2011
Tracks:
1  -  You Are Good (feat. Brian Johnson)  -  265.520000<br />
2  -  One Thing Remains (feat. Brian Johnson)  -  300.973000<br />
3  -  Furious (feat. Jeremy Riddle)  -  316.440000<br />
4  -  Be Lifted High (feat. Brian Johnson)  -  414.493000<br />
5  -  God I Look To You (feat. Jenn Johnson)  -  452.520000<br />
6  -  I Will Exalt (feat. Amanda Falk)  -  404.880000<br />
7  -  What Would I Have Done (feat. Brian Johnson and Jenn Johnson)  -  373.160000<br />
8  -  Hope's Anthem (feat. William Matthews)  -  345.160000<br />
9  -  Love Came Down (feat. Brian Johnson)  -  306.880000<br />
10  -  Deep Cries Out (feat. William Matthews)  -  314.480000<br />
11  -  God Of The Redeemed (feat. Jeremy Riddle)  -  368.426000<br />
12  -  Forever And A Day (feat. Jenn Johnson)  -  425.466000<br />
13  -  One Thirst (feat. Jeremy Riddle and Steffany Frizzell)  -  425.493000<br />
于 2012-06-20T17:52:34.880 回答
1

一种将 XML 解析为可用数组的超级简单方法如下:

<?php 
$xml = file_get_contents('http://ws.spotify.com/lookup/1/?uri=spotify:album:77Tgq2oSMvgP4Y9pBVKRJa&extras=trackdetail');

//Type cast XML into Array
$a = json_decode(json_encode((array) simplexml_load_string($xml)),1);

//See the full array
//print_r($a);

echo $a['name'].' - '.$a['artist']['name'].' - '.$a['released'].'<br />'.PHP_EOL;

foreach($a['tracks']['track'] as $track){
    echo $track['track-number'].' - '.$track['name'].' - '.secondsToTime($track['length']).'<br />'.PHP_EOL;
}

/*
Be Lifted High - Bethel Live - 2011<br />
1 - You Are Good (feat. Brian Johnson) - 4m25s<br />
2 - One Thing Remains (feat. Brian Johnson) - 5m0s<br />
3 - Furious (feat. Jeremy Riddle) - 5m16s<br />
4 - Be Lifted High (feat. Brian Johnson) - 6m54s<br />
5 - God I Look To You (feat. Jenn Johnson) - 7m32s<br />
6 - I Will Exalt (feat. Amanda Falk) - 6m44s<br />
7 - What Would I Have Done (feat. Brian Johnson and Jenn Johnson) - 6m13s<br />
8 - Hope's Anthem (feat. William Matthews) - 5m45s<br />
9 - Love Came Down (feat. Brian Johnson) - 5m6s<br />
10 - Deep Cries Out (feat. William Matthews) - 5m14s<br />
11 - God Of The Redeemed (feat. Jeremy Riddle) - 6m8s<br />
12 - Forever And A Day (feat. Jenn Johnson) - 7m5s<br />
13 - One Thirst (feat. Jeremy Riddle and Steffany Frizzell) - 7m5s<br />
*/

function secondsToTime($seconds){
     // minutes
    $divisor_for_minutes = $seconds % (60 * 60);
    $minutes = floor($divisor_for_minutes / 60);

    // seconds
    $divisor_for_seconds = $divisor_for_minutes % 60;
    $seconds = ceil($divisor_for_seconds);

    // return the final song len string
    return $minutes."m".$seconds."s";
}
?>
于 2012-06-20T17:54:08.720 回答
1

这样的事情可能对你有用:

<?php
$url = "http://ws.spotify.com/lookup/1/?uri=spotify:album:77Tgq2oSMvgP4Y9pBVKRJa&extras=trackdetail";
$xml = new SimpleXMLElement(file_get_contents($url));

$album = array(
    'name' => (string) $xml->name,
    'artist' => (string) $xml->artist->name,
    'released' => (string) $xml->released,
    'tracks' => array()
);

foreach ($xml->tracks->track as $track) {
    $album['tracks'][] = array(
        'number' => (string) $track->{'track-number'},
        'name' => (string) $track->name,
        'length' => (string) $track->length
    );
}

print_r($album);
于 2012-06-20T17:57:21.057 回答