1

我发布了类似这样的内容,没有任何回应,但这是因为我没有提供足够的数据。我关闭了那个并做了这个,因为它更好地描述了。我不打算做这个。

我的代码:

 <?php
$q = $_GET['q'];
$xml = simplexml_load_file("http://ws.spotify.com/search/1/track?q=".$q."");

echo $xml->tracks . "<br />";
$num = 0;
foreach($xml->children() as $child){
    $num ++;
    if($num < 5){
        echo "<a href='".$child->attributes(href)."' title=''>".$child->name . " by " . $child->artist->name . "</a><br />";
    }
}

 ?>

我试图将“href”的属性称为轨道。

在 Spotify 的网站上,他们显示:

<?xml version="1.0" encoding="utf-8"?>
<tracks xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/" xmlns="http://www.spotify.com/ns/music/1">
  <opensearch:Query role="request" startPage="1" searchTerms="foo"/>
  <opensearch:totalResults>768</opensearch:totalResults>
  <opensearch:startIndex>0</opensearch:startIndex>
  <opensearch:itemsPerPage>100</opensearch:itemsPerPage>
  <track href="spotify:track:3ZsjgLDSvusBgxGWrTAVto">
    <name>The Pretender</name>
    <artist href="spotify:artist:7jy3rLJdDQY21OgRLCZ9sD">
      <name>Foo Fighters</name>
    </artist>
    <id type="isrc">USRW30700007</id>
    <id type="isrc">USRW30700007</id>
    <track-number>1</track-number>
    <length>269.373000</length>
    <popularity>0.79251</popularity>
  </track>
  ...
</tracks>
4

1 回答 1

0

SimpleXML 中的属性非常“简单”;-)

foreach ($xml->children() as $child) {
    ...
    $href = (string)$child['href'];
}
于 2012-05-16T02:06:40.787 回答