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?`
$url = 'https://raw.githubusercontent.com/HelmerLuzo/TDTChannels_EPG/master/TDTChannels_EPG.xml';
$xml=simplexml_load_file("$url");
foreach($xml->programme as $item){
echo "Logo : ".'<icon src="' .$item->icon. '"/>';
echo "Title : ".$item->title. "<br>";
echo "<br>";
}
output after code (missing icon):
Logo : Title : La hora de La 1 La hora política
Logo : Title : Menudos Torres
Logo : Title : Corazón ...
XML url is like this:
<programme start="20220513083000 0200" stop="20220513100000 0200" channel="24Horas.TV">
<title lang="es">La hora de La 1 La hora política</title>
<sub-title lang="es">La hora política</sub-title>
<desc lang="es">Magazine matinal de la 1 presentado por Marc Sala y Silvia Intxaurrondo en el que se reúne información de actualidad con reportajes y entrevistas dedicados a analizar en profundidad el mundo de la política.</desc>
<date>2022</date>
<category lang="es">Entretenimiento</category>
<category lang="es">Magacín</category>
<icon src="https://www.movistarplus.es/recorte/n/caratula4/M24HP2071537" />
<country lang="es">España</country>
<episode-num system="onscreen">T3 E169</episode-num>
<rating system="ES">
<value>7</value>
</rating>
CodePudding user response:
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.
<?php
$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';
$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'],
'Description' => '',
'Date' => '',
'Start' => '',
'Stop' => ''
];
if (isset($programme['desc']))
$nowShowing['Description'] = $programme['desc'];
if (isset($programme['date']))
$nowShowing['Date'] = $programme['date'];
$nowShowing['Start'] = $start->format($outputFormat);
$nowShowing['Stop'] = $stop->format($outputFormat);
$channels[$id]['Now Showing'] = $nowShowing;
}
}
var_dump($channels);
This will output a series of array elements like so:
["AlJQ.TV"]=>
array(2) {
["Display Name"]=>
string(16) "Al Jazeera Qatar"
["Now Showing"]=>
array(5) {
["Title"]=>
string(8) "Newshour"
["Description"]=>
string(192) "Boletín informativo que hace un repaso a las últimas noticias del día desde los distintos distintos centros internacionales que Al Jazeera tiene en Doha, Kuala Lumpur, Londres y Washington."
["Date"]=>
string(4) "2006"
["Start"]=>
string(25) "2022-05-13 07:00:00 1000"
["Stop"]=>
string(25) "2022-05-13 08:00:00 1000"
}
}
CodePudding user response:
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 "\r\n";
}