dongshang1768 2012-06-20 17:35
浏览 24
已采纳

PHP simpleXML范围

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

  • 写回答

3条回答 默认 最新

  • dousenjue3214 2012-06-20 17:54
    关注

    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";
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算