Skip to content
Advertisement

Get icon from XML URL

I am creating some EPG for the website. I do not have experience. Unfortunately, I’m finding it difficult. How can I get the icons, what is wrong in this code?`

Final code working:

    <?php
     $url = 'XML URL LINK';
     $xml=simplexml_load_file("$url");  
     $progs = $xml->xpath('//programme');
     foreach ($progs as $prog) {
       $title = $prog->xpath('./title/text()')[0];
       $link = (count($prog->xpath('./icon/@src'))>0) ? ($prog->xpath('./icon/@src'))[0] : ("No icon");
       echo "Logo: <img src='{$link}'> <br/>" ;
       echo "Title : ".$title. "<br>";
       echo "<br>";
     }
    ?>

Advertisement

Answer

If I understand you correctly, something like this should get you close to what I think you are trying to do. Note that not all programs have associated logos.

$progs = $xml->xpath('//programme');

foreach ($progs as $prog) {
    $title = $prog->xpath('./title/text()')[0];
    $link = (count($prog->xpath('./icon/@src'))>0) ? ($prog->xpath('./icon/@src'))[0] : ("No icon");
    echo($title .": ".  $link);
    echo "rn";
}

EDIT:

I’m still not entirely sure I understand what you want, but it sounds like you need to wrap the whole thing in html and output to a browser. Something like:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <?php
         $url = 'https://raw.githubusercontent.com/HelmerLuzo/TDTChannels_EPG/master/TDTChannels_EPG.xml';
         $xml=simplexml_load_file("$url");  
         $progs = $xml->xpath('//programme');
         foreach ($progs as $prog) {
           $title = $prog->xpath('./title/text()')[0];
           $link = (count($prog->xpath('./icon/@src'))>0) ? ($prog->xpath('./icon/@src'))[0] : ("No icon");
           echo "Logo: <img src='{$link}'> <br/>" ;
           echo "Title : ".$title. "<br>";
           echo "<br>";
}
        ?>
    </body>
</html>
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement