Search code examples
phpxmlcover

Open XML elements in PHP


Good, I created an XML document with the name and title of songs and artists within that document come also the corresponding cover. Now the link of my XML file is: http://inlivefm.96.lt/nw.xml .

I also have another XML document that gives me the name and artist title that this amounts to the moment (ADELE - HELLO) Now the link of my XML file is: http://inlivefm.96.lt/NowOnAir.xml .

Well, what I'm trying to make is that with document I created give me the cover of this song to give right now. I tried to make a code but to no avail in PHP, so I came to ask your help to get success. Here I leave the code I am using to try to get what I want.

<?php
$xml = simplexml_load_file('http://inlivefm.6te.net/agora.xml');
$artist = urlencode($xml->Event->Song->Artist['name']);
$track = urlencode($xml->Event->Song['title']);
$url = simplexml_load_file("http://inlivefm.96.lt/nw.xml");
$largeImage = $url->xpath('/ilm/$artist- $track/image[@size="cover"]')[0];
echo '<img src="'.$largeImage.'" />';     
?>

Can you please help me doing this?

Thank you all in advance.


Solution

  • correct xpath to take image is

    (/ilm/artist/image[@size="cover"])[count(/ilm/artist/name[.="ADELE-HELLO"]/preceding-sibling::name)+1]
    

    because you need to get image with the same position as artist's name.

    In php code it should be something like this:

    $XML = simplexml_load_file('http://inlivefm.6te.net/agora.xml');
    $artist = $XML->Event->Song->Artist['name']; 
    $track = $XML->Event->Song['title']; 
    
    $URL = simplexml_load_file("http://inlivefm.96.lt/nw.xml"); 
    $largeImage = $URL->xpath('(/ilm/artist/image[@size="cover"])[count(/ilm/artist/name[.="'. $artist .' - '. $track.'"]/preceding-sibling::name)+1]'); 
    echo '<img src = "'. $largeImage[0]. '" />';