'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>";
}
?>
Solution 1:[1]
This was a fun one. This uses a known hack to convert the xml into an array using json_decode
and json_encode
, this makes it easier to work with in my opinion. It also makes extensive use of the DateTime
object.
$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>';
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |