Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8119807
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T04:55:02+00:00 2026-06-06T04:55:02+00:00

This is my first time using XML documents. What I’m trying to do is

  • 0

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).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T04:55:03+00:00Added an answer on June 6, 2026 at 4:55 am

    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";
    }
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is my first time using StAX for parsing XML documents (still in the
I'm using this XML classes for the first time and can't find this piece
This is my first time using XSLT. I'm trying to create a file that
This is my first time using a webservice and I' trying to create a
This is my first time using AJAX with ASP.NET so please allow m some
Sorry, this is my first time using this forum. Apparently people can edit my
I'm fairly new to both Django and Python. This is my first time using
This is the first time ever I'm using AJAX, and I want to do
This is my first time to create a report using iReport plugin in Netbeans
This is my first time doing mobile development with twitter. I'm using Adobe Flash

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.