You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用PHP将Singtel EPG JSON接口数据转换为指定格式XML文件

Singtel EPG接口转标准XML格式PHP实现方案

实现逻辑

  • 先从Singtel官方节目指南页提取全量频道ID与对应频道名称
  • 按指定拉取天数,批量请求对应日期的EPG JSON接口,接口日期格式为ddMMyyyy,时区统一使用Asia/Singapore
  • 解析JSON数据,提取对应频道下的节目开始时间、结束时间、节目标题、节目描述字段
  • 用DOMDocument生成符合标准的EPG XML文件,避免XML转义错误

完整实现代码

<?php
date_default_timezone_set('Asia/Singapore');
// 配置参数
$maxDays = 7; // 最多支持7天,和官方接口限制一致
$outputFile = 'singtel_epg.xml';
$sgTimezone = new DateTimeZone('Asia/Singapore');

// 1. 获取频道列表
$channelListUrl = 'https://www.singtel.com/personal/products-services/tv/tv-programme-guide';
$ch = curl_init($channelListUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$channelPageContent = curl_exec($ch);
curl_close($ch);

// 匹配频道ID和名称,对应WebGrab的正则规则
preg_match_all('/\{"title":"([^"]*)","description".*?"epgChannelId":"(\d+)"\}/', $channelPageContent, $channelMatches);
$channels = [];
for ($i=0; $i<count($channelMatches[1]); $i++) {
    $channelId = $channelMatches[2][$i];
    $channelName = htmlspecialchars_decode($channelMatches[1][$i]);
    $channels[$channelId] = $channelName;
}

// 2. 拉取多日EPG数据
$programs = [];
for ($day=0; $day<$maxDays; $day++) {
    $dateStr = date('dmY', strtotime("+{$day} day"));
    $epgUrl = "https://www.singtel.com/etc/singtel/public/tv/epg-parsed-data/{$dateStr}.json";
    
    $ch = curl_init($epgUrl);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    $jsonContent = curl_exec($ch);
    curl_close($ch);
    
    $epgData = json_decode($jsonContent, true);
    if (!$epgData) continue;
    
    // 提取节目数据
    foreach ($epgData as $item) {
        $channelId = $item['channelid'] ?? '';
        if (!isset($channels[$channelId])) continue;
        
        $startTime = DateTime::createFromFormat('Y-m-d\TH:i:s.u\Z', $item['startDateTime'], new DateTimeZone('UTC'));
        $startTime->setTimezone($sgTimezone);
        $endTime = DateTime::createFromFormat('Y-m-d\TH:i:s.u\Z', $item['endDateTime'], new DateTimeZone('UTC'));
        $endTime->setTimezone($sgTimezone);
        
        $programs[] = [
            'channel_id' => $channels[$channelId],
            'start' => $startTime->format('YmdHis O'),
            'stop' => $endTime->format('YmdHis O'),
            'title' => $item['title'] ?? '',
            'desc' => $item['program']['description'] ?? ''
        ];
    }
}

// 3. 生成XML文件
$dom = new DOMDocument('1.0', 'UTF-8');
$dom->formatOutput = true;

$tvNode = $dom->createElement('tv');
$tvNode->setAttribute('generator-info-name', 'Singtel EPG Generator');
$tvNode->setAttribute('generator-info-url', 'http://www.singteltv.com.sg');
$dom->appendChild($tvNode);

// 添加频道节点
foreach ($channels as $channelName) {
    $channelNode = $dom->createElement('channel');
    $channelNode->setAttribute('id', $channelName);
    
    $displayNameNode = $dom->createElement('display-name');
    $displayNameNode->setAttribute('lang', 'en');
    $displayNameNode->appendChild($dom->createTextNode($channelName));
    $channelNode->appendChild($displayNameNode);
    
    $urlNode = $dom->createElement('url', 'http://www.singteltv.com.sg');
    $channelNode->appendChild($urlNode);
    
    $tvNode->appendChild($channelNode);
}

// 添加节目节点
foreach ($programs as $prog) {
    $progNode = $dom->createElement('programme');
    $progNode->setAttribute('start', $prog['start']);
    $progNode->setAttribute('stop', $prog['stop']);
    $progNode->setAttribute('channel', $prog['channel_id']);
    
    $titleNode = $dom->createElement('title');
    $titleNode->setAttribute('lang', 'en');
    $titleNode->appendChild($dom->createTextNode($prog['title']));
    $progNode->appendChild($titleNode);
    
    if (!empty($prog['desc'])) {
        $descNode = $dom->createElement('desc');
        $descNode->setAttribute('lang', 'en');
        $descNode->appendChild($dom->createTextNode($prog['desc']));
        $progNode->appendChild($descNode);
    }
    
    $tvNode->appendChild($progNode);
}

$dom->save($outputFile);
echo "EPG文件生成成功,路径:{$outputFile}";
?>

使用说明

  • 代码依赖curl和dom扩展,大部分PHP环境默认已开启
  • 可修改开头的$maxDays参数调整拉取天数,最大支持7天
  • 生成的XML文件默认保存在代码同目录的singtel_epg.xml,可修改$outputFile参数调整保存路径
  • 如需调整XML根节点的生成器信息,直接修改$tvNode->setAttribute对应参数即可

内容的提问来源于stack exchange,提问作者mynameis

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 12:24:07