This is my first time using XML documents. What I'm trying to do is extract some information from a spotify search. e.g. this page: Spotify Results
I'm not that good with scoping, everything I know so far has been from tutorials.
What I want the end results to look like is somthing like this:
(Album Name) - (Artist Name) (year)
(track number) - (Track Name) - (length)
(track number) - (Track Name) - (length)
(track number) - (Track Name) - (length)
...
Here is the code I have so far.
<?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 />";
}
}
?>
The Problem that I'm having is the Artist Name and Year is not showing, and also it's putting in more " - " then there needs to be. (guessing it has something to do with children() but not outputting any data.)
Also, I was thinking about having this information saved into an array and processing that data into a SQL database. If you know of a better way of writing it with the array that would be awesome. (kill two birds with one stone).
A Super simple way to parse XML to a workable array is like so:
<?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";
}
?>
The root node is "album". You are referencing $root->children()
, when you should just treat the root node as an album object.
$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 . "
");
echo("artist: " . $album->artist->name . "
");
echo("released: " . $album->released . "
");
echo "Tracks:
" ;
foreach($album->tracks->children() as $track){
echo " " . $track->{'track-number'} .
" - " .
$track->name .
" - " .
$track->length .
"<br />
";
}
output:
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 />
Something like this might work for you:
<?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);