PHP XML 调用属性


PHP XML calling attributes

我发布了这样的东西,没有回复,但那是因为我没有提供足够的数据。我关闭了那个并做了这个,因为它更好地描述了。我不打算做这个。

我的代码:

 <?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>

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

foreach ($xml->children() as $child) {
    ...
    $href = (string)$child['href'];
}