0

I want the track-id that is listed in the href attribute.

This is the spotify xml file:

http://ws.spotify.com/search/1/track?q=Sportfreunde+Stiller+AND+track:Ein+Kompliment

This is what I tried so far:

$url = "http://ws.spotify.com/search/1/track?q=Sportfreunde+Stiller+AND+track:Ein+Kompliment";

$xml = simplexml_load_file($url);

foreach ($xml->track as $track) {
    echo $track->name."<br>";
}

How to get in the upper hierarchy?

This is the desired output: spotify:track:3ptJBraFGtaeprD4BHmTfV. In my code I just put the name of the track in the output so that you can see that I am in the wrong hierarchy.

hakre
  • 193,403
  • 52
  • 435
  • 836
bademeister
  • 107
  • 1
  • 11
  • Upper hierarchy you mean to traverse deeper into the document tree? Check the [simplexml usage examples](http://php.net/simplexml.examples-basic), they demonstrate this pretty well and with examples. – M8R-1jmw5r Apr 30 '13 at 13:05
  • possible duplicate of [PHP SimpleXML + Get Attribute](http://stackoverflow.com/questions/10537657/php-simplexml-get-attribute) – hakre May 02 '13 at 12:54

2 Answers2

0

You can use $track->attributes() instead of $track->name, it will output the value in the href attribute.

Ex:

<?php
$url = "http://ws.spotify.com/search/1/track?q=Sportfreunde+Stiller+AND+track:Ein+Kompliment";

$xml = simplexml_load_file($url);

foreach ($xml->track as $track) {
    echo $track->attributes() . "<br>";
}
?>
Daniel Mensing
  • 956
  • 5
  • 13
  • I want to use the href attribute in a other variable. In this way it would be nice to have a direct pointer to it – bademeister Apr 30 '13 at 13:16
  • appending ->href to attributes() will only output value for href attribute. You can also access the tracks like this: $xml->track[0]->attributes()->href; Where [0] is the number of the track. – Daniel Mensing Apr 30 '13 at 13:38
  • @MartinFranke: This answer also directly explains how you access attributes - with array-access and the attributes name: http://stackoverflow.com/a/16306029/367456 - this is just a pick from the same day. Sure we already had this answered long ago. If you need a good answer, it's often better to use the search. Let me know if you've got more questions. – hakre May 02 '13 at 12:53
0

do print_r($xml) and give what you get

It worked fine for me I got name or result like

Ein Kompliment
Ein Kompliment - Unplugged
Ein Kompliment - Unplugged
Ein Kompliment
Ein Kompliment
Ein Kompliment - Live aus der Olympiahalle München am 26.05.04
Ein Kompliment - Live aus der Olympiahalle München am 26.05.04
Ein Kompliment
Ein Kompliment (Tribute To Sportfreunde Stiller)
Ein Kompliment (Unplugged Karaoke Version) - Originally Performed By Sportfreunde Stiller
Ein Kompliment - Originally Performed By Sportfreunde Stiller
Ein Kompliment (Tribute To Sportfreunde Stiller)
Ein Kompliment - Originally Performed By Sportfreunde Stiller
Ein Kompliment (Karaoke Version) - Originally Performed By Sportfreunde Stiller
Ein Kompliment (Karaoke Version) - Originally Performed By Sportfreunde Stiller
Darshan
  • 327
  • 1
  • 2
  • 11