从 XML URL 获取图标

Get icon from XML URL

我正在为网站创建一些 EPG。我没有经验。不幸的是,我发现这很难。我怎样才能得到图标,这段代码有什么问题?`

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>";
     }
    ?>

这很有趣。这使用已知的技巧将 xml 转换为使用 json_decodejson_encode 的数组,在我看来这使得它更容易使用。它还广泛使用 DateTime 对象。

$url = 'https://raw.githubusercontent.com/HelmerLuzo/TDTChannels_EPG/master/TDTChannels_EPG.xml';
$xml = simplexml_load_file($url);
$xml = json_decode(json_encode($xml), true); // Hack so we don't have to cast values in the future

// https://www.php.net/manual/en/timezones.php
$originalTimezone = 'Europe/Madrid';
$changeTimezone = 'Australia/Sydney'; // If empty, will not change timezone
//$outputFormat = 'Y-m-d H:i:s O';
$outputFormat = 'G:i';

$channels = [];
foreach ($xml['channel'] as $channel) {
    $id = $channel['@attributes']['id'];
    $channels[$id]['Display Name'] = $channel['display-name'];
    $channels[$id]['Now Showing'] = [];
}

$now = new DateTime('now', new DateTimeZone($originalTimezone));
$programmes = [];
foreach ($xml['programme'] as $programme) {
    $id = $programme['@attributes']['channel'];

    $start = $programme['@attributes']['start'];
    $start = DateTime::createFromFormat('YmdHis O', $start);
    if (!empty($changeTimezone))
        $start = $start->setTimezone(new DateTimeZone($changeTimezone));
    
    $stop = $programme['@attributes']['stop'];
    $stop = DateTime::createFromFormat('YmdHis O', $stop);
    if (!empty($changeTimezone))
        $stop = $stop->setTimezone(new DateTimeZone($changeTimezone));

    if ($start < $now && $now < $stop) {
        $nowShowing = [
            'Title' => $programme['title'],
            'Subtitle' => '',
            'Description' => '',
            'Date' => '',
            'Start' => '',
            'Stop' => '',
            'Icon' => '',
        ];
    
        if (isset($programme['sub-title']))
            $nowShowing['Subtitle'] = $programme['sub-title'];

        if (isset($programme['desc']))
            $nowShowing['Description'] = $programme['desc'];
    
        if (isset($programme['date']))
            $nowShowing['Date'] = $programme['date'];

        if (isset($programme['date']))
            $nowShowing['Icon'] = $programme['icon']['@attributes']['src'];

        $nowShowing['Start'] = $start->format($outputFormat);
        $nowShowing['Stop'] = $stop->format($outputFormat);
    
        $channels[$id]['Now Showing'] = $nowShowing;
    }
}

foreach ($channels as $id => $details) {
    echo $details['Now Showing']['Start'] . ' - ' . $details['Now Showing']['Stop'] . '<br>' . 
         'Channel:' . $details['Display Name'] . '<br>' . 
         'Title: ' . $details['Now Showing']['Title'] . '<br>' . 
         'Info: ' . $details['Now Showing']['Subtitle'] . '<br>' . 
         'Description: ' . $details['Now Showing']['Description'] . '<br>' . 
         'Logo: <img src="'.$details['Now Showing']['Icon'].'"/><br><br>'; 
}

如果我对你的理解是正确的,像这样的事情应该会让你接近我认为你正在尝试做的事情。请注意,并非所有程序都有相关徽标。

$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 "\r\n";
}

编辑:

我仍然不完全确定我理解你想要什么,但听起来你需要将整个内容包装在 html 中并输出到浏览器。类似于:

<!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>