0
Good evening. I have the following xml file:
<MusicHistory CompactMode="1">
<Item0 Block="2018-02-11T23:00:00" Folder="Musicas" Music="Zara Larsson - So Good" Composer="" Comment="" Start="2018-11-03T02:19:13.000Z"/>
<Item1 Block="2018-02-11T23:00:00" Folder="Musicas" Music="The Weeknd - Pray For Me" Composer="" Comment="" Start="2018-11-03T02:15:48.000Z"/>
<Item2 Block="2018-02-11T23:00:00" Folder="Musicas" Music="Shawn Mendes - Treat You Better" Composer="" Comment="" Start="2018-11-03T02:12:49.000Z"/>
<Item3 Block="2018-02-11T22:00:00" Folder="Musicas" Music="James Arthur - Say You Won't Let Go" Composer="" Comment="" Start="2018-11-03T02:09:27.000Z"/>
<Item4 Block="2018-02-11T22:00:00" Folder="Musicas" Music="Tove Lo - Cool Girl" Composer="" Comment="" Start="2018-11-03T02:06:12.000Z"/>
<Item5 Block="2018-02-11T22:00:00" Folder="Musicas" Music="Avicii - Taste The Feeling" Composer="" Comment="" Start="2018-11-03T02:03:06.000Z"/>
<Item6 Block="2018-02-11T22:00:00" Folder="Musicas" Music="Jason Derulo - Tiptoe" Composer="" Comment="" Start="2018-11-03T01:59:58.000Z"/>
<Item7 Block="2018-02-11T22:00:00" Folder="Musicas" Music="Little Mix - Love Me Like You" Composer="" Comment="" Start="2018-11-03T01:56:43.000Z"/>
<Item8 Block="2018-02-11T22:00:00" Folder="Musicas" Music="David Gueta - Play Hard" Composer="" Comment="" Start="2018-11-03T01:53:25.000Z"/>
<Item9 Block="2018-02-11T22:00:00" Folder="Musicas" Music="OneRepublic - Apoligize" Composer="" Comment="13+6319C8200604F0C2D46E050A7FC26FE1+10221800" Start="2018-11-03T01:50:14.000Z"/>
<Item10 Block="2018-02-11T22:00:00" Folder="Musicas" Music="Katy Perry - Teenage Dream" Composer="" Comment="" Start="2018-11-03T01:46:34.000Z"/>
<Item11 Block="2018-02-11T22:00:00" Folder="Musicas" Music="Bebe Rexha – No Broken Hearts" Composer="" Comment="" Start="2018-11-03T01:42:38.000Z"/>
<Item12 Block="2018-02-11T22:00:00" Folder="Musicas" Music="MC G15 - Deu Onda" Composer="" Comment="" Start="2018-11-03T01:39:27.000Z"/>
<Item13 Block="2018-02-11T22:00:00" Folder="Musicas" Music="Maluma - Sín Contrato" Composer="" Comment="" Start="2018-11-03T01:35:53.000Z"/>
<Item14 Block="2018-02-11T22:00:00" Folder="Musicas" Music="Pitbull - Options" Composer="" Comment="" Start="2018-11-03T01:31:56.000Z"/>
<Item15 Block="2018-02-11T22:00:00" Folder="Musicas" Music="Rihanna - Umbrella" Composer="" Comment="" Start="2018-11-03T01:27:48.000Z"/>
<Item16 Block="2018-02-11T22:00:00" Folder="Musicas" Music="Skillet - Famous" Composer="" Comment="" Start="2018-11-03T01:24:33.000Z"/>
<Item17 Block="2018-02-11T22:00:00" Folder="Musicas" Music="The Vamps - All Night" Composer="" Comment="" Start="2018-11-03T01:21:26.000Z"/>
<Item18 Block="2018-02-11T22:00:00" Folder="Musicas" Music="OMI - Cheerleader" Composer="" Comment="" Start="2018-11-03T01:18:27.000Z"/>
<Item19 Block="2018-02-11T22:00:00" Folder="Musicas" Music="Austin Mahone - MMM Yeah" Composer="" Comment="" Start="2018-11-03T01:14:38.000Z"/>
<Item20 Block="2018-02-11T22:00:00" Folder="Musicas" Music="Demi Lovato - Stone Cold" Composer="" Comment="" Start="2018-11-03T01:11:36.000Z"/>
</MusicHistory>
I’m trying to list each of this item in php, but I was not successful.
Like I’m doing:
$am = curl_init();
curl_setopt($am, CURLOPT_URL, 'file:///C:/Playlist/pgm/Montagem/MusicHistory.xml');
curl_setopt($am, CURLOPT_RETURNTRANSFER, true);
$music = curl_exec($am);
curl_close($am);
$history = simplexml_load_string($music);
$i = 0;
foreach($history->MusicHistory as $listar){
$data = array(
'musica' => $listar->Item0[0]->attributes()['Music'],
'numero' => $i
);
echo json_encode($array, JSON_PRETTY_PRINT);
$i++;
}
The problem is that the foreach does not return anything. Note: outside the foreach I can get the data normally. For example: $history->Item0[0]->Attributes()['Music'].
But I would like to simplify the code so I don’t have to make an array for every $i item.
No return. The return action I’m using is echo json_encode($array, JSON_PRETTY_PRINT);
– Paulo Sérgio Filho
How you are setting the content in the variable
$history
? If possible post a code that can be tested.– Valdeir Psr
simplexml_load_string. I will post my Curl that I use to get xml.
– Paulo Sérgio Filho